@glassly/miniapp-cli 0.1.10 → 0.1.11-dev.0
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 +41 -0
- package/package.json +2 -1
- package/src/api.ts +11 -0
- package/src/dev.ts +7 -4
- package/src/sim/background-worker.ts +112 -0
- package/src/sim/background.ts +52 -0
- package/src/sim/client/app.css +450 -0
- package/src/sim/client/index.html +118 -0
- package/src/sim/client/main.ts +249 -0
- package/src/sim/control.ts +96 -0
- package/src/sim/device.test.ts +20 -0
- package/src/sim/device.ts +68 -0
- package/src/sim/host.test.ts +250 -0
- package/src/sim/host.ts +952 -0
- package/src/sim/screenshot.ts +61 -0
- package/src/sim/server.ts +412 -0
- package/src/sim/sim.ts +333 -0
- package/src/sim/storage.ts +46 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import {MirrorCanvasRenderer, type MirrorSnapshot} from "@glassly/miniapp/mirror"
|
|
2
|
+
|
|
3
|
+
interface LogEntry {
|
|
4
|
+
seq: number
|
|
5
|
+
timestamp: number
|
|
6
|
+
source: string
|
|
7
|
+
level: string
|
|
8
|
+
message: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface Status {
|
|
12
|
+
packageName: string
|
|
13
|
+
name: string
|
|
14
|
+
version?: string
|
|
15
|
+
background: string
|
|
16
|
+
connected: boolean
|
|
17
|
+
paused: boolean
|
|
18
|
+
subscriptions: string[]
|
|
19
|
+
claimedGestures: string[]
|
|
20
|
+
permissions: string[]
|
|
21
|
+
ui: {open: boolean; clients: number}
|
|
22
|
+
display: {view: string | null; frames: number; elements: number}
|
|
23
|
+
device: {model: string; canvas: {width: number; height: number} | null}
|
|
24
|
+
speaker: string
|
|
25
|
+
sensors: {glassesBattery: {level: number}; location: {lat: number; lng: number}; glassesConnected: boolean}
|
|
26
|
+
colorScheme: string
|
|
27
|
+
unsimulated: string[]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type ServerMessage =
|
|
31
|
+
| {type: "hello"; status: Status; snapshot: MirrorSnapshot; logs: LogEntry[]; hasUi: boolean}
|
|
32
|
+
| {type: "display"; snapshot: MirrorSnapshot}
|
|
33
|
+
| {type: "log"; entry: LogEntry}
|
|
34
|
+
| {type: "status"; status: Status}
|
|
35
|
+
| {type: "effect"; effect: {kind: string; text?: string}}
|
|
36
|
+
|
|
37
|
+
const LOG_LINES = 1500
|
|
38
|
+
|
|
39
|
+
const $ = <T extends HTMLElement>(id: string) => document.getElementById(id) as T
|
|
40
|
+
|
|
41
|
+
const renderer = new MirrorCanvasRenderer($<HTMLCanvasElement>("display"))
|
|
42
|
+
const logPane = $("tab-log")
|
|
43
|
+
const scenePane = $("tab-scene")
|
|
44
|
+
const statePane = $("tab-state")
|
|
45
|
+
const lensNote = $("lens-note")
|
|
46
|
+
const follow = $<HTMLInputElement>("follow")
|
|
47
|
+
const aloud = $<HTMLInputElement>("aloud")
|
|
48
|
+
|
|
49
|
+
async function post(route: string, body: Record<string, unknown> = {}): Promise<void> {
|
|
50
|
+
const response = await fetch(`/api/${route}`, {
|
|
51
|
+
method: "POST",
|
|
52
|
+
headers: {"content-type": "application/json"},
|
|
53
|
+
body: JSON.stringify(body),
|
|
54
|
+
})
|
|
55
|
+
if (response.ok) return
|
|
56
|
+
const {error} = (await response.json().catch(() => ({}))) as {error?: string}
|
|
57
|
+
appendLog({seq: 0, timestamp: Date.now(), source: "host", level: "error", message: error ?? `${route} failed`})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function time(timestamp: number): string {
|
|
61
|
+
const d = new Date(timestamp)
|
|
62
|
+
return [d.getHours(), d.getMinutes(), d.getSeconds()].map((n) => String(n).padStart(2, "0")).join(":")
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function appendLog(entry: LogEntry): void {
|
|
66
|
+
const line = document.createElement("div")
|
|
67
|
+
line.className = "log-line"
|
|
68
|
+
line.dataset.source = entry.source
|
|
69
|
+
line.dataset.level = entry.level
|
|
70
|
+
for (const [cls, text] of [
|
|
71
|
+
["time", time(entry.timestamp)],
|
|
72
|
+
["source", entry.source],
|
|
73
|
+
["message", entry.message],
|
|
74
|
+
]) {
|
|
75
|
+
const cell = document.createElement("span")
|
|
76
|
+
cell.className = cls
|
|
77
|
+
cell.textContent = text
|
|
78
|
+
line.append(cell)
|
|
79
|
+
}
|
|
80
|
+
logPane.append(line)
|
|
81
|
+
while (logPane.childElementCount > LOG_LINES) logPane.firstElementChild?.remove()
|
|
82
|
+
if (follow.checked) logPane.scrollTop = logPane.scrollHeight
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function describe(frame: MirrorSnapshot): string {
|
|
86
|
+
if (frame.content.kind !== "scene") return `(${frame.content.kind})`
|
|
87
|
+
return frame.content.elements
|
|
88
|
+
.map((el) => {
|
|
89
|
+
const box = `${el.box.x},${el.box.y} ${el.box.w}x${el.box.h}`
|
|
90
|
+
const detail = el.type === "text" ? JSON.stringify(el.text ?? "") : el.type === "image" ? "image" : ""
|
|
91
|
+
const style = el.style && Object.keys(el.style).length ? ` ${JSON.stringify(el.style)}` : ""
|
|
92
|
+
return `${el.type.padEnd(8)} ${el.id.padEnd(16)} @ ${box} ${detail}${style}`
|
|
93
|
+
})
|
|
94
|
+
.join("\n")
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function showDisplay(frame: MirrorSnapshot): void {
|
|
98
|
+
renderer.update(frame)
|
|
99
|
+
scenePane.textContent = describe(frame)
|
|
100
|
+
const count = frame.content.kind === "scene" ? frame.content.elements.length : 0
|
|
101
|
+
$("display-meta").textContent = `${frame.canvas ? `${frame.canvas.width}×${frame.canvas.height} · ` : ""}${count} elements`
|
|
102
|
+
lensNote.hidden = frame.connected
|
|
103
|
+
lensNote.textContent = frame.connected ? "" : "glasses disconnected"
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function showStatus(status: Status): void {
|
|
107
|
+
document.title = `${status.name} · Glassly Simulator`
|
|
108
|
+
$("app-name").textContent = status.version ? `${status.name} ${status.version}` : status.name
|
|
109
|
+
$("app-package").textContent = status.packageName
|
|
110
|
+
const state = $("run-state")
|
|
111
|
+
const label = status.background === "running" && !status.connected ? "starting" : status.background
|
|
112
|
+
state.dataset.state = status.background
|
|
113
|
+
state.textContent = status.paused && status.background === "running" ? "paused" : label
|
|
114
|
+
$("device-model").textContent = status.device.model
|
|
115
|
+
|
|
116
|
+
const rows: [string, string, boolean?][] = [
|
|
117
|
+
["subscriptions", status.subscriptions.join(", ") || "none"],
|
|
118
|
+
["claimed gestures", status.claimedGestures.join(", ") || "none"],
|
|
119
|
+
["declared permissions", status.permissions.join(", ") || "none"],
|
|
120
|
+
["phone page", status.ui.open ? `open (${status.ui.clients})` : "closed"],
|
|
121
|
+
["display", `${status.display.view ?? "no"} view, ${status.display.frames} frames rendered`],
|
|
122
|
+
["speaker", status.speaker],
|
|
123
|
+
]
|
|
124
|
+
if (status.unsimulated.length) rows.push(["not simulated", status.unsimulated.join(", "), true])
|
|
125
|
+
statePane.replaceChildren(
|
|
126
|
+
...rows.flatMap(([name, value, flag]) => {
|
|
127
|
+
const dt = document.createElement("dt")
|
|
128
|
+
dt.textContent = name
|
|
129
|
+
const dd = document.createElement("dd")
|
|
130
|
+
dd.textContent = value
|
|
131
|
+
if (flag) dd.className = "flag"
|
|
132
|
+
return [dt, dd]
|
|
133
|
+
}),
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function connect(): void {
|
|
138
|
+
const socket = new WebSocket(`${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/sim/socket`)
|
|
139
|
+
socket.onmessage = (event) => {
|
|
140
|
+
const message = JSON.parse(event.data as string) as ServerMessage
|
|
141
|
+
if (message.type === "hello") {
|
|
142
|
+
logPane.replaceChildren()
|
|
143
|
+
message.logs.forEach(appendLog)
|
|
144
|
+
showStatus(message.status)
|
|
145
|
+
showDisplay(message.snapshot)
|
|
146
|
+
const frame = $<HTMLIFrameElement>("phone-frame")
|
|
147
|
+
$("phone-empty").hidden = message.hasUi
|
|
148
|
+
$("phone-link").hidden = !message.hasUi
|
|
149
|
+
frame.hidden = !message.hasUi
|
|
150
|
+
if (message.hasUi && !frame.src) frame.src = "/ui/"
|
|
151
|
+
} else if (message.type === "display") showDisplay(message.snapshot)
|
|
152
|
+
else if (message.type === "log") appendLog(message.entry)
|
|
153
|
+
else if (message.type === "status") showStatus(message.status)
|
|
154
|
+
else if (message.type === "effect" && message.effect.kind === "speak" && aloud.checked && message.effect.text) {
|
|
155
|
+
speechSynthesis.cancel()
|
|
156
|
+
speechSynthesis.speak(new SpeechSynthesisUtterance(message.effect.text))
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
socket.onclose = () => {
|
|
160
|
+
const state = $("run-state")
|
|
161
|
+
state.dataset.state = "offline"
|
|
162
|
+
state.textContent = "simulator stopped"
|
|
163
|
+
setTimeout(connect, 1000)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function flash(button: Element | null): void {
|
|
168
|
+
if (!button) return
|
|
169
|
+
button.classList.add("pressed")
|
|
170
|
+
setTimeout(() => button.classList.remove("pressed"), 140)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
document.querySelectorAll<HTMLButtonElement>("[data-touch]").forEach((button) => {
|
|
174
|
+
button.addEventListener("click", () => void post("touch", {gesture: button.dataset.touch}))
|
|
175
|
+
})
|
|
176
|
+
document.querySelectorAll<HTMLButtonElement>("[data-button]").forEach((button) => {
|
|
177
|
+
button.addEventListener("click", () => void post("button", {pressType: button.dataset.button}))
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
const KEYS: Record<string, string> = {
|
|
181
|
+
t: '[data-touch="single_tap"]',
|
|
182
|
+
d: '[data-touch="double_tap"]',
|
|
183
|
+
h: '[data-touch="long_press"]',
|
|
184
|
+
ArrowUp: '[data-touch="swipe_up"]',
|
|
185
|
+
ArrowDown: '[data-touch="swipe_down"]',
|
|
186
|
+
u: '[data-touch="head_up"]',
|
|
187
|
+
j: '[data-touch="head_down"]',
|
|
188
|
+
b: '[data-button="short"]',
|
|
189
|
+
}
|
|
190
|
+
window.addEventListener("keydown", (event) => {
|
|
191
|
+
const target = event.target as HTMLElement | null
|
|
192
|
+
if (target && (target.tagName === "INPUT" || target.tagName === "TEXTAREA" || target.isContentEditable)) return
|
|
193
|
+
if (event.metaKey || event.ctrlKey || event.altKey) return
|
|
194
|
+
const selector = KEYS[event.key]
|
|
195
|
+
if (!selector) return
|
|
196
|
+
event.preventDefault()
|
|
197
|
+
const button = document.querySelector<HTMLButtonElement>(selector)
|
|
198
|
+
flash(button)
|
|
199
|
+
button?.click()
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
$("say").addEventListener("submit", (event) => {
|
|
203
|
+
event.preventDefault()
|
|
204
|
+
const input = $<HTMLInputElement>("say-text")
|
|
205
|
+
const text = input.value.trim()
|
|
206
|
+
if (!text) return
|
|
207
|
+
const isFinal = !$<HTMLInputElement>("say-interim").checked
|
|
208
|
+
void post("say", {text, isFinal, language: $<HTMLInputElement>("say-language").value.trim() || "en-US"})
|
|
209
|
+
if (isFinal) input.value = ""
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
$("notify").addEventListener("submit", (event) => {
|
|
213
|
+
event.preventDefault()
|
|
214
|
+
void post("notify", {
|
|
215
|
+
title: $<HTMLInputElement>("notify-title").value,
|
|
216
|
+
content: $<HTMLInputElement>("notify-content").value,
|
|
217
|
+
})
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
$("battery").addEventListener("change", (event) => {
|
|
221
|
+
void post("battery", {target: "glasses", level: Number((event.target as HTMLInputElement).value)})
|
|
222
|
+
})
|
|
223
|
+
$("send-location").addEventListener("click", () => {
|
|
224
|
+
void post("location", {lat: Number($<HTMLInputElement>("lat").value), lng: Number($<HTMLInputElement>("lng").value)})
|
|
225
|
+
})
|
|
226
|
+
$("connected").addEventListener("change", (event) => {
|
|
227
|
+
void post("connection", {connected: (event.target as HTMLInputElement).checked})
|
|
228
|
+
})
|
|
229
|
+
$("light").addEventListener("change", (event) => {
|
|
230
|
+
void post("color-scheme", {colorScheme: (event.target as HTMLInputElement).checked ? "light" : "dark"})
|
|
231
|
+
})
|
|
232
|
+
$("restart").addEventListener("click", () => void post("restart"))
|
|
233
|
+
$("clear-log").addEventListener("click", () => logPane.replaceChildren())
|
|
234
|
+
|
|
235
|
+
document.querySelectorAll<HTMLButtonElement>('[role="tab"]').forEach((tab) => {
|
|
236
|
+
tab.addEventListener("click", () => {
|
|
237
|
+
document.querySelectorAll('[role="tab"]').forEach((other) => other.setAttribute("aria-selected", String(other === tab)))
|
|
238
|
+
for (const name of ["log", "scene", "state"]) $(`tab-${name}`).hidden = name !== tab.dataset.tab
|
|
239
|
+
})
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
function fitPhone(): void {
|
|
243
|
+
const body = $("phone-body")
|
|
244
|
+
const room = (body.parentElement?.parentElement?.clientHeight ?? 700) - 70
|
|
245
|
+
body.style.setProperty("--scale", String(Math.max(0.45, Math.min(1, room / 852))))
|
|
246
|
+
}
|
|
247
|
+
window.addEventListener("resize", fitPhone)
|
|
248
|
+
fitPhone()
|
|
249
|
+
connect()
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {existsSync, writeFileSync} from "fs"
|
|
2
|
+
import {resolve} from "path"
|
|
3
|
+
|
|
4
|
+
import {listSimRecords, type SimRecord} from "./sim.js"
|
|
5
|
+
|
|
6
|
+
export interface SimTarget {
|
|
7
|
+
cwd?: string
|
|
8
|
+
port?: number
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The running simulator a control command talks to: `--port`, else this
|
|
13
|
+
* project's. Outside a miniapp project the only running simulator is used.
|
|
14
|
+
*/
|
|
15
|
+
export function findSim(target: SimTarget = {}): string {
|
|
16
|
+
if (target.port) return `http://127.0.0.1:${target.port}`
|
|
17
|
+
const records = listSimRecords()
|
|
18
|
+
const cwd = resolve(target.cwd ?? process.cwd())
|
|
19
|
+
const own = records.find((record: SimRecord) => record.cwd === cwd)
|
|
20
|
+
if (own) return `http://127.0.0.1:${own.port}`
|
|
21
|
+
if (existsSync(resolve(cwd, "miniapp.json"))) {
|
|
22
|
+
throw new Error("No simulator is running for this project. Start one with `glassly sim` (or `glassly sim --detach`).")
|
|
23
|
+
}
|
|
24
|
+
if (records.length === 1) return `http://127.0.0.1:${records[0].port}`
|
|
25
|
+
if (records.length === 0) throw new Error("No simulator is running. Start one with `glassly sim` in a miniapp project.")
|
|
26
|
+
throw new Error(
|
|
27
|
+
`Several simulators are running; pass --port or --cwd:\n${records
|
|
28
|
+
.map((r: SimRecord) => ` ${r.port} ${r.packageName} ${r.cwd}`)
|
|
29
|
+
.join("\n")}`,
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function call(target: SimTarget, route: string, init?: RequestInit): Promise<Response> {
|
|
34
|
+
const base = findSim(target)
|
|
35
|
+
let response: Response
|
|
36
|
+
try {
|
|
37
|
+
response = await fetch(`${base}/api/${route}`, init)
|
|
38
|
+
} catch {
|
|
39
|
+
throw new Error(`The simulator at ${base} did not answer. Is \`glassly sim\` still running?`)
|
|
40
|
+
}
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
const body = (await response.json().catch(() => null)) as {error?: string} | null
|
|
43
|
+
throw new Error(body?.error ?? `Simulator answered ${response.status} for ${route}`)
|
|
44
|
+
}
|
|
45
|
+
return response
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function simGet<T = unknown>(target: SimTarget, route: string): Promise<T> {
|
|
49
|
+
return (await (await call(target, route)).json()) as T
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function simText(target: SimTarget, route: string): Promise<string> {
|
|
53
|
+
return (await call(target, route)).text()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function simPost<T = {ok: boolean}>(
|
|
57
|
+
target: SimTarget,
|
|
58
|
+
route: string,
|
|
59
|
+
body: Record<string, unknown> = {},
|
|
60
|
+
): Promise<T> {
|
|
61
|
+
const response = await call(target, route, {
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers: {"content-type": "application/json"},
|
|
64
|
+
body: JSON.stringify(body),
|
|
65
|
+
})
|
|
66
|
+
return (await response.json()) as T
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function simScreenshot(
|
|
70
|
+
target: SimTarget,
|
|
71
|
+
path: string,
|
|
72
|
+
options: {scale?: number; wait?: boolean} = {},
|
|
73
|
+
): Promise<string> {
|
|
74
|
+
const query = new URLSearchParams({scale: String(options.scale ?? 2), wait: options.wait === false ? "0" : "1"})
|
|
75
|
+
const response = await call(target, `screenshot?${query}`)
|
|
76
|
+
const file = resolve(path)
|
|
77
|
+
writeFileSync(file, new Uint8Array(await response.arrayBuffer()))
|
|
78
|
+
return file
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface SimLogLine {
|
|
82
|
+
seq: number
|
|
83
|
+
timestamp: number
|
|
84
|
+
source: string
|
|
85
|
+
level: string
|
|
86
|
+
message: string
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function simLogs(
|
|
90
|
+
target: SimTarget,
|
|
91
|
+
options: {since?: number; sources?: string[]} = {},
|
|
92
|
+
): Promise<{entries: SimLogLine[]; seq: number}> {
|
|
93
|
+
const query = new URLSearchParams({since: String(options.since ?? 0)})
|
|
94
|
+
if (options.sources?.length) query.set("source", options.sources.join(","))
|
|
95
|
+
return simGet(target, `logs?${query}`)
|
|
96
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/// <reference types="bun-types" />
|
|
2
|
+
|
|
3
|
+
import {expect, test} from "bun:test"
|
|
4
|
+
import {existsSync} from "fs"
|
|
5
|
+
import {fileURLToPath} from "url"
|
|
6
|
+
|
|
7
|
+
import {simDevice} from "./device.js"
|
|
8
|
+
|
|
9
|
+
// The app's own phone-mode profile. It exists only inside the monorepo, and a
|
|
10
|
+
// computed path keeps the engine's sources out of this package's typecheck.
|
|
11
|
+
const engineProfile = fileURLToPath(
|
|
12
|
+
new URL("../../../../mobile/modules/engine/src/types/capabilities/simulated-glasses.ts", import.meta.url),
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
test.skipIf(!existsSync(engineProfile))("the simulator's glasses match the Glassly App's Simulated Glasses", async () => {
|
|
16
|
+
const {simulatedGlasses} = (await import(engineProfile)) as {simulatedGlasses: {display: Record<string, unknown>}}
|
|
17
|
+
const device = simDevice()
|
|
18
|
+
expect(device.capabilities).toEqual(JSON.parse(JSON.stringify(simulatedGlasses)))
|
|
19
|
+
for (const [key, value] of Object.entries(device.scene)) expect(simulatedGlasses.display[key]).toEqual(value)
|
|
20
|
+
})
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {G1_PROFILE, type DisplayProfile, type SceneDisplayCapabilities} from "@glassly/miniapp/display"
|
|
2
|
+
|
|
3
|
+
export interface SimDevice {
|
|
4
|
+
id: string
|
|
5
|
+
capabilities: Record<string, unknown> & {modelName: string}
|
|
6
|
+
scene: SceneDisplayCapabilities
|
|
7
|
+
profile: DisplayProfile
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const SIMULATED_DISPLAY = {
|
|
11
|
+
width: 576,
|
|
12
|
+
height: 288,
|
|
13
|
+
canPosition: true,
|
|
14
|
+
maxTextElements: 120,
|
|
15
|
+
maxImageElements: 120,
|
|
16
|
+
shapes: ["rect", "circle", "arc", "pie", "line", "triangle", "quad", "bezier", "svg"],
|
|
17
|
+
animation: true,
|
|
18
|
+
rotation: true,
|
|
19
|
+
depth: true,
|
|
20
|
+
intensityLevels: 16,
|
|
21
|
+
partialUpdate: true,
|
|
22
|
+
} satisfies SceneDisplayCapabilities
|
|
23
|
+
|
|
24
|
+
// Kept equal to the Glassly App's "Simulated Glasses" (phone mode), which
|
|
25
|
+
// previews the G2 Glassly CFW scene API; device.test.ts compares the two.
|
|
26
|
+
const SIMULATED: SimDevice = {
|
|
27
|
+
id: "simulated",
|
|
28
|
+
scene: SIMULATED_DISPLAY,
|
|
29
|
+
profile: G1_PROFILE,
|
|
30
|
+
capabilities: {
|
|
31
|
+
modelName: "Simulated Glasses",
|
|
32
|
+
hasCamera: false,
|
|
33
|
+
camera: null,
|
|
34
|
+
hasDisplay: true,
|
|
35
|
+
display: {
|
|
36
|
+
count: 1,
|
|
37
|
+
isColor: false,
|
|
38
|
+
color: "green",
|
|
39
|
+
canDisplayBitmap: true,
|
|
40
|
+
resolution: {width: 640, height: 480},
|
|
41
|
+
fieldOfView: {horizontal: 30},
|
|
42
|
+
maxTextLines: 7,
|
|
43
|
+
adjustBrightness: true,
|
|
44
|
+
...SIMULATED_DISPLAY,
|
|
45
|
+
},
|
|
46
|
+
hasMicrophone: true,
|
|
47
|
+
microphone: {count: 1, hasVAD: false},
|
|
48
|
+
hasSpeaker: true,
|
|
49
|
+
speaker: {count: 1, isPrivate: false},
|
|
50
|
+
hasIMU: false,
|
|
51
|
+
imu: null,
|
|
52
|
+
hasButton: true,
|
|
53
|
+
button: {count: 1, buttons: [{type: "press", events: ["press"], isCapacitive: false}]},
|
|
54
|
+
hasLight: false,
|
|
55
|
+
light: null,
|
|
56
|
+
power: {hasExternalBattery: false},
|
|
57
|
+
hasWifi: false,
|
|
58
|
+
hasOta: false,
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const SIM_DEVICES: Record<string, SimDevice> = {simulated: SIMULATED}
|
|
63
|
+
|
|
64
|
+
export function simDevice(id = "simulated"): SimDevice {
|
|
65
|
+
const device = SIM_DEVICES[id]
|
|
66
|
+
if (!device) throw new Error(`Unknown simulator device "${id}". Available: ${Object.keys(SIM_DEVICES).join(", ")}`)
|
|
67
|
+
return device
|
|
68
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
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
|
+
/** A real SDK session wired straight to the host, standing in for the worker bridge. */
|
|
11
|
+
async function connect(manifest: Partial<SimManifest> = {}) {
|
|
12
|
+
const events: SimHostEvent[] = []
|
|
13
|
+
let deliver: (raw: string) => void = () => {}
|
|
14
|
+
const host = new SimHost({
|
|
15
|
+
manifest: {packageName: "com.example.sim", name: "Sim Test", ...manifest},
|
|
16
|
+
device: simDevice(),
|
|
17
|
+
storage: createSimStorage(),
|
|
18
|
+
toBackground: (raw) => queueMicrotask(() => deliver(raw)),
|
|
19
|
+
emit: (event) => events.push(event),
|
|
20
|
+
frameLatencyMs: 0,
|
|
21
|
+
speechMsPerChar: 0,
|
|
22
|
+
})
|
|
23
|
+
const transport: Transport = {
|
|
24
|
+
open: async () => {},
|
|
25
|
+
send: (raw) => queueMicrotask(() => host.fromBackground(raw)),
|
|
26
|
+
onMessage: (handler) => {
|
|
27
|
+
deliver = handler
|
|
28
|
+
},
|
|
29
|
+
onDisconnect: () => {},
|
|
30
|
+
close: () => {},
|
|
31
|
+
isOpen: () => true,
|
|
32
|
+
}
|
|
33
|
+
host.backgroundStarted()
|
|
34
|
+
const session = new MiniappSession({transport, packageName: "com.example.sim"})
|
|
35
|
+
const errors: unknown[] = []
|
|
36
|
+
session.on("error", (error) => errors.push(error))
|
|
37
|
+
await session.connect()
|
|
38
|
+
const logs = () => events.flatMap((event) => (event.type === "log" ? [event.entry.message] : []))
|
|
39
|
+
return {host, session, events, errors, logs}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const settle = () => Bun.sleep(5)
|
|
43
|
+
const box = {x: 0, y: 0, w: 576, h: 288}
|
|
44
|
+
|
|
45
|
+
describe("handshake", () => {
|
|
46
|
+
test("hands the miniapp the simulated glasses and its declared permissions", async () => {
|
|
47
|
+
const {session} = await connect({permissions: [{type: "MICROPHONE"}]})
|
|
48
|
+
expect(session.capabilities?.display?.width).toBe(576)
|
|
49
|
+
expect(session.capabilities?.display?.height).toBe(288)
|
|
50
|
+
expect(session.permissions.has("microphone")).toBe(true)
|
|
51
|
+
expect(session.permissions.has("location")).toBe(false)
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
describe("display", () => {
|
|
56
|
+
test("wraps text the way the phone does and shows it on the glasses", async () => {
|
|
57
|
+
const {session, host} = await connect()
|
|
58
|
+
const result = await session.display.render([
|
|
59
|
+
{type: "text", id: "hello", box: {x: 0, y: 0, w: 120, h: 288}, text: "Hello from the simulator"},
|
|
60
|
+
])
|
|
61
|
+
expect(result).toMatchObject({status: "displayed", presented: true, degraded: false})
|
|
62
|
+
const content = host.snapshot().content
|
|
63
|
+
expect(content.kind).toBe("scene")
|
|
64
|
+
if (content.kind !== "scene") return
|
|
65
|
+
expect(content.elements[0].id).toBe("hello")
|
|
66
|
+
expect(content.elements[0].text?.split("\n").length).toBeGreaterThan(1)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test("reports elements it had to drop", async () => {
|
|
70
|
+
const {session, logs} = await connect()
|
|
71
|
+
const result = await session.display.render([
|
|
72
|
+
{type: "text", id: "seen", box, text: "on screen"},
|
|
73
|
+
{type: "text", id: "lost", box: {x: 900, y: 0, w: 50, h: 20}, text: "off the canvas"},
|
|
74
|
+
])
|
|
75
|
+
expect(result.degraded).toBe(true)
|
|
76
|
+
expect(result.dropped).toEqual(["lost"])
|
|
77
|
+
expect(logs().some((line) => line.includes("dropped elements: lost"))).toBe(true)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test("render([]) clears the glasses", async () => {
|
|
81
|
+
const {session, host} = await connect()
|
|
82
|
+
await session.display.render([{type: "rect", id: "r", box: {x: 10, y: 10, w: 40, h: 40}}])
|
|
83
|
+
expect(host.snapshot().content.kind).toBe("scene")
|
|
84
|
+
await session.display.render([])
|
|
85
|
+
expect(host.snapshot().content.kind).toBe("empty")
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test("the dashboard view belongs to dashboards", async () => {
|
|
89
|
+
const {session, host} = await connect()
|
|
90
|
+
const result = await session.display.render([{type: "text", id: "t", box, text: "overlay"}], {view: "dashboard"})
|
|
91
|
+
expect(result).toMatchObject({status: "displayed", presented: false})
|
|
92
|
+
expect(host.snapshot().content.kind).toBe("empty")
|
|
93
|
+
})
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
describe("input", () => {
|
|
97
|
+
test("a gesture reaches both the catch-all and the per-gesture listeners", async () => {
|
|
98
|
+
const {session, host} = await connect()
|
|
99
|
+
const all: string[] = []
|
|
100
|
+
const taps: string[] = []
|
|
101
|
+
const head: string[] = []
|
|
102
|
+
session.input.onTouch((touch) => all.push(touch.kind))
|
|
103
|
+
session.input.onTouch("single_tap", (touch) => taps.push(touch.kind))
|
|
104
|
+
session.imu.onHeadPosition((position) => head.push(position.position))
|
|
105
|
+
await settle()
|
|
106
|
+
host.touch("single_tap")
|
|
107
|
+
host.touch("swipe_up")
|
|
108
|
+
host.touch("head_up")
|
|
109
|
+
await settle()
|
|
110
|
+
expect(all).toEqual(["single_tap", "swipe_up", "head_up"])
|
|
111
|
+
expect(taps).toEqual(["single_tap"])
|
|
112
|
+
expect(head).toEqual(["up"])
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
test("button presses carry their press type", async () => {
|
|
116
|
+
const {session, host} = await connect()
|
|
117
|
+
const presses: string[] = []
|
|
118
|
+
session.input.onButtonPress((press) => presses.push(press.pressType))
|
|
119
|
+
await settle()
|
|
120
|
+
host.button("long")
|
|
121
|
+
await settle()
|
|
122
|
+
expect(presses).toEqual(["long"])
|
|
123
|
+
})
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
describe("speech", () => {
|
|
127
|
+
test("typed speech arrives as a transcription", async () => {
|
|
128
|
+
const {session, host} = await connect({permissions: [{type: "MICROPHONE"}]})
|
|
129
|
+
const heard: {text: string; isFinal: boolean}[] = []
|
|
130
|
+
session.transcription.on((data) => heard.push({text: data.text, isFinal: data.isFinal}))
|
|
131
|
+
await settle()
|
|
132
|
+
host.transcription("turn on the", {isFinal: false})
|
|
133
|
+
host.transcription("turn on the lights")
|
|
134
|
+
await settle()
|
|
135
|
+
expect(heard).toEqual([
|
|
136
|
+
{text: "turn on the", isFinal: false},
|
|
137
|
+
{text: "turn on the lights", isFinal: true},
|
|
138
|
+
])
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
test("a language listener hears only its language", async () => {
|
|
142
|
+
const {session, host} = await connect({permissions: [{type: "MICROPHONE"}]})
|
|
143
|
+
const spanish: string[] = []
|
|
144
|
+
session.transcription.forLanguage("es-ES", (data) => spanish.push(data.text))
|
|
145
|
+
await settle()
|
|
146
|
+
host.transcription("hello", {language: "en-US"})
|
|
147
|
+
host.transcription("hola", {language: "es-MX"})
|
|
148
|
+
await settle()
|
|
149
|
+
expect(spanish).toEqual(["hola"])
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
test("subscribing without the manifest permission fails the way it does on a phone", async () => {
|
|
153
|
+
const {session, errors, logs} = await connect()
|
|
154
|
+
session.transcription.on(() => {})
|
|
155
|
+
await settle()
|
|
156
|
+
expect(JSON.stringify(errors)).toContain("PERMISSION_NOT_DECLARED")
|
|
157
|
+
expect(logs().some((line) => line.includes("MICROPHONE permission not declared"))).toBe(true)
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
test("speak() is recorded and resolves once the utterance ends", async () => {
|
|
161
|
+
const {session, events} = await connect()
|
|
162
|
+
const result = await session.speaker.speak("hi there")
|
|
163
|
+
expect(result).toMatchObject({completed: true})
|
|
164
|
+
const spoken = events.flatMap((event) => (event.type === "effect" && event.effect.kind === "speak" ? [event.effect.text] : []))
|
|
165
|
+
expect(spoken).toEqual(["hi there"])
|
|
166
|
+
})
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
describe("phone", () => {
|
|
170
|
+
test("storage round-trips", async () => {
|
|
171
|
+
const {session} = await connect()
|
|
172
|
+
await session.storage.set("notes", "[1,2]")
|
|
173
|
+
expect(await session.storage.get("notes")).toBe("[1,2]")
|
|
174
|
+
expect(await session.storage.keys()).toEqual(["notes"])
|
|
175
|
+
await session.storage.delete("notes")
|
|
176
|
+
expect(await session.storage.get("notes")).toBeNull()
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
test("location needs the permission, then follows the simulated phone", async () => {
|
|
180
|
+
const denied = await connect()
|
|
181
|
+
await expect(denied.session.location.getOnce()).rejects.toMatchObject({code: "PERMISSION_NOT_DECLARED"})
|
|
182
|
+
|
|
183
|
+
const {session, host} = await connect({permissions: [{type: "LOCATION"}]})
|
|
184
|
+
const fixes: number[] = []
|
|
185
|
+
session.location.onUpdate((fix) => fixes.push(fix.lat))
|
|
186
|
+
await settle()
|
|
187
|
+
host.setLocation(48.8566, 2.3522)
|
|
188
|
+
await settle()
|
|
189
|
+
expect(fixes).toEqual([48.8566])
|
|
190
|
+
expect(await session.location.getOnce()).toMatchObject({lat: 48.8566, lng: 2.3522})
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
test("a disconnect pauses the miniapp and holds back its streams", async () => {
|
|
194
|
+
const {session, host} = await connect()
|
|
195
|
+
const taps: string[] = []
|
|
196
|
+
const pauses: boolean[] = []
|
|
197
|
+
session.input.onTouch((touch) => taps.push(touch.kind))
|
|
198
|
+
session.on("pause", () => pauses.push(true))
|
|
199
|
+
session.on("resume", () => pauses.push(false))
|
|
200
|
+
await settle()
|
|
201
|
+
host.setGlassesConnected(false)
|
|
202
|
+
host.touch("single_tap")
|
|
203
|
+
await settle()
|
|
204
|
+
host.setGlassesConnected(true)
|
|
205
|
+
host.touch("double_tap")
|
|
206
|
+
await settle()
|
|
207
|
+
expect(pauses).toEqual([true, false])
|
|
208
|
+
expect(taps).toEqual(["double_tap"])
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
test("a request the simulator does not cover says so", async () => {
|
|
212
|
+
const {session, host} = await connect()
|
|
213
|
+
await expect(session.camera.takePhoto()).rejects.toMatchObject({code: "NOT_IMPLEMENTED"})
|
|
214
|
+
expect((host.status().unsimulated as string[]).length).toBe(1)
|
|
215
|
+
})
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
describe("phone page bridge", () => {
|
|
219
|
+
test("the CLI can stand in for the page: broadcasts and RPC calls", async () => {
|
|
220
|
+
const {session, host, events} = await connect()
|
|
221
|
+
const pings: unknown[] = []
|
|
222
|
+
session.ui.on("ping", (payload) => pings.push(payload))
|
|
223
|
+
session.ui.handle("sum" as never, (async (payload: {a: number; b: number}) => payload.a + payload.b) as never)
|
|
224
|
+
session.ui.onOpen(() => session.ui.send("state", {ready: true}))
|
|
225
|
+
await settle()
|
|
226
|
+
|
|
227
|
+
host.uiSend("ping", {at: 1})
|
|
228
|
+
await settle()
|
|
229
|
+
expect(pings).toEqual([{at: 1}])
|
|
230
|
+
expect(await host.uiRequest("sum", {a: 2, b: 3})).toBe(5)
|
|
231
|
+
await expect(host.uiRequest("missing", {})).rejects.toThrow("no handler registered")
|
|
232
|
+
|
|
233
|
+
const pushed = events.flatMap((event) => (event.type === "ui" ? [event.frame] : []))
|
|
234
|
+
expect(pushed).toContainEqual(expect.objectContaining({type: "msg", channel: "state", payload: {ready: true}}))
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
test("a page that connects and leaves opens and closes the background's UI", async () => {
|
|
238
|
+
const {session, host} = await connect()
|
|
239
|
+
const seen: string[] = []
|
|
240
|
+
session.ui.onOpen(() => seen.push("open"))
|
|
241
|
+
session.ui.onClose(() => seen.push("close"))
|
|
242
|
+
await settle()
|
|
243
|
+
host.fromUi("tab-1", JSON.stringify({type: "ready"}))
|
|
244
|
+
await settle()
|
|
245
|
+
expect(session.ui.isOpen()).toBe(true)
|
|
246
|
+
host.uiClosed("tab-1")
|
|
247
|
+
await settle()
|
|
248
|
+
expect(seen).toEqual(["open", "close"])
|
|
249
|
+
})
|
|
250
|
+
})
|