/// import {afterEach, beforeEach, describe, expect, setSystemTime, test} from "bun:test" import {createHash} from "node:crypto" import {mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync} from "fs" import {tmpdir} from "os" import {join} from "path" import JSZip from "jszip" import { buildProjectSnapshot, buildProjectZip, classifyWatchEvent, listProjectFiles, reloadSignalsForBatch, startDevSidecar, } from "./dev-server" describe("listProjectFiles — dist/ inclusion", () => { let root: string beforeEach(() => { root = mkdtempSync(join(tmpdir(), "mentra-dev-test-")) }) afterEach(() => { rmSync(root, {recursive: true, force: true}) }) function touch(rel: string, contents = "// stub") { const abs = join(root, rel) mkdirSync(join(abs, ".."), {recursive: true}) writeFileSync(abs, contents) } // Convenience: the zip-internal entry names the function produces. const entries = (root: string) => listProjectFiles(root).map((f) => f.entry) test("ships both build bundles with the dist/ prefix stripped off the entry names", () => { touch('miniapp.json', '{"packageName":"com.test","version":"1.0.0","name":"Test"}') touch("dist/background/index.js") touch("dist/ui/index.html") touch("dist/ui/main.js") touch("src/background/index.ts") const listed = listProjectFiles(root) // disk paths keep dist/ (that's where the files live)… expect(listed.map((f) => f.disk)).toContain("dist/background/index.js") // …but zip entry names drop it, matching miniapp.json's entry.* paths. expect(entries(root)).toContain("background/index.js") expect(entries(root)).toContain("ui/index.html") expect(entries(root)).toContain("ui/main.js") expect(entries(root)).toContain("miniapp.json") expect(entries(root)).not.toContain("dist/background/index.js") }) test("strict allowlist: source + build config + node_modules + public are NOT shipped", () => { touch('miniapp.json', '{"packageName":"com.test","version":"1.0.0","name":"Test"}') touch("dist/background/index.js") touch("src/background/index.ts") touch("package.json") touch("tsconfig.json") touch("build.ts") touch("bunfig.toml") touch("node_modules/x/index.js") touch("public/fonts/big.ttf") const listed = entries(root) expect(listed.some((p) => p.startsWith("src/"))).toBe(false) expect(listed.some((p) => p.startsWith("public/"))).toBe(false) expect(listed.some((p) => p.startsWith("node_modules/"))).toBe(false) expect(listed).not.toContain("package.json") expect(listed).not.toContain("tsconfig.json") expect(listed).not.toContain("build.ts") expect(listed).not.toContain("bunfig.toml") expect(listed).toContain("miniapp.json") expect(listed).toContain("background/index.js") }) test("ships the manifest-declared icon and skips unrelated images", () => { touch('miniapp.json', '{"packageName":"com.test","version":"1.0.0","name":"Test","icon":"my-icon.png"}') touch("my-icon.png") touch("other-image.png") touch("dist/background/index.js") const listed = entries(root) expect(listed).toContain("my-icon.png") expect(listed).not.toContain("other-image.png") }) test("defaults to icon.png when manifest does not declare an icon", () => { touch('miniapp.json', '{"packageName":"com.test","version":"1.0.0","name":"Test"}') touch("icon.png") touch("dist/background/index.js") const listed = entries(root) expect(listed).toContain("icon.png") }) test("skips icon if it is missing from disk (manifest may be stale)", () => { touch('miniapp.json', '{"packageName":"com.test","version":"1.0.0","name":"Test","icon":"icon.png"}') touch("dist/background/index.js") const listed = entries(root) expect(listed).not.toContain("icon.png") }) }) describe("buildProjectZip — two-output bundle contract", () => { let root: string beforeEach(() => { root = mkdtempSync(join(tmpdir(), "mentra-zip-test-")) }) afterEach(() => { rmSync(root, {recursive: true, force: true}) }) function touch(rel: string, contents = "// stub") { const abs = join(root, rel) mkdirSync(join(abs, ".."), {recursive: true}) writeFileSync(abs, contents) } test("zip ships both bundles at the root (dist/ prefix stripped), manifest at root", async () => { touch("miniapp.json", '{"packageName":"com.test","version":"1.0.0","name":"Test","hardwareRequirements":[]}') touch("dist/background/index.js", "console.log('bg')") touch("dist/ui/index.html", "") touch("dist/ui/main.js", "console.log('ui')") const buf = await buildProjectZip(root) const zip = await JSZip.loadAsync(buf) expect(zip.files["miniapp.json"]).toBeDefined() expect(zip.files["background/index.js"]).toBeDefined() expect(zip.files["ui/index.html"]).toBeDefined() expect(zip.files["ui/main.js"]).toBeDefined() // The on-disk dist/ prefix must not leak into the zip layout. expect(zip.files["dist/background/index.js"]).toBeUndefined() }) test("zip preserves file contents byte-for-byte", async () => { touch("miniapp.json", '{"a":1}') touch("dist/background/index.js", "/* bg-stub */") const buf = await buildProjectZip(root) const zip = await JSZip.loadAsync(buf) const manifest = await zip.files["miniapp.json"]!.async("string") const bg = await zip.files["background/index.js"]!.async("string") expect(manifest).toBe('{"a":1}') expect(bg).toBe("/* bg-stub */") }) }) describe("buildProjectSnapshot: the ZIP the served miniapp.json names", () => { let root: string beforeEach(() => { root = mkdtempSync(join(tmpdir(), "mentra-snapshot-test-")) }) afterEach(() => { setSystemTime() rmSync(root, {recursive: true, force: true}) }) function touch(rel: string, contents = "// stub") { const abs = join(root, rel) mkdirSync(join(abs, ".."), {recursive: true}) writeFileSync(abs, contents) } const manifest = '{"packageName":"com.test","version":"1.0.0","name":"Test","hardwareRequirements":[]}' test("names the ZIP's SHA-256 and the miniapp.json inside it", async () => { touch("miniapp.json", manifest) touch("dist/background/index.js", "console.log('bg')") const snapshot = await buildProjectSnapshot(root) expect(snapshot.sha256).toBe(createHash("sha256").update(snapshot.zip).digest("hex")) expect(snapshot.manifest).toBe(manifest) const zip = await JSZip.loadAsync(snapshot.zip) expect(await zip.files["miniapp.json"]!.async("string")).toBe(manifest) }) test("the same files make the same ZIP, whenever it is built", async () => { touch("miniapp.json", manifest) touch("dist/background/index.js", "console.log('bg')") touch("dist/ui/assets/main.js", "console.log('ui')") setSystemTime(new Date("2026-01-01T10:00:00Z")) const first = await buildProjectSnapshot(root) setSystemTime(new Date("2026-06-01T18:30:07Z")) const second = await buildProjectSnapshot(root) expect(second.sha256).toBe(first.sha256) expect(Buffer.from(second.zip).equals(Buffer.from(first.zip))).toBe(true) expect(await buildProjectZip(root)).toEqual(second.zip) }) test("changed files make a different ZIP", async () => { touch("miniapp.json", manifest) touch("dist/background/index.js", "console.log('bg')") const before = await buildProjectSnapshot(root) touch("dist/background/index.js", "console.log('bg 2')") expect((await buildProjectSnapshot(root)).sha256).not.toBe(before.sha256) }) test("a project without miniapp.json has no manifest", async () => { touch("dist/background/index.js") expect((await buildProjectSnapshot(root)).manifest).toBeNull() }) }) describe("classifyWatchEvent: which layer a watcher event touched", () => { test("POSIX paths: background respawns, UI and other files reload", () => { expect(classifyWatchEvent("src/background/index.ts", "/")).toBe("respawn-bg") expect(classifyWatchEvent("packages/app/src/background/index.ts", "/")).toBe("respawn-bg") expect(classifyWatchEvent("src/ui/App.tsx", "/")).toBe("reload") expect(classifyWatchEvent("miniapp.json", "/")).toBe("reload") }) test("POSIX paths: build output, dependencies and bare directory events are ignored", () => { expect(classifyWatchEvent(".", "/")).toBe("ignore") expect(classifyWatchEvent("dist", "/")).toBe("ignore") expect(classifyWatchEvent("dist/background/index.js", "/")).toBe("ignore") expect(classifyWatchEvent("node_modules/jszip/index.js", "/")).toBe("ignore") expect(classifyWatchEvent(".git/index", "/")).toBe("ignore") expect(classifyWatchEvent("build/app.zip", "/")).toBe("ignore") expect(classifyWatchEvent(".next/cache", "/")).toBe("ignore") }) // Windows fs.watch reports paths with backslashes (Mentra-Community/MentraOS#3888). test("Windows paths: background respawns, UI and other files reload", () => { expect(classifyWatchEvent("src\\background\\index.ts", "\\")).toBe("respawn-bg") expect(classifyWatchEvent("packages\\app\\src\\background\\index.ts", "\\")).toBe("respawn-bg") expect(classifyWatchEvent("src\\ui\\App.tsx", "\\")).toBe("reload") }) test("Windows paths: build output and dependencies are ignored", () => { expect(classifyWatchEvent("dist\\background\\index.js", "\\")).toBe("ignore") expect(classifyWatchEvent("node_modules\\jszip\\index.js", "\\")).toBe("ignore") expect(classifyWatchEvent(".git\\index", "\\")).toBe("ignore") }) test("a backslash is left alone on platforms where it is not the separator", () => { expect(classifyWatchEvent("src\\background\\index.ts", "/")).toBe("reload") }) }) describe("classifyWatchEvent: shared code", () => { test("src/shared/ is imported by both layers, so it needs both", () => { expect(classifyWatchEvent("src/shared/channels.ts", "/")).toBe("both") expect(classifyWatchEvent("packages/app/src/shared/channels.ts", "/")).toBe("both") expect(classifyWatchEvent("src\\shared\\channels.ts", "\\")).toBe("both") }) }) describe("reloadSignalsForBatch: what one save sends the phone", () => { test("one layer sends its own signal", () => { expect(reloadSignalsForBatch(["reload", "reload"])).toEqual(["reload"]) expect(reloadSignalsForBatch(["respawn-bg"])).toEqual(["respawn-bg"]) }) test("both layers send both, the background first", () => { expect(reloadSignalsForBatch(["reload", "respawn-bg"])).toEqual(["respawn-bg", "reload"]) expect(reloadSignalsForBatch(["respawn-bg", "reload"])).toEqual(["respawn-bg", "reload"]) expect(reloadSignalsForBatch(["both"])).toEqual(["respawn-bg", "reload"]) }) test("an empty batch sends nothing", () => { expect(reloadSignalsForBatch([])).toEqual([]) }) }) describe("startDevSidecar: a save that touches both layers", () => { let dir: string let sidecar: ReturnType | null = null beforeEach(() => { dir = realpathSync(mkdtempSync(join(tmpdir(), "mentra-dev-sidecar-"))) mkdirSync(join(dir, "src", "ui"), {recursive: true}) mkdirSync(join(dir, "src", "background"), {recursive: true}) }) afterEach(() => { sidecar?.stop() sidecar = null rmSync(dir, {recursive: true, force: true}) }) test("respawns the background and reloads the screen", async () => { const port = 47_000 + Math.floor(Math.random() * 2_000) sidecar = startDevSidecar({port, watchDir: dir, silent: true}) const received: string[] = [] const ws = new WebSocket(`ws://127.0.0.1:${port}/__mentra_dev`) await new Promise((resolve, reject) => { ws.onopen = () => resolve() ws.onerror = () => reject(new Error("sidecar socket failed")) }) ws.onmessage = (event) => received.push(JSON.parse(String(event.data)).type) // Let the watcher settle before the save. await Bun.sleep(1_000) writeFileSync(join(dir, "src", "ui", "App.tsx"), "export const ui = 2\n") writeFileSync(join(dir, "src", "background", "index.ts"), "export const bg = 2\n") const deadline = Date.now() + 10_000 const both = () => received.includes("respawn-bg") && received.includes("reload") while (!both() && Date.now() < deadline) await Bun.sleep(50) ws.close() // One batch sends ["respawn-bg", "reload"]; on a slow machine the two writes can land in separate batches. Either // way the screen reloads, which a lone respawn-bg never did. expect([...new Set(received)].sort()).toEqual(["reload", "respawn-bg"]) }, 20_000) })