feat(web): zero-input pairing, a real QR, and no scanner or scopes
The console side of ADR 0001: a scanned link pairs itself, the status page
draws the daemon's actual symbol, and the second entry point for pairing
(the in-page camera scanner) is gone.
- `POST /api/v1/pair/redeem` replaced the dual-purpose `/api/v1/pair`; the
redeem response is parsed strictly as `{token, device}` (the five-layer
alias fallbacks for `deviceToken`/`payload.device`/… are deleted).
- `features/status/qr.ts` no longer invents "a deterministic fake QR from a
seed" — it renders `pairCode.qr.rows` module by module (devicePixelRatio
aware, square, token colours, redraws on theme/resize), and a mint without a
matrix shows the link instead of a pattern that will not scan.
- `/pair/<CODE>` (daemon 302 or the SPA route) lands on
`/connect?code=…&auto=1`, which redeems against its own origin with the
localized device name and then navigates to `next`/`/workspaces`, dropping
the code from the URL; a rejected code falls back to the prefilled form.
- The camera scanner (`features/connect/scan-dialog.tsx`) and its i18n keys are
removed: phones scan with the system camera, desktops type the code. Paste a
`pi2://pair?…` link or `/pair/CODE` still fills the form.
- Scope tracking disappears from the store, API types, device list and pairing
metadata (`pi2-device-scopes` is no longer written or read).
- Stale default removed: link parsing falls back to the served origin's port,
then 443/80, instead of a hard-coded 4321.
- `scripts/ui-{fixtures,mock-server,shots}.mjs` follow the new contract
(mint carries `qr`/`bases`, redeem route, no scopes), so the visual harness
exercises the real flow.
Verification: web typecheck clean, 299/299 tests, `vite build` green, visual
matrix 24/24 clean, and the i18n catalog has no missing keys.
This commit is contained in:
1 parent
a57c874095
commit
3e35fb7ef3
20 files changed
+419
-946
No files matched your search
@@ -4,6 +4,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { commandCalls, mockApi, type MockRoute } from "./helpers/api-mock";
|
||||
import { renderPage } from "./helpers/render-page";
|
||||
|
||||
import { drawQr } from "@/features/status/qr";
|
||||
import { setLanguage } from "@/lib/i18n";
|
||||
import { StatusPage } from "@/pages/status";
|
||||
import { useConnectionStore } from "@/stores/connection";
|
||||
@@ -74,12 +75,19 @@ const modelsBody = {
|
||||
],
|
||||
};
|
||||
|
||||
/** A 21×21 byte-mode matrix (`rows` are `"0"`/`"1"`, no quiet zone). */
|
||||
const qrRows = Array.from({ length: 21 }, (_, y) =>
|
||||
Array.from({ length: 21 }, (_, x) => ((x * 7 + y * 3 + (x ^ y)) % 5 < 2 ? "1" : "0")).join(""),
|
||||
);
|
||||
|
||||
const pairBody = {
|
||||
accepted: true,
|
||||
code: "ABCD1234",
|
||||
url: "http://127.0.0.1:4321/pair/ABCD1234",
|
||||
expiresAt: Date.now() + 9 * 60_000,
|
||||
nonce: "nonce-1",
|
||||
qr: { version: 1, ecc: "M", mask: 3, size: 21, rows: qrRows },
|
||||
bases: [{ kind: "lan", name: "eth0", address: "192.168.2.10:4321", url: "http://192.168.2.10:4321" }],
|
||||
};
|
||||
|
||||
const tasksBody = {
|
||||
@@ -107,7 +115,6 @@ const devicesBody = [
|
||||
id: "dev-1",
|
||||
name: "Pixel",
|
||||
kind: "phone",
|
||||
scopes: ["read", "steer"],
|
||||
createdAt: Date.now() - 90_000,
|
||||
lastSeenAt: Date.now() - 60_000,
|
||||
revokedAt: null,
|
||||
@@ -177,6 +184,48 @@ afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("drawQr", () => {
|
||||
function fakeCanvas(width: number) {
|
||||
const fills: number[][] = [];
|
||||
const context = {
|
||||
fillStyle: "",
|
||||
fillRect: (...args: number[]) => fills.push(args),
|
||||
clearRect: () => undefined,
|
||||
};
|
||||
const canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
clientWidth: width,
|
||||
parentElement: null,
|
||||
getContext: () => context,
|
||||
};
|
||||
return { canvas, fills };
|
||||
}
|
||||
|
||||
it("paints one square per dark module, capped at 200px and scaled by dpr", () => {
|
||||
const { canvas, fills } = fakeCanvas(480);
|
||||
drawQr(canvas as unknown as HTMLCanvasElement, qrRows);
|
||||
|
||||
const dpr = window.devicePixelRatio || 1;
|
||||
expect(canvas.width).toBe(200 * dpr);
|
||||
expect(canvas.height).toBe(200 * dpr);
|
||||
// background + one fillRect per `1` module (no fake pattern).
|
||||
const dark = qrRows.reduce((total, row) => total + [...row].filter((cell) => cell === "1").length, 0);
|
||||
expect(fills).toHaveLength(dark + 1);
|
||||
});
|
||||
|
||||
it("clears the canvas for an empty or ragged matrix", () => {
|
||||
const { canvas, fills } = fakeCanvas(200);
|
||||
drawQr(canvas as unknown as HTMLCanvasElement, []);
|
||||
expect(fills).toHaveLength(0);
|
||||
expect(canvas.width).toBe(0);
|
||||
|
||||
drawQr(canvas as unknown as HTMLCanvasElement, ["101", "10"]);
|
||||
expect(fills).toHaveLength(0);
|
||||
expect(canvas.width).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("StatusPage", () => {
|
||||
it("renders the four metric tiles, one count line and no primary button", async () => {
|
||||
mockApi(apiRoutes());
|
||||
@@ -275,12 +324,15 @@ describe("StatusPage", () => {
|
||||
await waitFor(() => expect(screen.getByTestId("ws-status")).toHaveTextContent("Connected"));
|
||||
});
|
||||
|
||||
it("mints a pairing code and shows it with a square QR and the expiry hint", async () => {
|
||||
it("mints a pairing code and renders the daemon's QR matrix with the expiry hint", async () => {
|
||||
const fetchMock = mockApi(apiRoutes());
|
||||
await renderPage(routes(), "/status", { paired: true });
|
||||
|
||||
expect(await screen.findByTestId("pairing-code")).toHaveTextContent("ABCD1234");
|
||||
expect(screen.getByRole("img", { name: "Pairing QR code" })).toHaveClass("qr-canvas");
|
||||
const canvas = screen.getByRole("img", { name: "Pairing QR code" });
|
||||
expect(canvas).toHaveClass("qr-canvas");
|
||||
// 真矩阵:模块数就是 daemon 下发的 `qr.size` / `rows.length`,不是种子图案。
|
||||
expect(canvas).toHaveAttribute("data-qr-size", String(qrRows.length));
|
||||
expect(screen.getByText("Expires in 9 min")).toBeInTheDocument();
|
||||
expect(screen.getByText(/Pairing code expires in \d+ minutes/)).toBeInTheDocument();
|
||||
|
||||
@@ -293,6 +345,26 @@ describe("StatusPage", () => {
|
||||
expect(calls[0]?.body).toEqual({ ttlSec: 600 });
|
||||
});
|
||||
|
||||
it("shows the pairing URL instead of a QR when the daemon omits the matrix", async () => {
|
||||
mockApi(
|
||||
apiRoutes([
|
||||
{
|
||||
method: "POST",
|
||||
path: "/api/v1/pair",
|
||||
handler: () => {
|
||||
const { qr: _qr, ...withoutQr } = pairBody;
|
||||
return { body: withoutQr };
|
||||
},
|
||||
},
|
||||
]),
|
||||
);
|
||||
await renderPage(routes(), "/status", { paired: true });
|
||||
|
||||
expect(await screen.findByTestId("pairing-code")).toHaveTextContent("ABCD1234");
|
||||
expect(screen.queryByRole("img", { name: "Pairing QR code" })).toBeNull();
|
||||
expect(screen.getByTestId("pair-url")).toHaveTextContent("http://127.0.0.1:4321/pair/ABCD1234");
|
||||
});
|
||||
|
||||
it("offers a retry when minting a pairing code fails", async () => {
|
||||
let attempts = 0;
|
||||
mockApi(
|
||||
|
||||
Reference in new issue
Block a user