test(e2e): cover the console login handoff (A13)

This commit is contained in:
yuez committed 2026-10-23 08:34:48 +08:00
1 parent 51ef1c250b
commit 84869f3dc6
2 files changed
+81 -4

No files matched your search

+12 -3
View File
@@ -28,7 +28,7 @@ the real daemon/plugin/browser, never `MOCK.*` data.
The v1 gate is green when: The v1 gate is green when:
- **A0–A12** (API/WS against a real headless nvim + real pi) pass. - **A0–A13** (API/WS against a real headless nvim + real pi) pass.
- **B1–B14** (Playwright against the daemon-served production bundle) pass. - **B1–B14** (Playwright against the daemon-served production bundle) pass.
- **C1–C4** (responsive, theme, language, no console errors) pass. - **C1–C4** (responsive, theme, language, no console errors) pass.
- `tsc --noEmit`, `vitest run` (daemon), `vitest run` (web) stay green. - `tsc --noEmit`, `vitest run` (daemon), `vitest run` (web) stay green.
@@ -110,7 +110,7 @@ then the browser suite, each in its own temp root. Both drivers are standalone w
## 4. Scenarios ## 4. Scenarios
All scenario ids below are implemented in the two drivers: `scenario("A0", …)` … `scenario("A12", …)` All scenario ids below are implemented in the two drivers: `scenario("A0", …)` … `scenario("A13", …)`
in `api-e2e.mjs`, `scenario("B1", …)` … `scenario("B14", …)` and `scenario("C1", …)` … in `api-e2e.mjs`, `scenario("B1", …)` … `scenario("B14", …)` and `scenario("C1", …)` …
`scenario("C4", …)` in `ui-e2e.mjs`. A driver exits non-zero only on **FAIL**; SKIP is `scenario("C4", …)` in `ui-e2e.mjs`. A driver exits non-zero only on **FAIL**; SKIP is
tolerant, and every skip is printed with its reason and written into the report. tolerant, and every skip is printed with its reason and written into the report.
@@ -220,6 +220,15 @@ The plan's original intent below is unchanged; the legend **P0**/**P1**/**P2** s
- reject case: another file is requested; respond `{cancelled:true}` (or Rejected); - reject case: another file is requested; respond `{cancelled:true}` (or Rejected);
the file does not exist and the turn continues/settles without error state. the file does not exist and the turn continues/settles without error state.
**A13 Console login handoff**
- `POST /api/v1/console/session` with the CLI token → 200, one `local browser`/`laptop`
device with every scope, and a `http://127.0.0.1:<port>/#t=<token>` URL (fragment only,
never `?t=`).
- the handed-off token authenticates; a second login returns the **same** token so a second
`:PiWeb` does not log the open tab out.
- no CLI token → 401, a device token → 401 (loopback-only, local-CLI-only).
- revoking the device → 204, the old token → 401, and the next login mints a new token.
**A12 Errors** **A12 Errors**
- unknown session id → `E_SESSION_NOT_FOUND`; malformed `cmd` → `E_BAD_REQUEST`; - unknown session id → `E_SESSION_NOT_FOUND`; malformed `cmd` → `E_BAD_REQUEST`;
unknown method → `E_METHOD_NOT_ALLOWED`; oversized/malformed JSON does not kill the unknown method → `E_METHOD_NOT_ALLOWED`; oversized/malformed JSON does not kill the
@@ -365,7 +374,7 @@ which were skipped and why (non-blocking only), and the command that reproduces
```bash ```bash
cd <repo> # the pi2 CLI/daemon checkout (this repository) cd <repo> # the pi2 CLI/daemon checkout (this repository)
# backend only (A0–A12): tsc, then `pi2 daemon` + headless nvim + real pi # backend only (A0–A13): tsc, then `pi2 daemon` + headless nvim + real pi
bash scripts/e2e/run-api.sh bash scripts/e2e/run-api.sh
# browser (B1–B14, C1–C4): also builds web/dist (pnpm), then Playwright # browser (B1–B14, C1–C4): also builds web/dist (pnpm), then Playwright
+69 -1
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
/** /**
* A0–A12 — API/WS end-to-end driver (docs/e2e.md §4.1). * A0–A13 — API/WS end-to-end driver (docs/e2e.md §4.1).
* *
* Runs against a live daemon + one managed headless nvim per workspace + a real * Runs against a live daemon + one managed headless nvim per workspace + a real
* `pi --mode rpc` child. The lifecycle (temp dirs, ephemeral port, pairing, * `pi --mode rpc` child. The lifecycle (temp dirs, ephemeral port, pairing,
@@ -2870,6 +2870,74 @@ scenario("A12", "Errors", async (ctx) => {
} }
}); });
// ---------------------------------------------------------------------------
// A13 — console login (local browser handoff)
// ---------------------------------------------------------------------------
scenario("A13", "Console login handoff", async (ctx) => {
const session = () => http("POST", "/api/v1/console/session", { token: CLI_TOKEN });
const first = await session();
ctx.eq("POST /api/v1/console/session → 200", first.status, 200);
const token = first.json?.token;
const url = typeof first.json?.url === "string" ? first.json.url : "";
const deviceId = first.json?.device?.id;
ctx.eq("the session names the local-browser device", first.json?.device?.name, "local browser");
ctx.eq("the local-browser device is a laptop", first.json?.device?.kind, "laptop");
ctx.eq("the local-browser device holds every scope", first.json?.device?.scopes, [
"read",
"steer",
"control",
"admin",
]);
ctx.check("the handoff url puts the token in the fragment", /\/#t=[A-Za-z0-9_-]{20,}$/.test(url), {
expected: "http://127.0.0.1:<port>/#t=<token>",
observed: url.replace(/#t=.{6}.*/, "#t=<redacted>"),
});
ctx.check("the handoff url never carries the token in the query string", !url.includes("?t="), {
expected: "no ?t=",
observed: url.replace(/#t=.{6}.*/, "#t=<redacted>"),
});
ctx.check("the handoff url targets loopback", url.startsWith("http://127.0.0.1:"), {
expected: "http://127.0.0.1:<port>/#t=…",
observed: url.replace(/#t=.{6}.*/, "#t=<redacted>"),
});
const used = await http("GET", "/api/v1/workspaces", { token });
ctx.eq("the handed-off token authenticates → 200", used.status, 200);
// `:PiWeb` twice must not log the tab that is already open out.
const second = await session();
ctx.eq("a second login reuses the same token", second.json?.token, token);
const stillUsed = await http("GET", "/api/v1/workspaces", { token });
ctx.eq("the first tab keeps working after a second :PiWeb", stillUsed.status, 200);
const anonymous = await http("POST", "/api/v1/console/session");
ctx.eq("no cli token → 401", anonymous.status, 401);
const asDevice = await http("POST", "/api/v1/console/session", { token });
ctx.eq("a device token cannot mint another console session", asDevice.status, 401);
const devices = await http("GET", "/api/v1/devices", { token: CLI_TOKEN });
const browsers = (Array.isArray(devices.json) ? devices.json : []).filter(
(device) => device?.name === "local browser",
);
ctx.eq("exactly one local-browser device exists", browsers.length, 1);
// Revocation drops the stored plaintext, so the next login mints a new token.
const revoked = await http("DELETE", `/api/v1/devices/${encodeURIComponent(deviceId)}`, { token: CLI_TOKEN });
ctx.eq("revoking the local browser → 204", revoked.status, 204);
const afterRevoke = await session();
ctx.check("the next login mints a new token", afterRevoke.status === 200 && afterRevoke.json?.token !== token, {
expected: "200 with a different token",
observed: { status: afterRevoke.status, sameToken: afterRevoke.json?.token === token },
});
const deadToken = await http("GET", "/api/v1/workspaces", { token });
ctx.eq("the revoked token is rejected → 401", deadToken.status, 401);
const freshToken = await http("GET", "/api/v1/workspaces", { token: afterRevoke.json?.token });
ctx.eq("the new token works → 200", freshToken.status, 200);
});
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// reporting // reporting
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------