diff --git a/apps/mobile/preview/device-lab.test.mjs b/apps/mobile/preview/device-lab.test.mjs new file mode 100644 index 0000000..204f99b --- /dev/null +++ b/apps/mobile/preview/device-lab.test.mjs @@ -0,0 +1,55 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { join, dirname } from "node:path"; +import { fileURLToPath } from "node:url"; + +// The Device lab screen (HM-SET-10) must render in the preview harness: its route is listed, and every name the screen +// imports from the adapters is one the one stub file exports. Nothing here runs the screen or touches a device. +const here = dirname(fileURLToPath(import.meta.url)); +const read = (path) => readFileSync(join(here, path), "utf8"); + +test("the Device lab is a preview route", () => { + assert.match(read("src/routes.ts"), /import S_device_lab from "\.\.\/\.\.\/src\/app\/device-lab";/); + assert.match(read("src/routes.ts"), /"device-lab": S_device_lab,/); +}); + +test("the stub adapters export every adapter name the Device lab screen imports", () => { + const screen = read("../src/app/device-lab.tsx"); + const exported = new Set([...read("src/stubs/adapters.ts").matchAll(/export\s+(?:async\s+)?(?:const|let|function|class)\s+(\w+)/g)].map((m) => m[1])); + const fromAdapters = /from\s*"\.\.\/adapters(?:\/[\w-]+)?";/; + // Only named imports can be checked name by name; a namespace or default import would hide what the screen uses. + const lines = screen.split("\n").filter((line) => fromAdapters.test(line)); + for (const line of lines) assert.match(line, /^import\s*(?:type\s*)?\{[^}]*\}\s*from/, `named imports only: ${line}`); + const imported = [...screen.matchAll(/import\s*(type\s*)?\{([^}]*)\}\s*from\s*"\.\.\/adapters(?:\/[\w-]+)?";/g)] + .filter((m) => !m[1]) + .flatMap((m) => m[2].split(",")) + .map((name) => name.trim()) + .filter((name) => name && !name.startsWith("type ")) + .map((name) => name.split(/\s+as\s+/)[0]); + assert.ok(imported.length > 0, "the screen imports from the adapters"); + const missing = imported.filter((name) => !exported.has(name)); + assert.deepEqual(missing, []); +}); + +// The Device lab part of the stub, from its heading comment to the end of the file. +const lab = () => { + const stub = read("src/stubs/adapters.ts"); + const start = stub.indexOf("// Device lab (HM-SET-10)"); + assert.ok(start >= 0, "the stub has a Device lab section"); + return stub.slice(start); +}; + +test("the Device lab runs on a fixed clock, so its report shot is the same every day", () => { + assert.doesNotMatch(lab(), /Date\.now\(\)/); + assert.match(lab(), /LAB_CLOCK\.now/); +}); + +test("the Device lab's sleep takes the port's StopSignal", () => { + assert.match(lab(), /\(ms: number, signal: StopSignal\)/); +}); + +test("the Device lab's allowed cores come from the made-up phone, not a literal", () => { + assert.doesNotMatch(lab(), /allowedCores:\s*\d/); + assert.match(lab(), /allowedCores:\s*labHardware\.allowedCpus\.length/); +}); diff --git a/apps/mobile/preview/package.json b/apps/mobile/preview/package.json index b1802a1..30ef2fb 100644 --- a/apps/mobile/preview/package.json +++ b/apps/mobile/preview/package.json @@ -7,6 +7,7 @@ "type": "module", "scripts": { "build": "node build.mjs", + "test": "node --test device-lab.test.mjs browser.test.mjs", "shoot": "node shoot.mjs", "compress": "python3 -c \"import glob; from PIL import Image; [Image.open(f).convert('RGB').quantize(colors=256, dither=Image.Dither.NONE).save(f, optimize=True) for f in glob.glob('out/screens/*.png')]\"", "clips": "node clips.mjs", diff --git a/apps/mobile/preview/shots.mjs b/apps/mobile/preview/shots.mjs index bab1d91..0660195 100644 --- a/apps/mobile/preview/shots.mjs +++ b/apps/mobile/preview/shots.mjs @@ -148,6 +148,15 @@ export const shots = [ core({ id: "HM-WEB-02", name: "Web task, the plan", route: "browser", variant: "plan", params: { recipe: "farmacia" }, mockup: "web-plan" }), core({ id: "HM-WEB-02", name: "Web task, running", route: "browser", variant: "running", params: { recipe: "farmacia" }, act: startTask }), core({ id: "HM-SET-01", name: "Settings", route: "settings", mockup: "settings-root" }), + core({ id: "HM-SET-10", name: "HM-SET-10 Device lab, ready", route: "device-lab", file: "device-lab-ready" }), + core({ id: "HM-SET-10", name: "HM-SET-10 Device lab, battery saver on", route: "device-lab", variant: "battery", file: "device-lab-battery" }), + core({ id: "HM-SET-10", name: "HM-SET-10 Device lab, no model yet", route: "device-lab", variant: "fresh", file: "device-lab-no-model" }), + core({ id: "HM-SET-10", name: "HM-SET-10 Device lab, a run in progress", route: "device-lab", variant: "running", file: "device-lab-running", act: tap(/^(Empezar la prueba|Start the lab)$/) }), + core({ id: "HM-SET-10", name: "HM-SET-10 Device lab, the finished report", route: "device-lab", file: "device-lab-report", act: async (page) => { + await tap(/^(Empezar la prueba|Start the lab)$/)(page); + await page.getByText(/^(Tu informe|Your report)$/).first().waitFor({ timeout: 30000 }); + await page.waitForTimeout(500); + } }), core({ id: "HM-SET-09", name: "What Hermit can use (Q147)", route: "use", mockup: "settings-use" }), core({ id: "HM-VAULT-01", name: "Vault, unlocked", route: "vault" }), core({ id: "HM-SET-02", name: "Privacy", route: "privacy", mockup: "settings-privacy" }), diff --git a/apps/mobile/preview/src/routes.ts b/apps/mobile/preview/src/routes.ts index 6f2d06c..4094cdd 100644 --- a/apps/mobile/preview/src/routes.ts +++ b/apps/mobile/preview/src/routes.ts @@ -13,6 +13,7 @@ import S_calendar from "../../src/app/calendar"; import S_chat from "../../src/app/chat"; import S_connect from "../../src/app/connect"; import S_delete from "../../src/app/delete"; +import S_device_lab from "../../src/app/device-lab"; import S_download from "../../src/app/download"; import S_eval from "../../src/app/eval"; import S_fill from "../../src/app/fill"; @@ -67,6 +68,7 @@ export const routes = { "chat": S_chat, "connect": S_connect, "delete": S_delete, + "device-lab": S_device_lab, "download": S_download, "eval": S_eval, "fill": S_fill, diff --git a/apps/mobile/preview/src/stubs/adapters.ts b/apps/mobile/preview/src/stubs/adapters.ts index 2d8c2a3..358b3f2 100644 --- a/apps/mobile/preview/src/stubs/adapters.ts +++ b/apps/mobile/preview/src/stubs/adapters.ts @@ -1,5 +1,8 @@ // Preview only: stands in for apps/mobile/src/adapters/* (native modules, Keystore, QVAC, the phone's settings). // Every function answers with what a healthy Galaxy S26 on Wi-Fi would report; nothing reaches a device. +import type { StopSignal } from "@hermit/core"; +import { LAB_CLOCK, type LabEngine, type LabPorts, type PhoneFacts } from "@hermit/eval"; +import { initialPlan, PROFILES, profileForPlan, type HardwareFacts } from "@hermit/model"; import { permissionRow, remindersRow } from "@hermit/runtime"; import { scene } from "../scene"; @@ -155,3 +158,92 @@ export const copyLogValue = ok(true); export const walletBundleAvailable = () => false; export const appVersion = () => "0.1.0-preview.1"; export const startWalletWorklet = () => ({}); + +// Device lab (HM-SET-10): a made-up phone, named after the Examplecorp EX-1000 of +// scripts/devices/fixtures/good-report.json (its plan comes from initialPlan, so it differs from that fixture's). +// The engine answers at once, so a finished run can be shot; scene.variant "running" keeps one call open for the +// shot of a run in progress, and "battery" turns battery saver on. The clock starts at LAB_CLOCK and steps a set +// amount on each read, so the report's date and times are the same on every run. +const labHardware: HardwareFacts = { + platform: "android", + model: "EX-1000", + soc: "XC9000", + totalRamBytes: 7.5 * 1024 ** 3, + isLowRamDevice: false, + clusters: [ + { cpus: [0, 1, 2, 3], maxFreqKHz: 2_000_000 }, + { cpus: [4, 5, 6], maxFreqKHz: 2_600_000 }, + { cpus: [7], maxFreqKHz: 3_100_000 }, + ], + logicalCores: 8, + allowedCpus: [0, 1, 2, 3, 4, 5, 6, 7], + cpuFeatures: { dotprod: true, i8mm: true, sve2: false, sme2: false }, +}; +const labFacts: PhoneFacts = { + manufacturer: "Examplecorp", + model: "EX-1000", + socManufacturer: "Examplechip", + socModel: "XC9000", + totalRamBytes: labHardware.totalRamBytes, + lowRam: false, + androidRelease: "15", + apiLevel: 35, + abis: ["arm64-v8a"], + logicalCores: labHardware.logicalCores, + clusters: labHardware.clusters, + cpuFeatures: labHardware.cpuFeatures, + allowedCpus: labHardware.allowedCpus, + cpuset: "0-7", + batterySaver: scene.variant === "battery", +}; + +export const inTurn = () => false; +export const batterySaverOn = () => scene.variant === "battery"; +export const copyReport = ok(undefined); +export const keepScreenOn = ok(undefined); +export const openReportForm = ok(undefined); +export const shareReport = ok(undefined); + +export async function labSetup() { + const plan = initialPlan(labHardware); + return { profile: profileForPlan(PROFILES[0]!, plan), plan, allowedCores: labHardware.allowedCpus.length, facts: labFacts }; +} + +/** Waits `ms`, ending early when `signal` fires: the same contract as the phone's port, so the lab's caps really wait. */ +const waitFor = (ms: number, signal: StopSignal) => + new Promise((resolve) => { + if (signal.aborted) return resolve(); + const done = () => { + clearTimeout(timer); + signal.removeEventListener("abort", done); + resolve(); + }; + const timer = setTimeout(done, ms); + signal.addEventListener("abort", done, { once: true }); + }); + +const labEngine = (): LabEngine => ({ + complete: async (req) => { + const { signal } = req; + if (scene.variant === "running") { + await new Promise((_, reject) => signal?.addEventListener("abort", () => reject(new Error("stopped")), { once: true })); + } + for (let i = 0; i < 3; i++) req.onToken?.("…"); + await (signal ? waitFor(40, signal) : new Promise((resolve) => setTimeout(resolve, 40))); + return { text: "Listo.", toolCalls: [], stats: { tokensPerSecond: 42 } }; + }, + close: async () => {}, +}); + +/** How far the made-up clock moves on each read. */ +const LAB_STEP_MS = 250; + +export function labPorts(_profile: unknown): LabPorts { + let clock = LAB_CLOCK.now; + return { + load: async () => labEngine(), + thermal: () => 0, + sleep: waitFor, + now: () => (clock += LAB_STEP_MS), + }; +}