@glassly/miniapp-cli 0.1.14 → 0.1.15-beta.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/README.md +3 -1
- package/package.json +1 -1
- package/src/api.ts +2 -1
- package/src/sim/client/app.css +5 -0
- package/src/sim/client/main.ts +7 -2
- package/src/sim/device.test.ts +59 -12
- package/src/sim/device.ts +184 -51
- package/src/sim/host.test.ts +369 -10
- package/src/sim/host.ts +664 -88
- package/src/sim/models.json +599 -0
- package/src/sim/regenerate-models.ts +34 -0
- package/src/sim/registry.ts +394 -0
- package/src/sim/scenarios.test.ts +74 -0
- package/src/sim/script.test.ts +31 -0
- package/src/sim/script.ts +48 -0
- package/src/sim/server.ts +49 -1
- package/src/sim/shots.test.ts +59 -0
- package/src/sim/shots.ts +161 -0
- package/src/sim/sim.ts +114 -12
- package/src/sim/storage.ts +8 -7
package/src/sim/host.test.ts
CHANGED
|
@@ -1,20 +1,33 @@
|
|
|
1
1
|
/// <reference types="bun-types" />
|
|
2
2
|
|
|
3
3
|
import {describe, expect, test} from "bun:test"
|
|
4
|
+
import {mkdtempSync} from "fs"
|
|
5
|
+
import {tmpdir} from "os"
|
|
6
|
+
import {join} from "path"
|
|
4
7
|
import {MiniappSession, type Transport} from "@glassly/miniapp"
|
|
5
8
|
|
|
6
9
|
import {simDevice} from "./device.js"
|
|
10
|
+
import {parseScript} from "./script.js"
|
|
7
11
|
import {SimHost, type SimHostEvent, type SimManifest} from "./host.js"
|
|
8
|
-
import {
|
|
12
|
+
import {simStateDir} from "./sim.js"
|
|
13
|
+
import {SimRegistry} from "./registry.js"
|
|
14
|
+
import {createSimStorage, type SimStorage} from "./storage.js"
|
|
9
15
|
|
|
10
16
|
/** A real SDK session wired straight to the host, standing in for the worker bridge. */
|
|
11
|
-
async function connect(
|
|
17
|
+
async function connect(
|
|
18
|
+
manifest: Partial<SimManifest> = {},
|
|
19
|
+
device = "simulated",
|
|
20
|
+
stores: {storage?: SimStorage; settings?: SimStorage; memory?: SimStorage; registry?: SimRegistry} = {},
|
|
21
|
+
) {
|
|
12
22
|
const events: SimHostEvent[] = []
|
|
13
23
|
let deliver: (raw: string) => void = () => {}
|
|
14
24
|
const host = new SimHost({
|
|
15
25
|
manifest: {packageName: "com.example.sim", name: "Sim Test", ...manifest},
|
|
16
|
-
device: simDevice(),
|
|
17
|
-
storage: createSimStorage(),
|
|
26
|
+
device: simDevice(device),
|
|
27
|
+
storage: stores.storage ?? createSimStorage(),
|
|
28
|
+
settings: stores.settings,
|
|
29
|
+
memory: stores.memory,
|
|
30
|
+
registry: stores.registry,
|
|
18
31
|
toBackground: (raw) => queueMicrotask(() => deliver(raw)),
|
|
19
32
|
emit: (event) => events.push(event),
|
|
20
33
|
frameLatencyMs: 0,
|
|
@@ -31,7 +44,7 @@ async function connect(manifest: Partial<SimManifest> = {}) {
|
|
|
31
44
|
isOpen: () => true,
|
|
32
45
|
}
|
|
33
46
|
host.backgroundStarted()
|
|
34
|
-
const session = new MiniappSession({transport, packageName: "com.example.sim"})
|
|
47
|
+
const session = new MiniappSession({transport, packageName: manifest.packageName ?? "com.example.sim"})
|
|
35
48
|
const errors: unknown[] = []
|
|
36
49
|
session.on("error", (error) => errors.push(error))
|
|
37
50
|
await session.connect()
|
|
@@ -50,6 +63,12 @@ describe("handshake", () => {
|
|
|
50
63
|
expect(session.permissions.has("microphone")).toBe(true)
|
|
51
64
|
expect(session.permissions.has("location")).toBe(false)
|
|
52
65
|
})
|
|
66
|
+
|
|
67
|
+
test("glasses without a display report a null display, as the phone does", async () => {
|
|
68
|
+
const {session} = await connect({}, "mentra-live")
|
|
69
|
+
expect(session.capabilities?.hasDisplay).toBe(false)
|
|
70
|
+
expect(session.capabilities?.display).toBeNull()
|
|
71
|
+
})
|
|
53
72
|
})
|
|
54
73
|
|
|
55
74
|
describe("display", () => {
|
|
@@ -94,8 +113,22 @@ describe("display", () => {
|
|
|
94
113
|
})
|
|
95
114
|
|
|
96
115
|
describe("input", () => {
|
|
97
|
-
test("a
|
|
116
|
+
test("a scoped claim stacks on the default one, and releasing it restores the default", async () => {
|
|
98
117
|
const {session, host} = await connect()
|
|
118
|
+
const changes: string[] = []
|
|
119
|
+
session.input.onClaimChange((change) => changes.push(`${change.gesture} ${change.held ? "held" : "released"}`))
|
|
120
|
+
await settle()
|
|
121
|
+
await session.input.setClaimedGestures(["single_tap"])
|
|
122
|
+
await session.input.setClaimedGestures(["double_tap"], {scope: "modal"})
|
|
123
|
+
expect([...(host.status().claimedGestures as string[])].sort()).toEqual(["double_tap", "single_tap"])
|
|
124
|
+
await session.input.setClaimedGestures([], {scope: "modal"})
|
|
125
|
+
await settle()
|
|
126
|
+
expect(host.status().claimedGestures).toEqual(["single_tap"])
|
|
127
|
+
expect(changes).toEqual(["single_tap held", "double_tap held", "double_tap released"])
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
test("a gesture reaches both the catch-all and the per-gesture listeners", async () => {
|
|
131
|
+
const {session, host} = await connect({}, "g2")
|
|
99
132
|
const all: string[] = []
|
|
100
133
|
const taps: string[] = []
|
|
101
134
|
const head: string[] = []
|
|
@@ -134,7 +167,74 @@ describe("input", () => {
|
|
|
134
167
|
})
|
|
135
168
|
})
|
|
136
169
|
|
|
170
|
+
describe("devices", () => {
|
|
171
|
+
test("a gesture the glasses never send is refused with the ones they do", async () => {
|
|
172
|
+
const {host} = await connect({}, "mentra-display")
|
|
173
|
+
expect(() => host.touch("single_tap")).toThrow('Mentra Display does not send "single_tap". It sends: head_up')
|
|
174
|
+
expect(() => host.button()).toThrow("Mentra Display has no button that sends presses")
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
test("text-only glasses show the scene as one wrapped block of text, without shapes", async () => {
|
|
178
|
+
const {session, host} = await connect({}, "g1")
|
|
179
|
+
const result = await session.display.render([
|
|
180
|
+
{type: "text", id: "second", box: {x: 0, y: 100, w: 300, h: 40}, text: "World"},
|
|
181
|
+
{type: "text", id: "first", box: {x: 0, y: 0, w: 300, h: 40}, text: "Hello"},
|
|
182
|
+
{type: "rect", id: "frame", box: {x: 0, y: 0, w: 100, h: 100}},
|
|
183
|
+
])
|
|
184
|
+
expect(result).toEqual({status: "displayed", degraded: false, dropped: []})
|
|
185
|
+
const content = host.snapshot().content
|
|
186
|
+
if (content.kind !== "scene") throw new Error(`expected a scene, got ${content.kind}`)
|
|
187
|
+
expect(content.elements.map((el) => [el.type, el.text, el.box])).toEqual([
|
|
188
|
+
["text", "Hello\n\nWorld", {x: 0, y: 0, w: 640, h: 200}],
|
|
189
|
+
])
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
test("text-only glasses wrap a long line at the model's width", async () => {
|
|
193
|
+
const {session, host} = await connect({}, "g1")
|
|
194
|
+
await session.display.render([{type: "text", id: "t", box: {x: 0, y: 0, w: 640, h: 200}, text: "word ".repeat(60)}])
|
|
195
|
+
const content = host.snapshot().content
|
|
196
|
+
if (content.kind !== "scene") throw new Error(`expected a scene, got ${content.kind}`)
|
|
197
|
+
const lines = content.elements[0].text?.split("\n") ?? []
|
|
198
|
+
expect(lines.length).toBe(5)
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
test("stock RayNeo iO shows only the frame's text, joined in paint order", async () => {
|
|
202
|
+
const {session, host} = await connect({}, "rayneo-io")
|
|
203
|
+
await session.display.render([
|
|
204
|
+
{type: "text", id: "b", box: {x: 300, y: 0, w: 200, h: 40}, text: "two"},
|
|
205
|
+
{type: "rect", id: "r", box: {x: 0, y: 60, w: 100, h: 40}},
|
|
206
|
+
{type: "text", id: "a", box: {x: 0, y: 0, w: 200, h: 40}, text: "one"},
|
|
207
|
+
])
|
|
208
|
+
const content = host.snapshot().content
|
|
209
|
+
if (content.kind !== "scene") throw new Error(`expected a scene, got ${content.kind}`)
|
|
210
|
+
expect(content.elements.map((el) => [el.type, el.text])).toEqual([["text", "two\none"]])
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
test("stock G2 keeps six text and rect elements", async () => {
|
|
214
|
+
const {session} = await connect({}, "g2")
|
|
215
|
+
const rows = Array.from({length: 8}, (_, i) => ({type: "text" as const, id: `t${i}`, box: {x: 0, y: i * 30, w: 200, h: 30}, text: `row ${i}`}))
|
|
216
|
+
const result = await session.display.render([...rows, {type: "circle", id: "c", box: {x: 300, y: 0, w: 40, h: 40}}])
|
|
217
|
+
expect(result.dropped).toEqual(["t6", "t7", "c"])
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
test("glasses without a display answer that none is connected", async () => {
|
|
221
|
+
const {session, host} = await connect({}, "mentra-live")
|
|
222
|
+
const result = await session.display.render([{type: "text", id: "t", box, text: "hello"}])
|
|
223
|
+
expect(result).toEqual({status: "blocked", reason: "no display connected"})
|
|
224
|
+
expect(host.snapshot()).toMatchObject({canvas: null, content: {kind: "unavailable", reason: "no-display"}})
|
|
225
|
+
})
|
|
226
|
+
})
|
|
227
|
+
|
|
137
228
|
describe("speech", () => {
|
|
229
|
+
test("language hints are applied instead of hanging", async () => {
|
|
230
|
+
const {session} = await connect({permissions: [{type: "MICROPHONE"}]})
|
|
231
|
+
const reply = await Promise.race([
|
|
232
|
+
session.transcription.configure({languageHints: ["en"]}),
|
|
233
|
+
Bun.sleep(1000).then(() => "timed out"),
|
|
234
|
+
])
|
|
235
|
+
expect(reply).toBeUndefined()
|
|
236
|
+
})
|
|
237
|
+
|
|
138
238
|
test("typed speech arrives as a transcription", async () => {
|
|
139
239
|
const {session, host} = await connect({permissions: [{type: "MICROPHONE"}]})
|
|
140
240
|
const heard: {text: string; isFinal: boolean}[] = []
|
|
@@ -168,6 +268,59 @@ describe("speech", () => {
|
|
|
168
268
|
expect(logs().some((line) => line.includes("MICROPHONE permission not declared"))).toBe(true)
|
|
169
269
|
})
|
|
170
270
|
|
|
271
|
+
test("a played line grows in place under one utterance id, then ends on one final", async () => {
|
|
272
|
+
const {session, host} = await connect({permissions: [{type: "MICROPHONE"}]})
|
|
273
|
+
const heard: {text: string; isFinal: boolean; utteranceId?: string; speakerId?: string; transcribeLanguage?: string}[] = []
|
|
274
|
+
session.transcription.on((data) => heard.push(data as (typeof heard)[number]))
|
|
275
|
+
await settle()
|
|
276
|
+
await host.play(parseScript("A (en): turn on the lights\nB: thank you"), {wordsPerMinute: 60_000, gapMs: 0})
|
|
277
|
+
expect(heard.map(({text, isFinal, speakerId, transcribeLanguage}) => [text, isFinal, speakerId, transcribeLanguage])).toEqual([
|
|
278
|
+
["turn", false, "1", "en"],
|
|
279
|
+
["turn on", false, "1", "en"],
|
|
280
|
+
["turn on the", false, "1", "en"],
|
|
281
|
+
["turn on the lights", false, "1", "en"],
|
|
282
|
+
["turn on the lights", true, "1", "en"],
|
|
283
|
+
["thank", false, "2", "en"],
|
|
284
|
+
["thank you", false, "2", "en"],
|
|
285
|
+
["thank you", true, "2", "en"],
|
|
286
|
+
])
|
|
287
|
+
expect(new Set(heard.slice(0, 5).map((data) => data.utteranceId)).size).toBe(1)
|
|
288
|
+
expect(heard[5].utteranceId).not.toBe(heard[0].utteranceId)
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
test("a played line reaches a translation target with its original text, and same-language speech passes through", async () => {
|
|
292
|
+
const {session, host} = await connect({permissions: [{type: "MICROPHONE"}]})
|
|
293
|
+
const finals: Record<string, unknown>[] = []
|
|
294
|
+
session.translation.to("en-US", (data) => {
|
|
295
|
+
if (data.isFinal) finals.push(data as unknown as Record<string, unknown>)
|
|
296
|
+
})
|
|
297
|
+
await settle()
|
|
298
|
+
await host.play(parseScript("B (es): Está a dos cuadras. | en: It's two blocks away.\nA (en): Thanks"), {
|
|
299
|
+
wordsPerMinute: 60_000,
|
|
300
|
+
gapMs: 0,
|
|
301
|
+
})
|
|
302
|
+
expect(finals.map(({text, originalText, transcribeLanguage, translateLanguage, didTranslate}) => ({
|
|
303
|
+
text,
|
|
304
|
+
originalText,
|
|
305
|
+
transcribeLanguage,
|
|
306
|
+
translateLanguage,
|
|
307
|
+
didTranslate,
|
|
308
|
+
}))).toEqual([
|
|
309
|
+
{text: "It's two blocks away.", originalText: "Está a dos cuadras.", transcribeLanguage: "es", translateLanguage: "en-US", didTranslate: true},
|
|
310
|
+
{text: "Thanks", originalText: undefined, transcribeLanguage: "en", translateLanguage: "en-US", didTranslate: true},
|
|
311
|
+
])
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
test("typed translations reach every subscribed target by default", async () => {
|
|
315
|
+
const {session, host} = await connect({permissions: [{type: "MICROPHONE"}]})
|
|
316
|
+
const heard: string[] = []
|
|
317
|
+
session.translation.to("en-US", (data) => heard.push(`${data.text}|${(data as {utteranceId?: string}).utteranceId}`))
|
|
318
|
+
await settle()
|
|
319
|
+
host.translation("Two blocks away", {originalText: "A dos cuadras"})
|
|
320
|
+
await settle()
|
|
321
|
+
expect(heard).toEqual(["Two blocks away|utt_sim_tr_1"])
|
|
322
|
+
})
|
|
323
|
+
|
|
171
324
|
test("speak() is recorded and resolves once the utterance ends", async () => {
|
|
172
325
|
const {session, events} = await connect()
|
|
173
326
|
const result = await session.speaker.speak("hi there")
|
|
@@ -187,10 +340,7 @@ describe("phone", () => {
|
|
|
187
340
|
expect(await session.storage.get("notes")).toBeNull()
|
|
188
341
|
})
|
|
189
342
|
|
|
190
|
-
test("location
|
|
191
|
-
const denied = await connect()
|
|
192
|
-
await expect(denied.session.location.getOnce()).rejects.toMatchObject({code: "PERMISSION_NOT_DECLARED"})
|
|
193
|
-
|
|
343
|
+
test("location follows the simulated phone", async () => {
|
|
194
344
|
const {session, host} = await connect({permissions: [{type: "LOCATION"}]})
|
|
195
345
|
const fixes: number[] = []
|
|
196
346
|
session.location.onUpdate((fix) => fixes.push(fix.lat))
|
|
@@ -259,3 +409,212 @@ describe("phone page bridge", () => {
|
|
|
259
409
|
expect(seen).toEqual(["open", "close"])
|
|
260
410
|
})
|
|
261
411
|
})
|
|
412
|
+
|
|
413
|
+
describe("daily state", () => {
|
|
414
|
+
test("a note survives a simulator restart and reset clears it", async () => {
|
|
415
|
+
const dir = mkdtempSync(join(tmpdir(), "glassly-sim-test-"))
|
|
416
|
+
const files = () => ({memory: createSimStorage(join(dir, "memory.json"))})
|
|
417
|
+
const manifest = {permissions: [{type: "MEMORY"}]}
|
|
418
|
+
const first = await connect(manifest, "simulated", files())
|
|
419
|
+
await first.session.memory.addNote("buy milk")
|
|
420
|
+
|
|
421
|
+
const second = await connect(manifest, "simulated", files())
|
|
422
|
+
expect((await second.session.memory.query({contains: "milk"})).map((entry) => entry.text)).toEqual(["buy milk"])
|
|
423
|
+
await second.host.reset()
|
|
424
|
+
expect(await second.session.memory.query({contains: "milk"})).toEqual([])
|
|
425
|
+
})
|
|
426
|
+
|
|
427
|
+
test("two copies of one miniapp keep separate state folders", () => {
|
|
428
|
+
expect(simStateDir("com.example.notes", "/work/a")).not.toBe(simStateDir("com.example.notes", "/work/b"))
|
|
429
|
+
expect(simStateDir("com.example.notes", "/work/a")).toBe(simStateDir("com.example.notes", "/work/a/"))
|
|
430
|
+
})
|
|
431
|
+
|
|
432
|
+
test("derived settings push a change when the phone goes to the background", async () => {
|
|
433
|
+
const {session, host} = await connect({permissions: [{type: "PLATFORM_SETTINGS"}]})
|
|
434
|
+
const changes: unknown[] = []
|
|
435
|
+
session.settings.onChange((change) => changes.push(change))
|
|
436
|
+
await settle()
|
|
437
|
+
changes.length = 0
|
|
438
|
+
host.setVisibility("background")
|
|
439
|
+
await settle()
|
|
440
|
+
expect(changes).toEqual([{key: "phone_active", value: false}])
|
|
441
|
+
})
|
|
442
|
+
|
|
443
|
+
test("a transcript line that cannot be saved fails instead of answering ok", async () => {
|
|
444
|
+
const broken: SimStorage = {...createSimStorage(), set: () => {
|
|
445
|
+
throw new Error("EISDIR: illegal operation on a directory")
|
|
446
|
+
}}
|
|
447
|
+
const {session} = await connect({permissions: [{type: "MEMORY"}]}, "simulated", {memory: broken})
|
|
448
|
+
await expect(session.memory.appendLine({text: "hello", speakerId: null, language: null, at: 1_790_000_000_000})).rejects.toMatchObject({
|
|
449
|
+
code: "INTERNAL",
|
|
450
|
+
})
|
|
451
|
+
})
|
|
452
|
+
|
|
453
|
+
test("a written setting reaches subscribed miniapps and survives a restart", async () => {
|
|
454
|
+
const dir = mkdtempSync(join(tmpdir(), "glassly-sim-test-"))
|
|
455
|
+
const settings = () => createSimStorage(join(dir, "settings.json"))
|
|
456
|
+
const manifest = {permissions: [{type: "PLATFORM_SETTINGS"}]}
|
|
457
|
+
const {session} = await connect(manifest, "simulated", {settings: settings()})
|
|
458
|
+
const changes: unknown[] = []
|
|
459
|
+
session.settings.onChange((change) => changes.push(change))
|
|
460
|
+
await settle()
|
|
461
|
+
changes.length = 0
|
|
462
|
+
await session.settings.set("head_up_angle", 30)
|
|
463
|
+
await settle()
|
|
464
|
+
expect(changes).toEqual([{key: "head_up_angle", value: 30}])
|
|
465
|
+
|
|
466
|
+
const again = await connect(manifest, "simulated", {settings: settings()})
|
|
467
|
+
expect(await again.session.settings.get("head_up_angle")).toBe(30)
|
|
468
|
+
})
|
|
469
|
+
|
|
470
|
+
test("a dashboard miniapp gets the dashboard view only on glasses with custom firmware", async () => {
|
|
471
|
+
for (const [device, views] of [
|
|
472
|
+
["g2-cfw", ["main", "dashboard"]],
|
|
473
|
+
["rayneo-io-cfw", ["main", "dashboard"]],
|
|
474
|
+
["nimo-cfw", ["main", "dashboard"]],
|
|
475
|
+
["memomind-one", ["main", "dashboard"]],
|
|
476
|
+
["g2", ["main"]],
|
|
477
|
+
["g1", ["main"]],
|
|
478
|
+
] as [string, string[]][]) {
|
|
479
|
+
const {session} = await connect({type: "dashboard"}, device)
|
|
480
|
+
expect([device, session.capabilities?.display?.views as string[] | undefined]).toEqual([device, views])
|
|
481
|
+
}
|
|
482
|
+
})
|
|
483
|
+
})
|
|
484
|
+
|
|
485
|
+
describe("calling the miniapp", () => {
|
|
486
|
+
const actions = [{id: "set_language", description: "Set the language", parameters: {type: "object"}}]
|
|
487
|
+
|
|
488
|
+
test("a declared action runs and answers", async () => {
|
|
489
|
+
const {session, host} = await connect({actions})
|
|
490
|
+
session.actions.handle("set_language", async (params) => ({applied: params.language}))
|
|
491
|
+
await expect(host.callAction("set_language", {language: "fr"})).resolves.toEqual({applied: "fr"})
|
|
492
|
+
await expect(host.callAction("nope")).rejects.toThrow('ACTION_NOT_FOUND: com.example.sim does not declare action "nope"')
|
|
493
|
+
})
|
|
494
|
+
|
|
495
|
+
test("a placed widget answers with the card it draws, and events with its result", async () => {
|
|
496
|
+
const widgets = [{id: "headlines", name: "Headlines", sizes: ["MD"]}]
|
|
497
|
+
const {session, host} = await connect({widgets})
|
|
498
|
+
const handle = session.widgets.provide("headlines", {
|
|
499
|
+
onBox: (box) => handle.update([{type: "text", id: "t", box: {x: 0, y: 0, w: box?.width ?? 0, h: 20}, text: "Top story"}]),
|
|
500
|
+
onEvent: () => ({handled: true}),
|
|
501
|
+
})
|
|
502
|
+
const frame = (await host.callWidget("headlines", {box: {size: "MD", width: 251, height: 120}})) as {
|
|
503
|
+
elements: {text?: string}[]
|
|
504
|
+
}
|
|
505
|
+
expect(frame.elements.map((el) => el.text)).toEqual(["Top story"])
|
|
506
|
+
await expect(host.callWidget("headlines", {event: {type: "tap"}})).resolves.toEqual({handled: true})
|
|
507
|
+
await expect(host.callWidget("weather", {event: {}})).rejects.toThrow('WIDGET_NOT_FOUND: com.example.sim does not declare widget "weather"')
|
|
508
|
+
})
|
|
509
|
+
|
|
510
|
+
test("a widget event handler's error reaches the caller with its own message", async () => {
|
|
511
|
+
const widgets = [{id: "headlines", name: "Headlines", sizes: ["MD"]}]
|
|
512
|
+
const {session, host} = await connect({widgets})
|
|
513
|
+
session.widgets.provide("headlines", {
|
|
514
|
+
onEvent: () => {
|
|
515
|
+
throw new Error("boom from provider")
|
|
516
|
+
},
|
|
517
|
+
})
|
|
518
|
+
await expect(host.callWidget("headlines", {event: {type: "tap"}})).rejects.toThrow("INTERNAL: boom from provider")
|
|
519
|
+
})
|
|
520
|
+
})
|
|
521
|
+
|
|
522
|
+
describe("several miniapps", () => {
|
|
523
|
+
test("a dashboard lists, places and shows another miniapp's widget", async () => {
|
|
524
|
+
const registry = new SimRegistry()
|
|
525
|
+
const news = await connect({packageName: "com.example.news", widgets: [{id: "headlines", name: "Headlines", sizes: ["MD"]}]}, "g2-cfw", {registry})
|
|
526
|
+
const handle = news.session.widgets.provide("headlines", {
|
|
527
|
+
onBox: (box) => handle.update([{type: "text", id: "h", box: {x: 0, y: 0, w: box?.width ?? 0, h: 27}, text: "Top story"}]),
|
|
528
|
+
})
|
|
529
|
+
const dashboard = await connect({packageName: "com.example.dash", type: "dashboard", permissions: [{type: "HOST_WIDGETS"}]}, "g2-cfw", {registry})
|
|
530
|
+
const frames: string[] = []
|
|
531
|
+
dashboard.session.widgets.onFrame((frame) => frames.push(`${frame.packageName}/${frame.widgetId}: ${frame.elements.map((el) => (el as {text?: string}).text).join()}`))
|
|
532
|
+
|
|
533
|
+
expect((await dashboard.session.widgets.list()).map((row) => [row.packageName, row.id, row.running])).toEqual([["com.example.news", "headlines", true]])
|
|
534
|
+
await dashboard.session.widgets.place("com.example.news", "headlines", {size: "MD", width: 251, height: 120})
|
|
535
|
+
await settle()
|
|
536
|
+
expect(frames).toEqual(["com.example.news/headlines: Top story"])
|
|
537
|
+
})
|
|
538
|
+
|
|
539
|
+
test("one miniapp invokes another's action and gets its answer", async () => {
|
|
540
|
+
const registry = new SimRegistry()
|
|
541
|
+
const notes = await connect({packageName: "com.example.notes", actions: [{id: "add_note", description: "Add a note"}]}, "simulated", {registry})
|
|
542
|
+
const added: unknown[] = []
|
|
543
|
+
notes.session.actions.handle("add_note", async (params, ctx) => {
|
|
544
|
+
added.push([params.text, ctx.callerPackageName])
|
|
545
|
+
return {status: "saved"}
|
|
546
|
+
})
|
|
547
|
+
const assistant = await connect({packageName: "com.example.assistant"}, "simulated", {registry})
|
|
548
|
+
await expect(assistant.session.actions.invoke("com.example.notes", "add_note", {text: "buy milk"})).resolves.toEqual({status: "saved"})
|
|
549
|
+
expect(added).toEqual([["buy milk", "com.example.assistant"]])
|
|
550
|
+
expect((await assistant.session.actions.list()).map((row) => [row.packageName, row.id])).toEqual([["com.example.notes", "add_note"]])
|
|
551
|
+
})
|
|
552
|
+
|
|
553
|
+
test("the CLI calls an action and a widget of a miniapp installed beside the one under test", async () => {
|
|
554
|
+
const registry = new SimRegistry()
|
|
555
|
+
const notes = await connect(
|
|
556
|
+
{packageName: "com.example.notes", actions: [{id: "add_note", description: "Add a note"}], widgets: [{id: "list", name: "List", sizes: ["MD"]}]},
|
|
557
|
+
"simulated",
|
|
558
|
+
{registry},
|
|
559
|
+
)
|
|
560
|
+
notes.session.actions.handle("add_note", async (params) => ({saved: params.text}))
|
|
561
|
+
notes.session.widgets.provide("list", {onEvent: () => ({handled: true})})
|
|
562
|
+
const main = await connect({packageName: "com.example.dash"}, "simulated", {registry})
|
|
563
|
+
|
|
564
|
+
await expect(main.host.callAction("add_note", {text: "milk"}, undefined, "com.example.notes")).resolves.toEqual({saved: "milk"})
|
|
565
|
+
await expect(main.host.callWidget("list", {event: {type: "tap"}}, undefined, "com.example.notes")).resolves.toEqual({handled: true})
|
|
566
|
+
await expect(main.host.callAction("add_note", {}, undefined, "com.example.nope")).rejects.toThrow("APP_NOT_FOUND: Miniapp not found: com.example.nope")
|
|
567
|
+
})
|
|
568
|
+
|
|
569
|
+
test("a miniapp with MANAGE_MINIAPPS lists, stops and starts another", async () => {
|
|
570
|
+
let other: Awaited<ReturnType<typeof connect>> | null = null
|
|
571
|
+
const registry = new SimRegistry({
|
|
572
|
+
launch: () => void connect({packageName: "com.example.other"}, "simulated", {registry}).then((app) => (other = app)),
|
|
573
|
+
halt: () => other?.host.backgroundStopped(false),
|
|
574
|
+
})
|
|
575
|
+
other = await connect({packageName: "com.example.other"}, "simulated", {registry})
|
|
576
|
+
const manager = await connect({packageName: "com.example.manager", permissions: [{type: "MANAGE_MINIAPPS"}]}, "simulated", {registry})
|
|
577
|
+
const running = async () => (await manager.session.miniapps.list()).map((app) => [app.packageName, app.running])
|
|
578
|
+
|
|
579
|
+
expect(await running()).toEqual([["com.example.other", true]])
|
|
580
|
+
await manager.session.miniapps.stop("com.example.other")
|
|
581
|
+
expect(await running()).toEqual([["com.example.other", false]])
|
|
582
|
+
await manager.session.miniapps.start("com.example.other")
|
|
583
|
+
expect(await running()).toEqual([["com.example.other", true]])
|
|
584
|
+
})
|
|
585
|
+
|
|
586
|
+
test("widget calls are checked as the phone checks them", async () => {
|
|
587
|
+
const widgets = [{id: "headlines", name: "Headlines", sizes: ["MD"]}]
|
|
588
|
+
const {session, host} = await connect({widgets})
|
|
589
|
+
const handle = session.widgets.provide("headlines", {
|
|
590
|
+
onBox: (box) => {
|
|
591
|
+
const count = box?.width === 300 ? 65 : 1
|
|
592
|
+
handle.update(Array.from({length: count}, (_, i) => ({type: "text", id: `t${i}`, box: {x: 0, y: 0, w: 10, h: 10}, text: "x"})))
|
|
593
|
+
},
|
|
594
|
+
onEvent: () => ({handled: true, padding: "x".repeat(262_145)}),
|
|
595
|
+
})
|
|
596
|
+
await expect(host.callWidget("headlines", {box: {size: "MD", width: -1, height: 120}})).rejects.toThrow(
|
|
597
|
+
"INVALID_ARGUMENT: place needs a box {size, width, height}",
|
|
598
|
+
)
|
|
599
|
+
await expect(host.callWidget("headlines", {event: {}})).rejects.toThrow("INVALID_ARGUMENT: send needs an event with a string type")
|
|
600
|
+
await expect(host.callWidget("headlines", {event: {type: "tap"}})).rejects.toThrow(
|
|
601
|
+
"PAYLOAD_TOO_LARGE: widget event result exceeded the 256 KB cap",
|
|
602
|
+
)
|
|
603
|
+
await expect(host.callWidget("headlines", {box: {size: "MD", width: 300, height: 120}}, 100)).rejects.toThrow(
|
|
604
|
+
'No card frame for widget "headlines" within 100 ms',
|
|
605
|
+
)
|
|
606
|
+
})
|
|
607
|
+
|
|
608
|
+
test("a second placement of a widget waits its turn instead of taking the first one's frame", async () => {
|
|
609
|
+
const widgets = [{id: "headlines", name: "Headlines", sizes: ["MD"]}]
|
|
610
|
+
const {session, host} = await connect({widgets})
|
|
611
|
+
const handle = session.widgets.provide("headlines", {
|
|
612
|
+
onBox: (box) => setTimeout(() => handle.update([{type: "text", id: "w", box: {x: 0, y: 0, w: box?.width ?? 0, h: 10}, text: String(box?.width)}]), 20),
|
|
613
|
+
})
|
|
614
|
+
const first = host.callWidget("headlines", {box: {size: "MD", width: 100, height: 120}})
|
|
615
|
+
await expect(host.callWidget("headlines", {box: {size: "MD", width: 200, height: 120}})).rejects.toThrow(
|
|
616
|
+
'Widget "headlines" is still waiting for the frame of an earlier placement',
|
|
617
|
+
)
|
|
618
|
+
expect(((await first) as {elements: {text: string}[]}).elements[0].text).toBe("100")
|
|
619
|
+
})
|
|
620
|
+
})
|