@glassly/miniapp-cli 0.1.17-dev.0 → 0.1.17-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glassly/miniapp-cli",
3
- "version": "0.1.17-dev.0",
3
+ "version": "0.1.17-dev.1",
4
4
  "description": "Build tooling for Glassly miniapps: dev server, packer, manifest validation, and JSON Schema. The `glassly` command that drives it ships in @glassly/cli.",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -1,16 +1,17 @@
1
1
  /// <reference types="bun-types" />
2
2
 
3
3
  import {describe, expect, test} from "bun:test"
4
- import {mkdtempSync} from "fs"
4
+ import {mkdtempSync, readdirSync, readFileSync} from "fs"
5
5
  import {tmpdir} from "os"
6
6
  import {join} from "path"
7
- import {MiniappSession, type Transport} from "@glassly/miniapp"
7
+ import {MiniappSession, type RenderTransition, type Transport} from "@glassly/miniapp"
8
8
 
9
9
  import {simDevice} from "./device.js"
10
10
  import {parseScript} from "./script.js"
11
11
  import {SimHost, type SimHostEvent, type SimManifest} from "./host.js"
12
12
  import {simStateDir} from "./sim.js"
13
13
  import {SimRegistry} from "./registry.js"
14
+ import {startSimServer} from "./server.js"
14
15
  import {createSimStorage, type SimStorage} from "./storage.js"
15
16
 
16
17
  /** A real SDK session wired straight to the host, standing in for the worker bridge. */
@@ -18,6 +19,7 @@ async function connect(
18
19
  manifest: Partial<SimManifest> = {},
19
20
  device = "simulated",
20
21
  stores: {storage?: SimStorage; settings?: SimStorage; memory?: SimStorage; registry?: SimRegistry} = {},
22
+ listen: (event: SimHostEvent) => void = () => {},
21
23
  ) {
22
24
  const events: SimHostEvent[] = []
23
25
  let deliver: (raw: string) => void = () => {}
@@ -29,7 +31,10 @@ async function connect(
29
31
  memory: stores.memory,
30
32
  registry: stores.registry,
31
33
  toBackground: (raw) => queueMicrotask(() => deliver(raw)),
32
- emit: (event) => events.push(event),
34
+ emit: (event) => {
35
+ events.push(event)
36
+ listen(event)
37
+ },
33
38
  frameLatencyMs: 0,
34
39
  speechMsPerChar: 0,
35
40
  })
@@ -318,7 +323,50 @@ describe("speech", () => {
318
323
  await settle()
319
324
  host.translation("Two blocks away", {originalText: "A dos cuadras"})
320
325
  await settle()
321
- expect(heard).toEqual(["Two blocks away|utt_sim_tr_1"])
326
+ expect(heard).toHaveLength(1)
327
+ const [text, utteranceId] = heard[0].split("|")
328
+ expect(text).toBe("Two blocks away")
329
+ expect(utteranceId).toBeTruthy()
330
+ })
331
+
332
+ test("a listener for one regional source hears bare-language speech in that language", async () => {
333
+ const {session, host} = await connect({permissions: [{type: "MICROPHONE"}]})
334
+ const heard: {text: string; isFinal: boolean; transcribeLanguage?: string}[] = []
335
+ session.translation.fromTo("es-ES", "en-US", (data) => heard.push(data as (typeof heard)[number]))
336
+ await settle()
337
+ host.translation("Hello", {originalText: "Hola"})
338
+ await host.play(parseScript("B (es): Hola | en: Hi there"), {wordsPerMinute: 60_000, gapMs: 0})
339
+ expect(heard.filter((data) => data.isFinal).map(({text, transcribeLanguage}) => [text, transcribeLanguage])).toEqual([
340
+ ["Hello", "es-ES"],
341
+ ["Hi there", "es-ES"],
342
+ ])
343
+ })
344
+
345
+ test("speech works with an SDK that predates the shared speech preparation", () => {
346
+ const child = `
347
+ import * as hostRules from "@glassly/miniapp/host"
348
+ import {mock} from "bun:test"
349
+ mock.module("@glassly/miniapp/host", () => ({...hostRules, prepareTtsSentences: undefined}))
350
+ const {MiniappSession} = await import("@glassly/miniapp")
351
+ const {SimHost} = await import(${JSON.stringify(join(import.meta.dir, "host.ts"))})
352
+ const {simDevice} = await import(${JSON.stringify(join(import.meta.dir, "device.ts"))})
353
+ const {createSimStorage} = await import(${JSON.stringify(join(import.meta.dir, "storage.ts"))})
354
+ let deliver = () => {}
355
+ const spoken = []
356
+ const host = new SimHost({manifest: {packageName: "com.example.sim", name: "Sim"}, device: simDevice("simulated"), storage: createSimStorage(),
357
+ toBackground: (raw) => queueMicrotask(() => deliver(raw)), frameLatencyMs: 0, speechMsPerChar: 0,
358
+ emit: (event) => event.type === "effect" && event.effect.kind === "speak" && spoken.push(event.effect.text)})
359
+ host.backgroundStarted()
360
+ const session = new MiniappSession({packageName: "com.example.sim", transport: {open: async () => {}, send: (raw) => queueMicrotask(() => host.fromBackground(raw)),
361
+ onMessage: (h) => (deliver = h), onDisconnect: () => {}, close: () => {}, isOpen: () => true}})
362
+ await session.connect()
363
+ const result = await session.speaker.speak("hello").then((r) => r.completed, (e) => e.code)
364
+ const blank = await session.speaker.speak(" ").then(() => "ok", (e) => e.code)
365
+ console.log(JSON.stringify({result, blank, spoken}))
366
+ process.exit(0)
367
+ `
368
+ const run = Bun.spawnSync([process.execPath, "-e", child], {cwd: join(import.meta.dir, "../..")})
369
+ expect(JSON.parse(run.stdout.toString().trim())).toEqual({result: true, blank: "INTERNAL", spoken: ["hello"]})
322
370
  })
323
371
 
324
372
  test("speak() is recorded and resolves once the utterance ends", async () => {
@@ -328,6 +376,15 @@ describe("speech", () => {
328
376
  const spoken = events.flatMap((event) => (event.type === "effect" && event.effect.kind === "speak" ? [event.effect.text] : []))
329
377
  expect(spoken).toEqual(["hi there"])
330
378
  })
379
+
380
+ test("speak() speaks symbols as the Glassly App prepares them", async () => {
381
+ const {session, events} = await connect()
382
+ await session.speaker.speak("+")
383
+ await session.speaker.speak(["Hello", "%"])
384
+ await session.speaker.speak("#", {enableSanitization: false})
385
+ const spoken = events.flatMap((event) => (event.type === "effect" && event.effect.kind === "speak" ? [event.effect.text] : []))
386
+ expect(spoken).toEqual(["plus", "Hello percent", "#"])
387
+ })
331
388
  })
332
389
 
333
390
  describe("phone", () => {
@@ -369,10 +426,11 @@ describe("phone", () => {
369
426
  expect(taps).toEqual(["double_tap"])
370
427
  })
371
428
 
372
- test("a request the simulator does not cover says so", async () => {
373
- const {session, host} = await connect()
374
- await expect(session.camera.takePhoto()).rejects.toMatchObject({code: "NOT_IMPLEMENTED"})
375
- expect((host.status().unsimulated as string[]).length).toBe(1)
429
+ test("a request type the simulator does not know is listed as not simulated", async () => {
430
+ const {host} = await connect()
431
+ host.fromBackground(JSON.stringify({payload: {type: "miniapp_made_up"}, requestId: "r1"}))
432
+ await settle()
433
+ expect(host.status().unsimulated).toEqual(["miniapp_made_up"])
376
434
  })
377
435
  })
378
436
 
@@ -467,6 +525,40 @@ describe("daily state", () => {
467
525
  expect(await again.session.settings.get("head_up_angle")).toBe(30)
468
526
  })
469
527
 
528
+ test("a reading speed one app sets reaches every app in the sim, and only open keys reach apps without the permission", async () => {
529
+ const registry = new SimRegistry()
530
+ const settings = createSimStorage()
531
+ const reddit = await connect({packageName: "com.example.reddit"}, "simulated", {registry, settings})
532
+ const news = await connect({packageName: "com.example.news"}, "simulated", {registry, settings})
533
+ const dashboard = await connect({packageName: "com.example.dash", permissions: [{type: "PLATFORM_SETTINGS"}]}, "simulated", {registry, settings})
534
+ const heard: Record<string, unknown[]> = {news: [], dash: []}
535
+ news.session.settings.onChange((change) => heard.news.push(change))
536
+ dashboard.session.settings.onChange((change) => heard.dash.push(change))
537
+ await settle()
538
+ expect(heard.news.map((change) => (change as {key: string}).key)).not.toContain("brightness")
539
+ heard.news.length = 0
540
+ heard.dash.length = 0
541
+
542
+ await reddit.session.settings.set("reading_wpm", 260)
543
+ await dashboard.session.settings.set("brightness", 30)
544
+ await settle()
545
+ expect(heard.news).toEqual([{key: "reading_wpm", value: 260}])
546
+ expect(heard.dash).toEqual([
547
+ {key: "reading_wpm", value: 260},
548
+ {key: "brightness", value: 30},
549
+ ])
550
+ expect(await news.session.settings.get("reading_wpm")).toBe(260)
551
+ })
552
+
553
+ test("an app reads only its own per-app reading speed", async () => {
554
+ const settings = createSimStorage()
555
+ settings.set("reading_app_speeds", {"com.example.reddit": 300, "com.example.health-notes": "off"})
556
+ const {session} = await connect({packageName: "com.example.reddit"}, "simulated", {settings})
557
+ const values = await session.settings.getAll()
558
+ expect(values.reading_app_speed).toBe(300)
559
+ expect(JSON.stringify(values)).not.toContain("health-notes")
560
+ })
561
+
470
562
  test("a dashboard miniapp gets the dashboard view only on glasses with custom firmware", async () => {
471
563
  for (const [device, views] of [
472
564
  ["g2-cfw", ["main", "dashboard"]],
@@ -482,6 +574,34 @@ describe("daily state", () => {
482
574
  })
483
575
  })
484
576
 
577
+ describe("microphone audio", () => {
578
+ test("PCM reaches audio_chunk subscribers in 10 ms chunks of 16 kHz pcm_s16le", async () => {
579
+ const {session, host} = await connect({permissions: [{type: "MICROPHONE"}]})
580
+ const chunks: {bytes: number[]; sampleRate?: number; format?: string}[] = []
581
+ session.mic.onAudioChunk((chunk) => chunks.push({bytes: [...Buffer.from(chunk.data, "base64")].slice(0, 2), sampleRate: chunk.sampleRate, format: chunk.format}))
582
+ await settle()
583
+ const pcm = new Uint8Array(330)
584
+ pcm[0] = 7
585
+ pcm[320] = 9
586
+ expect(await host.feedAudio(pcm, {realtime: false})).toEqual({chunks: 2, durationMs: 10})
587
+ await settle()
588
+ expect(chunks).toEqual([
589
+ {bytes: [7, 0], sampleRate: 16000, format: "pcm_s16le"},
590
+ {bytes: [9, 0], sampleRate: 16000, format: "pcm_s16le"},
591
+ ])
592
+ })
593
+ })
594
+
595
+ describe("forcing a failure", () => {
596
+ test("the next request of a type fails with the given code, and the one after it runs", async () => {
597
+ const {session, host} = await connect()
598
+ expect(host.failNext("storage_get", "QUOTA_EXCEEDED", "Out of quota")).toBe("miniapp_storage_get")
599
+ await expect(session.storage.get("notes")).rejects.toMatchObject({code: "QUOTA_EXCEEDED", message: "Out of quota"})
600
+ expect(await session.storage.get("notes")).toBeNull()
601
+ expect(() => host.failNext("storage_gett", "QUOTA_EXCEEDED")).toThrow('Unknown request type "storage_gett"')
602
+ })
603
+ })
604
+
485
605
  describe("calling the miniapp", () => {
486
606
  const actions = [{id: "set_language", description: "Set the language", parameters: {type: "object"}}]
487
607
 
@@ -583,6 +703,217 @@ describe("several miniapps", () => {
583
703
  expect(await running()).toEqual([["com.example.other", true]])
584
704
  })
585
705
 
706
+ test("only the provider of a widget can answer its event", async () => {
707
+ const registry = new SimRegistry()
708
+ const news = await connect({packageName: "com.example.news", widgets: [{id: "headlines", name: "Headlines", sizes: ["MD"]}]}, "simulated", {registry})
709
+ news.session.widgets.provide("headlines", {onEvent: async () => (await Bun.sleep(50), {handled: true})})
710
+ const intruder = await connect({packageName: "com.example.intruder"}, "simulated", {registry})
711
+ const dashboard = await connect({packageName: "com.example.dash", type: "dashboard", permissions: [{type: "HOST_WIDGETS"}]}, "simulated", {registry})
712
+ await dashboard.session.widgets.place("com.example.news", "headlines", {size: "MD", width: 251, height: 120})
713
+ const answer = dashboard.session.widgets.send("com.example.news", "headlines", {type: "tap"})
714
+ await settle()
715
+ for (let n = 1; n <= 20; n++) {
716
+ registry.handle(intruder.host, "miniapp_widget_result", {callId: `wdg-sim-${n}`, ok: true, result: {handled: false}})
717
+ }
718
+ expect(await answer).toEqual({handled: true})
719
+ })
720
+
721
+ test("a miniapp the glasses cannot run is listed as incompatible and refused", async () => {
722
+ const registry = new SimRegistry()
723
+ const camera = await connect(
724
+ {
725
+ packageName: "com.example.camera",
726
+ hardwareRequirements: [{type: "CAMERA", level: "REQUIRED"}],
727
+ actions: [{id: "snap", description: "Take a photo"}],
728
+ widgets: [{id: "last", name: "Last photo", sizes: ["MD"]}],
729
+ },
730
+ "g2",
731
+ {registry},
732
+ )
733
+ camera.session.actions.handle("snap", async () => ({captured: true}))
734
+ const manager = await connect(
735
+ {packageName: "com.example.manager", type: "dashboard", permissions: [{type: "MANAGE_MINIAPPS"}, {type: "HOST_WIDGETS"}]},
736
+ "g2",
737
+ {registry},
738
+ )
739
+ expect(await manager.session.miniapps.list()).toEqual([])
740
+ const [listed] = await manager.session.miniapps.list({includeIncompatible: true})
741
+ expect(listed.compatibility).toEqual({
742
+ isCompatible: false,
743
+ missingRequired: [{type: "CAMERA", level: "REQUIRED"}],
744
+ missingOptional: [],
745
+ warnings: [],
746
+ })
747
+ expect((await manager.session.widgets.list()).map((row) => row.compatible)).toEqual([false])
748
+ const refused = {code: "APP_NOT_COMPATIBLE", message: "com.example.camera is not compatible with the connected glasses"}
749
+ await expect(manager.session.miniapps.start("com.example.camera")).rejects.toMatchObject(refused)
750
+ await expect(manager.session.actions.invoke("com.example.camera", "snap")).rejects.toMatchObject(refused)
751
+ await expect(manager.session.widgets.place("com.example.camera", "last", {size: "MD", width: 251, height: 120})).rejects.toMatchObject(refused)
752
+ })
753
+
754
+ test("a gesture claimed by two miniapps reaches only the latest claimant, and goes back when it lets go", async () => {
755
+ const registry = new SimRegistry()
756
+ const main = await connect({packageName: "com.example.main"}, "simulated", {registry})
757
+ const companion = await connect({packageName: "com.example.companion"}, "simulated", {registry})
758
+ const heard: string[] = []
759
+ const claims: string[] = []
760
+ for (const [name, app] of [["main", main], ["companion", companion]] as const) {
761
+ app.session.input.onTouch("single_tap", () => heard.push(name))
762
+ app.session.input.onClaimChange((change) => claims.push(`${name} ${change.gesture} ${change.held}`))
763
+ }
764
+ await main.session.input.setClaimedGestures(["single_tap"])
765
+ await companion.session.input.setClaimedGestures(["single_tap"])
766
+ await settle()
767
+ main.host.touch("single_tap")
768
+ await companion.session.input.setClaimedGestures([])
769
+ await settle()
770
+ main.host.touch("single_tap")
771
+ await settle()
772
+ expect(claims).toEqual([
773
+ "main single_tap true",
774
+ "main single_tap false",
775
+ "companion single_tap true",
776
+ "companion single_tap false",
777
+ "main single_tap true",
778
+ ])
779
+ expect(heard).toEqual(["companion", "main"])
780
+ })
781
+
782
+ test("an unclaimed gesture reaches every miniapp listening for it", async () => {
783
+ const registry = new SimRegistry()
784
+ const main = await connect({packageName: "com.example.main"}, "simulated", {registry})
785
+ const companion = await connect({packageName: "com.example.companion"}, "simulated", {registry})
786
+ const heard: string[] = []
787
+ main.session.input.onTouch("single_tap", () => heard.push("main"))
788
+ companion.session.input.onTouch("single_tap", () => heard.push("companion"))
789
+ await settle()
790
+ main.host.touch("single_tap")
791
+ await settle()
792
+ expect(heard.sort()).toEqual(["companion", "main"])
793
+ })
794
+
795
+ test("a widget lists the expansion its provider set", async () => {
796
+ const registry = new SimRegistry()
797
+ const news = await connect(
798
+ {packageName: "com.example.news", widgets: [{id: "headlines", name: "Headlines", sizes: ["MD", "EX"], expandOnTap: true}]},
799
+ "simulated",
800
+ {registry},
801
+ )
802
+ const handle = news.session.widgets.provide("headlines", {})
803
+ const dashboard = await connect({packageName: "com.example.dash", type: "dashboard", permissions: [{type: "HOST_WIDGETS"}]}, "simulated", {registry})
804
+ expect((await dashboard.session.widgets.list()).map((row) => row.expandable)).toEqual([true])
805
+ await handle.setExpandable(false)
806
+ expect((await dashboard.session.widgets.list()).map((row) => row.expandable)).toEqual([false])
807
+ })
808
+
809
+ test("a companion that fails to launch answers WAKE_FAILED", async () => {
810
+ const registry = new SimRegistry({
811
+ launch: () => {
812
+ throw new Error("dist/background.js is missing")
813
+ },
814
+ })
815
+ new SimHost({
816
+ manifest: {packageName: "com.example.broken", name: "Broken"},
817
+ device: simDevice("simulated"),
818
+ storage: createSimStorage(),
819
+ registry,
820
+ toBackground: () => {},
821
+ emit: () => {},
822
+ })
823
+ const manager = await connect({packageName: "com.example.manager", permissions: [{type: "MANAGE_MINIAPPS"}]}, "simulated", {registry})
824
+ await expect(manager.session.miniapps.start("com.example.broken")).rejects.toMatchObject({
825
+ code: "WAKE_FAILED",
826
+ message: "failed to wake com.example.broken: dist/background.js is missing",
827
+ })
828
+ })
829
+
830
+ test("stopping a miniapp that is still starting stops it", async () => {
831
+ const halted: string[] = []
832
+ let starting: SimHost | null = null
833
+ const registry = new SimRegistry({
834
+ launch: () => starting!.backgroundStarted(),
835
+ halt: (packageName) => {
836
+ halted.push(packageName)
837
+ starting!.backgroundStopped(false)
838
+ },
839
+ })
840
+ starting = new SimHost({
841
+ manifest: {packageName: "com.example.slow", name: "Slow"},
842
+ device: simDevice("simulated"),
843
+ storage: createSimStorage(),
844
+ registry,
845
+ toBackground: () => {},
846
+ emit: () => {},
847
+ })
848
+ const manager = await connect({packageName: "com.example.manager", permissions: [{type: "MANAGE_MINIAPPS"}]}, "simulated", {registry})
849
+ const start = manager.session.miniapps.start("com.example.slow")
850
+ await settle()
851
+ await manager.session.miniapps.stop("com.example.slow")
852
+ expect(halted).toEqual(["com.example.slow"])
853
+ await expect(start).rejects.toMatchObject({code: "WAKE_FAILED"})
854
+ })
855
+
856
+ test("a provider started only for its widget stops when its last placement goes, and is not listed as running", async () => {
857
+ let news: Awaited<ReturnType<typeof connect>> | null = null
858
+ const halted: string[] = []
859
+ const registry = new SimRegistry({
860
+ launch: () =>
861
+ void connect({packageName: "com.example.news", widgets: [{id: "headlines", name: "Headlines", sizes: ["MD"]}]}, "simulated", {registry}).then(
862
+ (app) => (news = app),
863
+ ),
864
+ halt: (packageName) => {
865
+ halted.push(packageName)
866
+ news?.host.backgroundStopped(false)
867
+ },
868
+ })
869
+ new SimHost({
870
+ manifest: {packageName: "com.example.news", name: "News", widgets: [{id: "headlines", name: "Headlines", sizes: ["MD"]}]},
871
+ device: simDevice("simulated"),
872
+ storage: createSimStorage(),
873
+ registry,
874
+ toBackground: () => {},
875
+ emit: () => {},
876
+ })
877
+ const dashboard = await connect(
878
+ {packageName: "com.example.dash", type: "dashboard", permissions: [{type: "HOST_WIDGETS"}, {type: "MANAGE_MINIAPPS"}]},
879
+ "simulated",
880
+ {registry},
881
+ )
882
+ await dashboard.session.widgets.place("com.example.news", "headlines", {size: "MD", width: 251, height: 120})
883
+ expect((await dashboard.session.miniapps.list()).map((app) => [app.packageName, app.running])).toEqual([["com.example.news", false]])
884
+ await dashboard.session.widgets.unplace("com.example.news", "headlines")
885
+ expect(halted).toEqual(["com.example.news"])
886
+ })
887
+
888
+ test("a provider started only for its widget stops when the miniapp hosting it stops", async () => {
889
+ let news: Awaited<ReturnType<typeof connect>> | null = null
890
+ const halted: string[] = []
891
+ const registry = new SimRegistry({
892
+ launch: () =>
893
+ void connect({packageName: "com.example.news", widgets: [{id: "headlines", name: "Headlines", sizes: ["MD"]}]}, "simulated", {registry}).then(
894
+ (app) => (news = app),
895
+ ),
896
+ halt: (packageName) => {
897
+ halted.push(packageName)
898
+ if (packageName === "com.example.news") news?.host.backgroundStopped(false)
899
+ else dashboard.host.backgroundStopped(false)
900
+ },
901
+ })
902
+ new SimHost({
903
+ manifest: {packageName: "com.example.news", name: "News", widgets: [{id: "headlines", name: "Headlines", sizes: ["MD"]}]},
904
+ device: simDevice("simulated"),
905
+ storage: createSimStorage(),
906
+ registry,
907
+ toBackground: () => {},
908
+ emit: () => {},
909
+ })
910
+ const dashboard = await connect({packageName: "com.example.dash", type: "dashboard", permissions: [{type: "HOST_WIDGETS"}]}, "simulated", {registry})
911
+ const manager = await connect({packageName: "com.example.manager", permissions: [{type: "MANAGE_MINIAPPS"}]}, "simulated", {registry})
912
+ await dashboard.session.widgets.place("com.example.news", "headlines", {size: "MD", width: 251, height: 120})
913
+ await manager.session.miniapps.stop("com.example.dash")
914
+ expect(halted.sort()).toEqual(["com.example.dash", "com.example.news"])
915
+ })
916
+
586
917
  test("widget calls are checked as the phone checks them", async () => {
587
918
  const widgets = [{id: "headlines", name: "Headlines", sizes: ["MD"]}]
588
919
  const {session, host} = await connect({widgets})
@@ -618,3 +949,111 @@ describe("several miniapps", () => {
618
949
  expect(((await first) as {elements: {text: string}[]}).elements[0].text).toBe("100")
619
950
  })
620
951
  })
952
+
953
+ describe("recording a played script", () => {
954
+ async function recorder(device: string, png: () => Promise<Uint8Array>, transition?: RenderTransition) {
955
+ let server: ReturnType<typeof startSimServer> | undefined
956
+ const {session, host} = await connect({permissions: [{type: "MICROPHONE"}]}, device, {}, (event) => server?.handle(event))
957
+ session.transcription.on((data) => {
958
+ void session.display.render([{id: "caption", type: "text", text: data.text, box: {x: 0, y: 0, w: 576, h: 100}, ...(transition ? {transition} : {})}])
959
+ })
960
+ await settle()
961
+ server = startSimServer({
962
+ host,
963
+ port: 0,
964
+ packageName: "com.example.sim",
965
+ ui: null,
966
+ page: {html: "", js: "", css: ""},
967
+ restart: async () => {},
968
+ stop: () => {},
969
+ screen: {update() {}, png},
970
+ })
971
+ const play = async (script: string, wordsPerMinute: number) => {
972
+ const dir = mkdtempSync(join(tmpdir(), "sim-frames-"))
973
+ const response = await fetch(`http://127.0.0.1:${server!.port}/api/play`, {
974
+ method: "POST",
975
+ headers: {"content-type": "application/json"},
976
+ body: JSON.stringify({script, wordsPerMinute, framesDir: dir}),
977
+ })
978
+ return {status: response.status, dir, body: (await response.json()) as {ok: boolean; frames?: number; error?: string}, files: () => readdirSync(dir)}
979
+ }
980
+ return {host, play, stop: () => server!.stop()}
981
+ }
982
+
983
+ test("a frame that cannot be saved fails the play instead of reporting it", async () => {
984
+ const {play, stop} = await recorder("simulated", async () => {
985
+ throw new Error("renderer crashed")
986
+ })
987
+ const result = await play("hello", 60_000)
988
+ stop()
989
+ expect(result.status).toBe(400)
990
+ expect(result.body.ok).toBe(false)
991
+ expect(result.body.error).toContain("renderer crashed")
992
+ expect(result.files()).toEqual([])
993
+ })
994
+
995
+ test("an animated frame is saved once it settles", async () => {
996
+ const remaining: number[] = []
997
+ const rec = await recorder(
998
+ "g2-cfw",
999
+ async () => {
1000
+ remaining.push(rec.host.displaySettlesAt() - Date.now())
1001
+ return new Uint8Array([1])
1002
+ },
1003
+ {durationMs: 600, enter: {dy: 60}},
1004
+ )
1005
+ const result = await rec.play("hello", 60_000)
1006
+ rec.stop()
1007
+ expect(result.body.frames).toBeGreaterThan(0)
1008
+ expect(result.files()).toHaveLength(result.body.frames!)
1009
+ expect(Math.max(...remaining)).toBeLessThanOrEqual(0)
1010
+ })
1011
+
1012
+ test("a frame still rendering when another play starts is not saved as the old play's", async () => {
1013
+ const caption = () => {
1014
+ const content = rec.host.snapshot().content
1015
+ return content.kind === "scene" ? content.elements.map((element) => (element as {text?: string}).text ?? "").join(" ") : ""
1016
+ }
1017
+ let started!: () => void
1018
+ const captureStarted = new Promise<void>((resolve) => (started = resolve))
1019
+ let release!: () => void
1020
+ const released = new Promise<void>((resolve) => (release = resolve))
1021
+ let calls = 0
1022
+ const rec = await recorder("simulated", async () => {
1023
+ if (++calls === 1) {
1024
+ started()
1025
+ await released
1026
+ }
1027
+ return new TextEncoder().encode(caption())
1028
+ })
1029
+ const first = rec.play("first old script that is long", 600)
1030
+ await captureStarted
1031
+ const second = rec.play("one two three", 60_000)
1032
+ await Bun.sleep(50)
1033
+ release()
1034
+ const [replaced, next] = [await first, await second]
1035
+ rec.stop()
1036
+ const saved = (result: typeof next) => result.files().map((file) => readFileSync(join(result.dir, file), "utf8"))
1037
+ expect(saved(replaced).every((text) => "first old script that is long".startsWith(text))).toBe(true)
1038
+ expect(replaced.files()).toHaveLength(replaced.body.frames!)
1039
+ expect(saved(next).every((text) => "one two three".startsWith(text))).toBe(true)
1040
+ })
1041
+
1042
+ test("a play that replaces another keeps its own frames", async () => {
1043
+ const caption = () => {
1044
+ const content = rec.host.snapshot().content
1045
+ return content.kind === "scene" ? content.elements.map((element) => (element as {text?: string}).text ?? "").join(" ") : ""
1046
+ }
1047
+ const rec = await recorder("simulated", async () => new TextEncoder().encode(caption()))
1048
+ const first = rec.play("first old script that is long", 600)
1049
+ await Bun.sleep(150)
1050
+ const second = await rec.play("one two three", 60_000)
1051
+ const replaced = await first
1052
+ rec.stop()
1053
+ const saved = (result: typeof second) => result.files().map((file) => readFileSync(join(result.dir, file), "utf8"))
1054
+ expect(second.body.frames).toBeGreaterThan(0)
1055
+ expect(saved(second).every((text) => "one two three".startsWith(text))).toBe(true)
1056
+ expect(saved(replaced).every((text) => "first old script that is long".startsWith(text))).toBe(true)
1057
+ expect(replaced.files()).toHaveLength(replaced.body.frames!)
1058
+ })
1059
+ })