Files
pi2d/test/daemon/config.test.ts
T
yuez a57c874095 feat(daemon)!: phone-first binding and a two-step pairing flow
Implements ADR 0001 (docs/adr/0001-phone-first-pairing.md): the console is
meant to be opened from a phone, so reachability stops being a mode and the
pairing link becomes the only access control that matters.

Binding / CORS
- `PI2_HOST` defaults to `0.0.0.0`; `PI2_ACCESS_MODE` (local|lan|tailscale) is
  deleted, together with its parser, `AccessMode` type and LAN probe.
- `src/core/network.ts` is the single source of truth for reachable addresses:
  kind classification, ordering (public → lan → other → tailscale → loopback,
  physical NICs first, link-local skipped), bind-aware `networkBases()`,
  `primaryBase()` and `localOrigins()`. Config, the pairing route, doctor and
  the CLI all consume it, so the CORS allowlist always matches the pairing URL.
- `/api/v1/info` and `daemon.json` report `host` instead of `access`; doctor's
  `access` check becomes `bind` (loopback ok, wildcard warns unless an `https:`
  public URL is configured).

Pairing
- `POST /api/v1/pair` only mints, requires a token, and returns the code, the
  chosen URL, the expiry, the nonce, the QR matrix and every usable base.
  `base` may pin one of those bases; anything else is a 400.
- `POST /api/v1/pair/redeem` is the public half, rate limited per client
  (5/min) and globally (50/h) → 429 `E_RATE_LIMITED` with `retry-after`.
- `GET /pair/:code` answers 302 to `/connect?code=…&auto=1`, so a scanned link
  needs no JavaScript to reach the console.
- `src/core/qr.ts` (moved out of the CLI) is the one encoder: byte mode, EC
  level M, versions 1–10, rows without a quiet zone, shared by the HTTP mint
  response and the terminal renderer.
- `pi2 pairing` prints a human report plus a half-block terminal QR and keeps
  `--base`; `--json` and `--public-url` are gone (the Neovim client calls the
  HTTP mint directly).

Device scopes
- `Scope`, `SCOPES`, `DEFAULT_SCOPES`, `DeviceRecord.scopes` and the `scopes`
  field in device/hello payloads are removed: nothing enforced them, and a
  paired device now holds the same rights as the local browser. A permission
  model, if wanted, gets its own ADR.

Breaking: `PI2_ACCESS_MODE`, `info.access`, `pi2 status --json`'s `access`,
`pi2 pairing --json`/`--public-url`, `POST /api/v1/pair {code}`, and the
`scopes` field. All in-repo callers and the e2e drivers are updated; the plugin
side (`:PiWebAccess`, `env.PI2_ACCESS_MODE`, text-format parsing) follows in
pi2.nvim.

Verification: root typecheck clean; 369/370 tests (the one failure is the
pre-existing supervisor timing flake, 24/24 in isolation ×3); `smoke.sh` green;
web suite 299/299; visual matrix 24/24.
2026-10-27 10:44:08 +08:00

117 lines
4.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import type { NetworkInterfaceInfo } from "node:os";
import { DEFAULT_HOST, loadConfig } from "../../src/config.js";
import { localOrigins } from "../../src/core/network.js";
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..");
const LOOPBACK = (port: number): string[] => [
`http://127.0.0.1:${port}`,
`http://localhost:${port}`,
`http://[::1]:${port}`,
];
const ipv4 = (address: string, overrides: Partial<NetworkInterfaceInfo> = {}): NetworkInterfaceInfo => ({
address,
netmask: "255.255.255.0",
family: "IPv4",
mac: "00:00:00:00:00:00",
internal: false,
cidr: `${address}/24`,
...overrides,
});
describe("loadConfig", () => {
it("uses protocol defaults and binds every interface", () => {
const config = loadConfig({}, { interfaces: {} });
expect(DEFAULT_HOST).toBe("0.0.0.0");
expect(config.host).toBe("0.0.0.0");
expect(config.port).toBe(7676);
expect(config.publicUrl).toBeNull();
expect(config.staticDir).toBe(join(repoRoot, "web", "dist"));
expect(config.corsOrigins).toEqual(LOOPBACK(7676));
expect(config.limits.maxBodyBytes).toBe(8 * 1024 * 1024);
expect(config.limits.maxFrameBytes).toBe(8 * 1024 * 1024);
expect(config.limits.maxConnections).toBe(64);
expect(config.tokenPath).toMatch(/d\.sock\.token$/);
expect(config.deviceStorePath).toMatch(/devices\.json$/);
});
it("honours environment overrides; PI2_ORIGINS is additive", () => {
const config = loadConfig(
{
PI2_PORT: "0",
PI2_HOST: "127.0.0.1",
PI2_STATIC_DIR: "/srv/pi2/web",
PI2_ORIGINS: "http://a.test, http://b.test ,,",
},
{ interfaces: {} },
);
expect(config.port).toBe(0);
expect(config.host).toBe("127.0.0.1");
expect(config.staticDir).toBe("/srv/pi2/web");
// Loopback origins stay in place; the extras are appended, normalized,
// and empty entries dropped.
expect(config.corsOrigins).toEqual([...LOOPBACK(0), "http://a.test", "http://b.test"]);
});
it("rejects invalid ports", () => {
expect(() => loadConfig({ PI2_PORT: "-1" })).toThrow(/PI2_PORT/);
expect(() => loadConfig({ PI2_PORT: "abc" })).toThrow(/PI2_PORT/);
expect(() => loadConfig({ PI2_PORT: "70000" })).toThrow(/PI2_PORT/);
});
it("allows every local IPv4 origin at the bound port", () => {
const config = loadConfig(
{ PI2_PORT: "8080" },
{
interfaces: {
lo: [ipv4("127.0.0.1", { internal: true })],
wlan0: [ipv4("192.168.11.7"), ipv4("169.254.10.1")],
tailscale0: [ipv4("100.64.0.14")],
},
},
);
// Loopback aliases plus each reachable address; link-local is skipped.
for (const origin of [...LOOPBACK(8080), "http://192.168.11.7:8080", "http://100.64.0.14:8080"]) {
expect(config.corsOrigins).toContain(origin);
}
expect(config.corsOrigins).toHaveLength(LOOPBACK(8080).length + 2);
});
it("narrows the bind on PI2_HOST", () => {
const config = loadConfig({ PI2_HOST: "127.0.0.1" }, { interfaces: { wlan0: [ipv4("192.168.11.7")] } });
expect(config.host).toBe("127.0.0.1");
// A loopback-only daemon is unreachable from the LAN, so the allowlist
// must not advertise an address nothing is listening on.
expect(config.corsOrigins).toEqual(LOOPBACK(7676));
});
it("adds the public URL origin and keeps loopback", () => {
const config = loadConfig({ PI2_PUBLIC_URL: "https://myhost.tail1234.ts.net/" }, { interfaces: {} });
expect(config.publicUrl).toBe("https://myhost.tail1234.ts.net");
expect(config.corsOrigins).toContain("https://myhost.tail1234.ts.net");
for (const origin of LOOPBACK(7676)) expect(config.corsOrigins).toContain(origin);
});
it("normalizes and de-duplicates explicit origins", () => {
const config = loadConfig(
{ PI2_ORIGINS: "https://a.test/, http://127.0.0.1:7676, ,http://a.test" },
{ interfaces: {} },
);
expect(config.corsOrigins.filter((origin) => origin.endsWith("a.test"))).toEqual([
"https://a.test",
"http://a.test",
]);
expect(config.corsOrigins.filter((origin) => origin === "http://127.0.0.1:7676")).toHaveLength(1);
});
it("derives only loopback origins for link-local addresses", () => {
expect(
localOrigins({ port: 7676, bindHost: "0.0.0.0", interfaces: { wlan0: [ipv4("169.254.10.1")] } }),
).toEqual(LOOPBACK(7676));
});
});