kernel / core / lib / src / cli / device-login / qr.test.tsmain at 949894f
import { encode } from "uqr";
import { expect, test } from "vitest";
import { terminalQrCode } from "./qr.ts";
// synthetic: no mind answers at this address, and the code is no code of a sign-in
const LINK = "https://iterate.example.com/device?user_code=BCDF-GHJK";
/** The dark modules that a camera sees in a code `drawn` with colors: a row for each half line,
* read from the background that the escape codes set and from the black half blocks on it. */
function modulesOf(drawn: string) {
const rows: boolean[][] = [];
for (const line of drawn.split("\n")) {
const top: boolean[] = [];
const bottom: boolean[] = [];
let blackBackground = false;
for (const [, code, char] of line.slice(2).matchAll(/\u001b\[([\d;]*)m|(.)/gu)) {
if (code !== undefined) blackBackground = code === "40";
else {
top.push(blackBackground || char === "▀");
bottom.push(blackBackground || char === "▄");
}
}
rows.push(top, bottom);
}
return rows;
}
test("with colors, a character is one module wide and two high, and the modules are the code's", () => {
const { data, size } = encode(LINK, { ecc: "L", border: 2 });
const drawn = terminalQrCode(LINK, true)!;
const lines = drawn.split("\n");
expect(lines).toHaveLength(Math.ceil(size / 2));
// every line ends with a reset, and is the indent and one character for each module
for (const line of lines) {
expect(line.endsWith("\u001b[0m")).toBe(true);
expect(line.replace(/\u001b\[[\d;]*m/g, "")).toHaveLength(2 + size);
}
// the half line under an odd code is light
const expected = [...data, ...(size % 2 ? [Array<boolean>(size).fill(false)] : [])];
expect(modulesOf(drawn)).toEqual(expected);
});
// what the sign-in prints under the code (index.ts `deviceLogin`)
const UNDER = ["", ` ${LINK}`, "", "The code expires in 5 minutes. Waiting for approval..."];
// 19 lines of code with colors (21 without, whose quiet zone is four modules), then the lines
// under it and the cursor's line: 5 rows when no line wraps, 7 when the link and the last line do
test.for([
{ colors: true, columns: 80, rows: 24, fits: true },
{ colors: true, columns: 40, rows: 26, fits: true },
{ colors: true, columns: 40, rows: 25, fits: false },
{ colors: true, columns: 38, rows: 50, fits: false },
{ colors: false, columns: 80, rows: 26, fits: true },
{ colors: false, columns: 44, rows: 28, fits: true },
{ colors: false, columns: 44, rows: 27, fits: false },
{ colors: false, columns: 42, rows: 50, fits: false },
])("$columns columns and $rows rows, colors $colors: fits $fits", (terminal) => {
const drawn = terminalQrCode(LINK, terminal.colors, terminal, UNDER);
if (!terminal.fits) return expect(drawn).toBeNull();
const lines = drawn!.replace(/\u001b\[[\d;]*m/g, "").split("\n");
expect(Math.max(...lines.map((line) => line.length))).toBeLessThanOrEqual(terminal.columns);
});
test("a stream that is no terminal has no size, and gets the code", () => {
expect(terminalQrCode(LINK, false, { columns: undefined, rows: undefined })).toContain("█");
});