@glassly/miniapp-cli 0.1.16 → 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.
@@ -13,7 +13,25 @@ const WAKE_TIMEOUT_MS = 10_000
13
13
  const PAYLOAD_MAX = 256 * 1024
14
14
  const WIDGET_FRAME_CAP = 64
15
15
  const WIDGET_EXPANDED_FRAME_CAP = 128
16
- const COMPATIBLE = {isCompatible: true, missingRequired: [], missingOptional: [], warnings: []}
16
+
17
+ type Requirement = {type?: string; level?: string}
18
+
19
+ // The Glassly App's HardwareCompatibility: a missing REQUIRED part makes the
20
+ // app incompatible; the phone microphone stands in for one the glasses lack.
21
+ function compatibilityOf(requirements: unknown, capabilities: Record<string, unknown>) {
22
+ const result = {isCompatible: true, missingRequired: [] as Requirement[], missingOptional: [] as Requirement[], warnings: [] as string[]}
23
+ for (const requirement of Array.isArray(requirements) ? (requirements as Requirement[]) : []) {
24
+ const type = String(requirement.type ?? "").toUpperCase()
25
+ const flag = `has${type === "IMU" ? "IMU" : type.charAt(0) + type.slice(1).toLowerCase()}`
26
+ const present = type === "MICROPHONE" || type === "EXIST" || capabilities[flag] === true
27
+ if (present) continue
28
+ if (String(requirement.level ?? "").toUpperCase() === "REQUIRED") {
29
+ result.missingRequired.push(requirement)
30
+ result.isCompatible = false
31
+ } else result.missingOptional.push(requirement)
32
+ }
33
+ return result
34
+ }
17
35
 
18
36
  const fail = (code: string, message: string): Outcome => ({ok: false, error: {code, message}})
19
37
  const tooLarge = (value: unknown) => JSON.stringify(value ?? null).length > PAYLOAD_MAX
@@ -39,11 +57,13 @@ export class SimRegistry {
39
57
  tooLarge,
40
58
  )
41
59
  : null
42
- private readonly widgetCalls = new Map<string, {host: string; settle: (outcome: Outcome) => void; timer: ReturnType<typeof setTimeout>}>()
60
+ private readonly widgetCalls = new Map<string, {host: string; provider: string; settle: (outcome: Outcome) => void; timer: ReturnType<typeof setTimeout>}>()
43
61
  private readonly cliFrames = new Map<string, (elements: unknown[]) => void>()
44
62
  private callSeq = 0
45
63
  /** The wearer's per-widget choice whether a tap expands an EX widget. */
46
64
  private readonly expandOnTap = new Map<string, boolean>()
65
+ private readonly widgetOnly = new Set<string>()
66
+ private gestureClaims = hostRules.emptyGestureClaims?.()
47
67
 
48
68
  constructor(private readonly options: SimRegistryOptions = {}) {}
49
69
 
@@ -65,6 +85,7 @@ export class SimRegistry {
65
85
  actions: hostRules.normalizeManifestActions?.(host.manifest.actions) ?? [],
66
86
  widgets: hostRules.normalizeManifestWidgets?.(host.manifest.widgets) ?? [],
67
87
  running: host.connected,
88
+ compatibility: compatibilityOf(host.manifest.hardwareRequirements, host.deviceCapabilities),
68
89
  }))
69
90
  }
70
91
 
@@ -73,7 +94,8 @@ export class SimRegistry {
73
94
  if (!this.actionCalls || !this.placements) return false
74
95
  const answer = (outcome: Outcome) =>
75
96
  outcome.ok ? caller.reply(requestId, true, outcome.data) : caller.reply(requestId, false, undefined, outcome.error)
76
- const run = (work: Promise<Outcome>) => void work.then(answer)
97
+ const run = (work: Promise<Outcome>) =>
98
+ void work.then(answer, (error) => answer(fail(MiniappErrorCode.INTERNAL, (error as Error).message)))
77
99
  switch (type) {
78
100
  case MiniappRequestType.MINIAPPS_LIST:
79
101
  if (!caller.requirePermission(requestId, "MANAGE_MINIAPPS")) return true
@@ -81,14 +103,15 @@ export class SimRegistry {
81
103
  ok: true,
82
104
  data: this.interopApps()
83
105
  .filter((app) => app.packageName !== caller.packageName)
84
- .map(({packageName, name, version, running, actions}) => ({
106
+ .filter((app) => payload.includeIncompatible === true || app.compatibility.isCompatible)
107
+ .map(({packageName, name, version, running, actions, compatibility}) => ({
85
108
  packageName,
86
109
  name,
87
110
  version,
88
- running,
111
+ running: running && !this.widgetOnly.has(packageName),
89
112
  hidden: false,
90
113
  system: false,
91
- compatibility: COMPATIBLE,
114
+ compatibility,
92
115
  actions,
93
116
  })),
94
117
  }), true
@@ -96,6 +119,8 @@ export class SimRegistry {
96
119
  if (!caller.requirePermission(requestId, "MANAGE_MINIAPPS")) return true
97
120
  const target = payload.packageName as string | undefined
98
121
  if (!target || !this.hosts.has(target)) return answer(fail(MiniappErrorCode.APP_NOT_FOUND, `Miniapp not found: ${target ?? "(missing packageName)"}`)), true
122
+ if (this.incompatible(target)) return answer(this.incompatible(target)!), true
123
+ this.widgetOnly.delete(target)
99
124
  run(this.wake(target).then((error) => error ?? {ok: true, data: undefined}))
100
125
  return true
101
126
  }
@@ -139,16 +164,15 @@ export class SimRegistry {
139
164
  if (!widget?.expandable) {
140
165
  return answer(fail(MiniappErrorCode.WIDGET_NOT_FOUND, `${caller.packageName} has no widget "${widgetId ?? "(missing widgetId)"}" that lists EX`)), true
141
166
  }
142
- const key = `${caller.packageName}/${widget.id}`
143
- if (typeof payload.expandable === "boolean") this.expandOnTap.set(key, payload.expandable)
144
- return answer({ok: true, data: {expandable: this.expandOnTap.get(key) ?? widget.expandOnTap}}), true
167
+ if (typeof payload.expandable === "boolean") this.expandOnTap.set(`${caller.packageName}/${widget.id}`, payload.expandable)
168
+ return answer({ok: true, data: {expandable: this.expandable(caller.packageName, widget)}}), true
145
169
  }
146
170
  case MiniappRequestType.WIDGET_UPDATE:
147
171
  this.widgetUpdate(caller.packageName, payload)
148
172
  return true
149
173
  case MiniappRequestType.WIDGET_RESULT: {
150
174
  const pending = this.widgetCalls.get(String(payload.callId))
151
- if (!pending) return true
175
+ if (!pending || pending.provider !== caller.packageName) return true
152
176
  this.widgetCalls.delete(String(payload.callId))
153
177
  clearTimeout(pending.timer)
154
178
  if (payload.ok !== true) {
@@ -206,23 +230,85 @@ export class SimRegistry {
206
230
 
207
231
  stop(packageName: string): void {
208
232
  const host = this.hosts.get(packageName)
209
- if (!host?.connected) return
210
- this.placements = hostRules.dropHost!(this.placements!, packageName)
233
+ if (!host?.running) return
234
+ this.releaseWidgetHost(packageName)
211
235
  this.placements = hostRules.dropProvider!(this.placements!, packageName)
236
+ this.widgetOnly.delete(packageName)
212
237
  this.options.halt?.(packageName)
213
238
  }
214
239
 
215
- /** Starts a miniapp that is not running and waits for its CONNECT; resolves with an error outcome on failure. */
240
+ /** A stopped host's widgets go; a provider left with none that only ran for them stops, as on the phone. */
241
+ releaseWidgetHost(packageName: string): void {
242
+ if (!this.placements) return
243
+ const providers = hostRules.placedProviders!(this.placements)
244
+ this.placements = hostRules.dropHost!(this.placements, packageName)
245
+ for (const provider of providers) {
246
+ if (provider !== packageName && this.widgetOnly.has(provider) && !hostRules.isProviderPlaced!(this.placements, provider)) this.stop(provider)
247
+ }
248
+ }
249
+
250
+ claimGestures(host: SimHost, gestures: string[], scope?: string): void {
251
+ if (!this.gestureClaims || !hostRules.setGestureClaims) return
252
+ this.applyClaims(hostRules.setGestureClaims(this.gestureClaims, host.packageName, gestures, scope))
253
+ }
254
+
255
+ dropGestureClaims(packageName: string): void {
256
+ if (!this.gestureClaims || !hostRules.dropGestureClaims) return
257
+ this.applyClaims(hostRules.dropGestureClaims(this.gestureClaims, packageName))
258
+ }
259
+
260
+ /** The gestures `packageName` currently holds. */
261
+ heldGestures(packageName: string): string[] {
262
+ const state = this.gestureClaims
263
+ if (!state || !hostRules.gestureClaimant) return []
264
+ const gestures = new Set(state.registrations.flatMap((entry) => [...entry.gestures]))
265
+ return [...gestures].filter((gesture) => hostRules.gestureClaimant!(state, gesture) === packageName)
266
+ }
267
+
268
+ /** Who hears a touch: its claimant alone (unless it only serves widgets), or every running miniapp when nobody claims it. */
269
+ touchReceivers(gesture: string): SimHost[] {
270
+ const claimant = this.gestureClaims && hostRules.gestureClaimant ? hostRules.gestureClaimant(this.gestureClaims, gesture) : null
271
+ if (claimant && !this.widgetOnly.has(claimant)) {
272
+ const host = this.hosts.get(claimant)
273
+ return host ? [host] : []
274
+ }
275
+ return this.apps().filter((host) => host.connected && !this.widgetOnly.has(host.packageName))
276
+ }
277
+
278
+ private applyClaims(result: {state: NonNullable<SimRegistry["gestureClaims"]>; changes: {packageName: string; gesture: string; held: boolean}[]}): void {
279
+ this.gestureClaims = result.state
280
+ for (const change of result.changes) this.hosts.get(change.packageName)?.gestureClaimChanged(change.gesture, change.held)
281
+ }
282
+
283
+ private incompatible(packageName: string): Outcome | null {
284
+ const app = this.interopApps().find((candidate) => candidate.packageName === packageName)
285
+ return app && !app.compatibility.isCompatible
286
+ ? fail(MiniappErrorCode.APP_NOT_COMPATIBLE, `${packageName} is not compatible with the connected glasses`)
287
+ : null
288
+ }
289
+
290
+ private expandable(provider: string, widget: {id: string; expandOnTap?: boolean}): boolean {
291
+ return this.expandOnTap.get(`${provider}/${widget.id}`) ?? widget.expandOnTap ?? false
292
+ }
293
+
294
+ /** Starts a miniapp that is not running and waits for its CONNECT, or until it stops; resolves with an error outcome on failure. */
216
295
  private async wake(packageName: string): Promise<Outcome | null> {
217
296
  const host = this.hosts.get(packageName)
218
297
  if (!host) return fail(MiniappErrorCode.APP_NOT_FOUND, `Miniapp not found: ${packageName}`)
219
298
  if (host.connected) return null
220
- if (!this.options.launch) return fail(MiniappErrorCode.WAKE_FAILED, `failed to wake ${packageName}`)
221
- this.options.launch(packageName)
222
- const connected = () => this.hosts.get(packageName)?.connected === true
299
+ const failed = (reason: string) => fail(MiniappErrorCode.WAKE_FAILED, `failed to wake ${packageName}: ${reason}`)
300
+ if (!this.options.launch) return failed("the simulator cannot start it")
301
+ try {
302
+ this.options.launch(packageName)
303
+ } catch (error) {
304
+ this.options.halt?.(packageName)
305
+ return failed((error as Error).message)
306
+ }
223
307
  const deadline = Date.now() + WAKE_TIMEOUT_MS
224
- while (!connected() && Date.now() < deadline) await Bun.sleep(20)
225
- return connected() ? null : fail(MiniappErrorCode.WAKE_FAILED, `failed to wake ${packageName}`)
308
+ const current = () => this.hosts.get(packageName) ?? host
309
+ while (!current().connected && current().running && Date.now() < deadline) await Bun.sleep(20)
310
+ if (current().connected) return null
311
+ return failed(current().running ? `no CONNECT within ${WAKE_TIMEOUT_MS} ms` : "it stopped while starting")
226
312
  }
227
313
 
228
314
  private async invoke(
@@ -258,9 +344,9 @@ export class SimRegistry {
258
344
  name: widget.name,
259
345
  sizes: widget.sizes,
260
346
  defaultSize: widget.defaultSize,
261
- expandable: widget.expandOnTap,
262
- running: app.running,
263
- compatible: true,
347
+ expandable: this.expandable(app.packageName, widget),
348
+ running: app.running && !this.widgetOnly.has(app.packageName),
349
+ compatible: app.compatibility.isCompatible,
264
350
  })
265
351
  }
266
352
  }
@@ -284,7 +370,10 @@ export class SimRegistry {
284
370
  const box = parseWidgetBox(payload.box)
285
371
  if (!box) return fail(MiniappErrorCode.INVALID_ARGUMENT, "place needs a box {size, width, height}")
286
372
  const {provider, widgetId} = target
373
+ const incompatible = this.incompatible(provider)
374
+ if (incompatible) return incompatible
287
375
  this.placements = hostRules.placeWidget!(this.placements!, {hostPackageName, providerPackageName: provider, widgetId, box})
376
+ if (!this.hosts.get(provider)?.running) this.widgetOnly.add(provider)
288
377
  const woken = await this.wake(provider)
289
378
  if (woken) {
290
379
  this.placements = hostRules.unplaceWidget!(this.placements!, hostPackageName, provider, widgetId)
@@ -311,6 +400,7 @@ export class SimRegistry {
311
400
  if (had && hostRules.hostsOf!(this.placements!, provider, widgetId).length === 0) {
312
401
  this.hosts.get(provider)?.deliver({type: MiniappResponseType.WIDGET_CALL, callId: `wdg-sim-${++this.callSeq}`, widgetId, kind: "box", box: null})
313
402
  }
403
+ if (this.widgetOnly.has(provider) && !hostRules.isProviderPlaced!(this.placements!, provider)) this.stop(provider)
314
404
  return {ok: true, data: undefined}
315
405
  }
316
406
 
@@ -334,7 +424,7 @@ export class SimRegistry {
334
424
  this.widgetCalls.delete(callId)
335
425
  settle(fail(MiniappErrorCode.ACTION_TIMEOUT, `widget event "${event.type}" timed out after ${timeoutMs}ms`))
336
426
  }, timeoutMs)
337
- this.widgetCalls.set(callId, {host: hostPackageName, settle, timer})
427
+ this.widgetCalls.set(callId, {host: hostPackageName, provider, settle, timer})
338
428
  this.hosts.get(provider)!.deliver({type: MiniappResponseType.WIDGET_CALL, callId, widgetId, kind: "event", event})
339
429
  })
340
430
  }
@@ -26,6 +26,10 @@ test("a colon inside the text is not a speaker", () => {
26
26
  ])
27
27
  })
28
28
 
29
+ test("a language needs no speaker", () => {
30
+ expect(parseScript("(es): Hola | en: Hello")).toEqual([{line: 1, language: "es", text: "Hola", translations: {en: "Hello"}}])
31
+ })
32
+
29
33
  test("a malformed time names its line", () => {
30
34
  expect(() => parseScript("hello\n[1:7x] A: hi")).toThrow("Line 2: a time is [m:ss] or [m:ss.s], got [1:7x]")
31
35
  })
package/src/sim/script.ts CHANGED
@@ -10,7 +10,7 @@ export interface ScriptLine {
10
10
  }
11
11
 
12
12
  const TIME = /^\[([^\]]*)\]\s*/
13
- const SPEAKER = /^([\p{L}\p{N}_.-]{1,24})(?:\s*\(([A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8})?)\))?:\s+/u
13
+ const SPEAKER = /^([\p{L}\p{N}_.-]{1,24})?\s*(?:\(([A-Za-z]{2,3}(?:-[A-Za-z0-9]{2,8})?)\))?:\s+/u
14
14
 
15
15
  /**
16
16
  * Parses a transcript script: one utterance per line, as
@@ -31,8 +31,8 @@ export function parseScript(source: string): ScriptLine[] {
31
31
  rest = rest.slice(time[0].length)
32
32
  }
33
33
  const speaker = SPEAKER.exec(rest)
34
- if (speaker) {
35
- entry.speaker = speaker[1]
34
+ if (speaker && (speaker[1] || speaker[2])) {
35
+ if (speaker[1]) entry.speaker = speaker[1]
36
36
  if (speaker[2]) entry.language = speaker[2]
37
37
  rest = rest.slice(speaker[0].length)
38
38
  }
package/src/sim/server.ts CHANGED
@@ -7,6 +7,7 @@ import type {MirrorSnapshot} from "@glassly/miniapp/mirror"
7
7
  import type {SimHost, SimHostEvent} from "./host.js"
8
8
  import {parsePhone, phoneLayout, RAIL_BACK, type PhoneLayout} from "./phone.js"
9
9
  import {parseScript} from "./script.js"
10
+ import {readWavPcm} from "./wav.js"
10
11
  import type {ScreenRenderer} from "./screenshot.js"
11
12
 
12
13
  export interface SimServerOptions {
@@ -263,16 +264,48 @@ export function startSimServer(options: SimServerOptions): SimServer {
263
264
  })
264
265
  }
265
266
 
266
- // While a script plays with a frames folder, each frame is saved once it settles.
267
- let recordFrames: {dir: string; count: number; timer: ReturnType<typeof setTimeout> | null} | null = null
268
- const scheduleFrame = (recording: NonNullable<typeof recordFrames>) => {
267
+ interface Recording {
268
+ dir: string
269
+ count: number
270
+ timer: ReturnType<typeof setTimeout> | null
271
+ dueAt: number
272
+ saving: Promise<void>
273
+ error: Error | null
274
+ superseded: boolean
275
+ }
276
+ let recordFrames: Recording | null = null
277
+ const unfinished = new Set<Recording>()
278
+ const saveFrame = (recording: Recording) => {
279
+ recording.timer = null
280
+ recording.saving = recording.saving.then(async () => {
281
+ if (recording.error || recording.superseded || typeof options.screen === "string") return
282
+ try {
283
+ const png = await options.screen.png(2)
284
+ if (recording.superseded) return
285
+ writeFileSync(join(recording.dir, `frame-${String(recording.count + 1).padStart(4, "0")}.png`), png)
286
+ recording.count++
287
+ } catch (error) {
288
+ recording.error = error as Error
289
+ }
290
+ })
291
+ }
292
+ const scheduleFrame = (recording: Recording) => {
269
293
  if (recording.timer) clearTimeout(recording.timer)
270
- recording.timer = setTimeout(async () => {
271
- recording.timer = null
272
- if (typeof options.screen === "string") return
273
- const file = join(recording.dir, `frame-${String(++recording.count).padStart(4, "0")}.png`)
274
- writeFileSync(file, await options.screen.png(2))
275
- }, Math.max(FRAME_SETTLE_MS, host.displaySettlesAt() - Date.now() + FRAME_SETTLE_MS))
294
+ const delay = Math.max(FRAME_SETTLE_MS, host.displaySettlesAt() - Date.now() + FRAME_SETTLE_MS)
295
+ recording.dueAt = Date.now() + delay
296
+ recording.timer = setTimeout(() => saveFrame(recording), delay)
297
+ }
298
+ const finishRecording = async (recording: Recording): Promise<number> => {
299
+ if (recordFrames === recording) recordFrames = null
300
+ if (recording.timer) {
301
+ clearTimeout(recording.timer)
302
+ await Bun.sleep(Math.max(0, recording.dueAt - Date.now()))
303
+ saveFrame(recording)
304
+ }
305
+ await recording.saving
306
+ unfinished.delete(recording)
307
+ if (recording.error) throw new Error(`Saving a frame to ${recording.dir} failed: ${recording.error.message}`)
308
+ return recording.count
276
309
  }
277
310
 
278
311
  const api = async (request: Request, url: URL): Promise<Response> => {
@@ -354,15 +387,21 @@ export function startSimServer(options: SimServerOptions): SimServer {
354
387
  if (lines.length === 0) throw new Error("The script has no lines to play")
355
388
  const frames = text("framesDir")
356
389
  if (frames && typeof options.screen === "string") throw new Error(`Frames need screenshots, which are unavailable: ${options.screen}`)
357
- recordFrames = frames ? {dir: frames, count: 0, timer: null} : null
358
390
  if (frames) mkdirSync(frames, {recursive: true})
391
+ for (const earlier of unfinished) {
392
+ earlier.superseded = true
393
+ if (earlier.timer) clearTimeout(earlier.timer)
394
+ earlier.timer = null
395
+ }
396
+ const recording: Recording | null = frames
397
+ ? {dir: frames, count: 0, timer: null, dueAt: 0, saving: Promise.resolve(), error: null, superseded: false}
398
+ : null
399
+ if (recording) unfinished.add(recording)
400
+ recordFrames = recording
359
401
  const played = await host.play(lines, {
360
402
  wordsPerMinute: body.wordsPerMinute === undefined ? undefined : number("wordsPerMinute"),
361
403
  })
362
- await Bun.sleep(400)
363
- const written = recordFrames?.count ?? 0
364
- recordFrames = null
365
- return json({ok: true, ...played, frames: written})
404
+ return json({ok: true, ...played, frames: recording ? await finishRecording(recording) : 0})
366
405
  }
367
406
  case "location":
368
407
  host.setLocation(number("lat"), number("lng"), body.accuracy === undefined ? undefined : number("accuracy"))
@@ -401,6 +440,13 @@ export function startSimServer(options: SimServerOptions): SimServer {
401
440
  break
402
441
  case "ui/request":
403
442
  return json({ok: true, result: await host.uiRequest(text("channel") ?? "", body.payload, Number(body.timeoutMs) || undefined)})
443
+ case "audio": {
444
+ const file = text("path")
445
+ if (!file) return json({ok: false, error: "audio needs the path of a WAV file"}, 400)
446
+ return json({ok: true, ...(await host.feedAudio(readWavPcm(readFileSync(file))))})
447
+ }
448
+ case "fail":
449
+ return json({ok: true, type: host.failNext(text("type") ?? "", text("code") ?? "", text("message"))})
404
450
  case "reset":
405
451
  await host.reset()
406
452
  break
package/src/sim/sim.ts CHANGED
@@ -35,7 +35,6 @@ export interface SimRecord {
35
35
 
36
36
  const COLOR = {reset: "\x1b[0m", dim: "\x1b[2m", red: "\x1b[31m", yellow: "\x1b[33m", magenta: "\x1b[35m", cyan: "\x1b[36m"}
37
37
 
38
- /** Where a miniapp's simulated storage, settings and memory live between runs, one folder per project. */
39
38
  export function simStateDir(packageName: string, cwd: string): string {
40
39
  return join(os.homedir(), ".glassly", "sim", packageName, createHash("sha1").update(resolve(cwd)).digest("hex").slice(0, 12))
41
40
  }
@@ -52,7 +51,7 @@ export async function simReset(cwd: string): Promise<string> {
52
51
  })
53
52
  if (!response.ok) throw new Error(`The simulator answered ${response.status} to reset`)
54
53
  } else {
55
- for (const name of ["storage.json", "settings.json", "memory.json"]) rmSync(join(simStateDir(manifest.packageName, cwd), name), {force: true})
54
+ for (const name of ["storage.json", "settings.json", "memory.json", "blobs"]) rmSync(join(simStateDir(manifest.packageName, cwd), name), {force: true, recursive: true})
56
55
  }
57
56
  return simStateDir(manifest.packageName, cwd)
58
57
  }
@@ -272,12 +271,13 @@ export async function sim(options: SimOptions = {}): Promise<void> {
272
271
  launch: (packageName) => (packageName === manifest.packageName ? startBackground() : others.get(packageName)?.start()),
273
272
  halt: (packageName) => (packageName === manifest.packageName ? stopBackground(false) : others.get(packageName)?.stop()),
274
273
  })
274
+ const settings = modules.createSimStorage(join(stateDir, "settings.json"))
275
275
  const host = new modules.SimHost({
276
276
  manifest,
277
277
  device,
278
278
  registry,
279
279
  storage: modules.createSimStorage(join(stateDir, "storage.json")),
280
- settings: modules.createSimStorage(join(stateDir, "settings.json")),
280
+ settings,
281
281
  memory: modules.createSimStorage(join(stateDir, "memory.json")),
282
282
  stateDir,
283
283
  toBackground: (raw) => background?.send(raw),
@@ -321,7 +321,7 @@ export async function sim(options: SimOptions = {}): Promise<void> {
321
321
  device,
322
322
  registry,
323
323
  storage: modules.createSimStorage(join(otherState, "storage.json")),
324
- settings: modules.createSimStorage(join(otherState, "settings.json")),
324
+ settings,
325
325
  memory: modules.createSimStorage(join(otherState, "memory.json")),
326
326
  stateDir: otherState,
327
327
  toBackground: (raw) => worker?.send(raw),
@@ -0,0 +1,122 @@
1
+ /// <reference types="bun-types" />
2
+
3
+ import {describe, expect, test} from "bun:test"
4
+ import {MiniappSession, type Transport} from "@glassly/miniapp"
5
+
6
+ import {simDevice} from "../device.js"
7
+ import {SimHost, type SimHostEvent, type SimManifest} from "../host.js"
8
+ import {createSimStorage} from "../storage.js"
9
+
10
+ async function connect(manifest: Partial<SimManifest> = {}, device = "simulated") {
11
+ const events: SimHostEvent[] = []
12
+ let deliver: (raw: string) => void = () => {}
13
+ const host = new SimHost({
14
+ manifest: {packageName: "com.example.sim", name: "Sim Test", ...manifest},
15
+ device: simDevice(device),
16
+ storage: createSimStorage(),
17
+ toBackground: (raw) => queueMicrotask(() => deliver(raw)),
18
+ emit: (event) => events.push(event),
19
+ frameLatencyMs: 0,
20
+ speechMsPerChar: 0,
21
+ })
22
+ const transport: Transport = {
23
+ open: async () => {},
24
+ send: (raw) => queueMicrotask(() => host.fromBackground(raw)),
25
+ onMessage: (handler) => {
26
+ deliver = handler
27
+ },
28
+ onDisconnect: () => {},
29
+ close: () => {},
30
+ isOpen: () => true,
31
+ }
32
+ host.backgroundStarted()
33
+ const session = new MiniappSession({transport, packageName: "com.example.sim"})
34
+ await session.connect()
35
+ const effects = (kind: string) => events.flatMap((event) => (event.type === "effect" && event.effect.kind === kind ? [event.effect] : []))
36
+ return {session, effects}
37
+ }
38
+
39
+ // 16-bit mono PCM: 3200 bytes at 16 kHz is 100 ms of audio.
40
+ const pcm = (bytes: number) => new Uint8Array(bytes)
41
+
42
+ describe("speaker streams", () => {
43
+ test("a stream reports its backlog, drains on close and reports each state", async () => {
44
+ const {session, effects} = await connect()
45
+ const states: string[] = []
46
+ session.speaker.onStateChange((event) => states.push(event.state))
47
+ const stream = await session.speaker.createStream({sampleRate: 16000})
48
+ const {bufferedMs} = await stream.write(pcm(3200))
49
+ expect(bufferedMs).toBeGreaterThan(50)
50
+ expect(bufferedMs).toBeLessThanOrEqual(100)
51
+ const started = Date.now()
52
+ expect(await stream.close()).toEqual({durationMs: 100})
53
+ expect(Date.now() - started).toBeGreaterThanOrEqual(80)
54
+ expect(states).toEqual(["loading", "playing", "stopped"])
55
+ expect(effects("speaker_stream_open")[0]).toMatchObject({sampleRate: 16000, channels: 1, volume: 1})
56
+ })
57
+
58
+ test("a write holds its reply while more than 2 s is buffered", async () => {
59
+ const {session} = await connect()
60
+ const stream = await session.speaker.createStream()
61
+ const started = Date.now()
62
+ const {bufferedMs} = await stream.write(pcm(80000))
63
+ expect(Date.now() - started).toBeGreaterThanOrEqual(450)
64
+ expect(bufferedMs).toBeLessThanOrEqual(2000)
65
+ expect(bufferedMs).toBeGreaterThan(1500)
66
+ await stream.abort()
67
+ })
68
+
69
+ test("rejects a sample rate the phone does not play", async () => {
70
+ const {session} = await connect()
71
+ await expect(session.speaker.createStream({sampleRate: 44100 as 16000})).rejects.toMatchObject({
72
+ code: "INVALID_ARGUMENT",
73
+ message: "unsupported sampleRate 44100 (use 16000, 24000, or 48000)",
74
+ })
75
+ })
76
+
77
+ test("opening a second stream closes the first", async () => {
78
+ const {session} = await connect()
79
+ const first = await session.speaker.createStream()
80
+ await session.speaker.createStream()
81
+ await expect(first.write(pcm(3200))).rejects.toMatchObject({code: "INTERNAL", message: "speaker stream is not open"})
82
+ })
83
+ })
84
+
85
+ describe("glasses sensors", () => {
86
+ test("the compass needs Glassly custom firmware", async () => {
87
+ const stock = await connect({}, "g2")
88
+ await expect(stock.session.compass.start({intervalMs: 500})).rejects.toMatchObject({
89
+ code: "INTERNAL",
90
+ message:
91
+ "Sensor command failed: the glasses need Glassly custom firmware with this sensor, and on the G2 an active miniapp display",
92
+ })
93
+ const cfw = await connect({}, "g2-cfw")
94
+ await cfw.session.compass.start({intervalMs: 500})
95
+ expect(cfw.effects("glasses_sensor")[0]).toMatchObject({sensor: "compass", operation: "start", options: {intervalMs: 500, minChangeDegrees: 5}})
96
+ })
97
+
98
+ test("rejects options outside the sensor's range", async () => {
99
+ const {session} = await connect({}, "g2-cfw")
100
+ await expect(session.lightSensor.start({intervalMs: 50})).rejects.toMatchObject({code: "INVALID_ARGUMENT", message: "Invalid intervalMs"})
101
+ })
102
+ })
103
+
104
+ describe("mic gates", () => {
105
+ test("need MICROPHONE", async () => {
106
+ const {session} = await connect()
107
+ await expect(session.mic.setLoudnessGateEnabled(false)).rejects.toMatchObject({
108
+ code: "PERMISSION_NOT_DECLARED",
109
+ message: 'MICROPHONE permission not declared in miniapp.json. Add {"type": "MICROPHONE"} to the "permissions" array.',
110
+ })
111
+ })
112
+
113
+ test("the loudness gate applies on G2 custom firmware and is a no-op on stock G2", async () => {
114
+ const manifest = {permissions: [{type: "MICROPHONE"}]}
115
+ const cfw = await connect(manifest, "g2-cfw")
116
+ await cfw.session.mic.setLoudnessGateEnabled(false)
117
+ expect(cfw.effects("mic_loudness_gate")).toMatchObject([{enabled: false, applies: true}])
118
+ const stock = await connect(manifest, "g2")
119
+ await stock.session.mic.setLoudnessGateEnabled(false)
120
+ expect(stock.effects("mic_loudness_gate")).toMatchObject([{enabled: false, applies: false}])
121
+ })
122
+ })