repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/ui/__tests__/server.test.ts
RawFile outline
- ui network helpers line 6
- prefers non-internal IPv4 LAN addresses line 7
- falls back to 127.0.0.1 when no LAN address exists line 18
- builds token URLs and redacts tokens in diagnostics line 26
- ui errors line 34
- formats human-actionable errors line 35
- ui server line 47
- serves shell only with query token and sets strict cookie line 48
- accepts cookie and bearer auth for status line 69
- rejects wrong tokens line 94
import { describe, expect, it } from "vitest";
import { humanError, redactToken, uiError } from "../errors";
import { chooseLanAddress, uiUrl } from "../network";
import { startUiServer } from "../server";
describe("ui network helpers", () => {
it("prefers non-internal IPv4 LAN addresses", () => {
const address = chooseLanAddress({
lo: [{ address: "127.0.0.1", family: "IPv4", internal: true } as never],
wifi: [
{ address: "192.168.1.44", family: "IPv4", internal: false } as never,
],
});
expect(address).toBe("192.168.1.44");
});
it("falls back to 127.0.0.1 when no LAN address exists", () => {
const address = chooseLanAddress({
lo: [{ address: "127.0.0.1", family: "IPv4", internal: true } as never],
});
expect(address).toBe("127.0.0.1");
});
it("builds token URLs and redacts tokens in diagnostics", () => {
const url = uiUrl("10.0.0.9", 54321, "abc");
expect(url).toBe("http://10.0.0.9:54321/?token=abc");
expect(redactToken(url)).toBe("http://10.0.0.9:54321/?token=<redacted>");
});
});
describe("ui errors", () => {
it("formats human-actionable errors", () => {
const err = uiError("server_bind_failed", "Could not bind LAN server", {
action: "Pick another port with /ui --port <port>.",
retryable: true,
});
expect(humanError(err)).toContain("Could not bind LAN server");
expect(humanError(err)).toContain("Pick another port");
expect(humanError(err)).toContain("Retryable: yes");
});
});
describe("ui server", () => {
it("serves shell only with query token and sets strict cookie", async () => {
const server = await startUiServer({
address: "127.0.0.1",
token: "tok",
preferredPort: 0,
});
try {
const unauthorized = await fetch(server.baseUrl);
expect(unauthorized.status).toBe(401);
expect(await unauthorized.text()).toContain("Unauthorized");
const authorized = await fetch(`${server.baseUrl}/?token=tok`);
expect(authorized.status).toBe(200);
expect(authorized.headers.get("set-cookie")).toContain("pi_ui_token=tok");
expect(authorized.headers.get("set-cookie")).toContain("SameSite=Strict");
expect(await authorized.text()).toContain("Pi UI shell");
} finally {
await server.close("test");
}
});
it("accepts cookie and bearer auth for status", async () => {
const server = await startUiServer({
address: "127.0.0.1",
token: "tok",
preferredPort: 0,
});
try {
const cookie = await fetch(`${server.baseUrl}/api/status`, {
headers: { cookie: "pi_ui_token=tok" },
});
expect(cookie.status).toBe(200);
expect(await cookie.json()).toMatchObject({
state: "waiting",
clients: 0,
});
const bearer = await fetch(`${server.baseUrl}/api/status`, {
headers: { authorization: "Bearer tok" },
});
expect(bearer.status).toBe(200);
} finally {
await server.close("test");
}
});
it("rejects wrong tokens", async () => {
const server = await startUiServer({
address: "127.0.0.1",
token: "tok",
preferredPort: 0,
});
try {
const response = await fetch(`${server.baseUrl}/api/status`, {
headers: { authorization: "Bearer wrong" },
});
expect(response.status).toBe(401);
} finally {
await server.close("test");
}
});
});