@glassly/miniapp-cli 0.1.16-dev.0 → 0.1.17-beta.2
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 +1 -1
- package/package.json +1 -1
- package/src/dev.ts +7 -5
- package/src/release.ts +7 -5
- package/src/sim/host.test.ts +447 -8
- package/src/sim/host.ts +204 -61
- package/src/sim/registry.ts +112 -22
- package/src/sim/script.test.ts +4 -0
- package/src/sim/script.ts +3 -3
- package/src/sim/server.ts +60 -14
- package/src/sim/shots.test.ts +12 -3
- package/src/sim/sim.ts +4 -4
- package/src/sim/types/audio.test.ts +122 -0
- package/src/sim/types/audio.ts +269 -0
- package/src/sim/types/blobs-paths.ts +88 -0
- package/src/sim/types/blobs-store.ts +406 -0
- package/src/sim/types/blobs.test.ts +191 -0
- package/src/sim/types/blobs.ts +112 -0
- package/src/sim/types/context.ts +19 -0
- package/src/sim/types/hardware.test.ts +102 -0
- package/src/sim/types/hardware.ts +320 -0
- package/src/sim/types/index.ts +17 -0
- package/src/sim/types/navigation.test.ts +98 -0
- package/src/sim/types/navigation.ts +320 -0
- package/src/sim/types/services.test.ts +140 -0
- package/src/sim/types/services.ts +237 -0
- package/src/sim/wav.test.ts +36 -0
- package/src/sim/wav.ts +34 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {mkdtempSync, rmSync} from "fs"
|
|
2
|
+
import {tmpdir} from "os"
|
|
3
|
+
import {join} from "path"
|
|
4
|
+
import {MiniappErrorCode, MiniappRequestType} from "@glassly/miniapp"
|
|
5
|
+
|
|
6
|
+
import {shareFileName} from "./blobs-paths.js"
|
|
7
|
+
import {BLOB_QUOTA_BYTES, OCTET, SimBlobStore} from "./blobs-store.js"
|
|
8
|
+
import type {SimTypeContext, SimTypeHandler} from "./context.js"
|
|
9
|
+
|
|
10
|
+
const stores = new WeakMap<SimTypeContext, SimBlobStore>()
|
|
11
|
+
const tempDirs: string[] = []
|
|
12
|
+
|
|
13
|
+
function storeFor(ctx: SimTypeContext): SimBlobStore {
|
|
14
|
+
let store = stores.get(ctx)
|
|
15
|
+
if (!store) {
|
|
16
|
+
store = new SimBlobStore(ctx.stateDir ? join(ctx.stateDir, "blobs") : memoryDir(), (requestId, ok, data, error) =>
|
|
17
|
+
ctx.reply(requestId, ok, data, error),
|
|
18
|
+
)
|
|
19
|
+
const created = store
|
|
20
|
+
stores.set(ctx, created)
|
|
21
|
+
ctx.onEnd(() => created.closeHandles())
|
|
22
|
+
}
|
|
23
|
+
return store
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// A host without a state folder (tests) gets its own throwaway folder, removed when the process exits.
|
|
27
|
+
function memoryDir(): string {
|
|
28
|
+
if (!tempDirs.length) process.once("exit", () => tempDirs.forEach((dir) => rmSync(dir, {recursive: true, force: true})))
|
|
29
|
+
const dir = mkdtempSync(join(tmpdir(), "glassly-sim-blobs-"))
|
|
30
|
+
tempDirs.push(dir)
|
|
31
|
+
return dir
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function share(ctx: SimTypeContext, store: SimBlobStore, payload: Record<string, unknown>, requestId?: string): void {
|
|
35
|
+
const key = payload.key as string
|
|
36
|
+
const meta = store.readMeta(key)
|
|
37
|
+
if (!meta || !store.exists(meta.fileName)) {
|
|
38
|
+
return ctx.fail(requestId, MiniappErrorCode.BLOB_NOT_FOUND, `Blob ${key} not found`)
|
|
39
|
+
}
|
|
40
|
+
ctx.fake(MiniappRequestType.BLOB_SHARE, "No share sheet; answers as if the user shared the file")
|
|
41
|
+
ctx.effect("share", {key, fileName: shareFileName(meta), mimeType: meta.mimeType || OCTET, path: store.filePath(meta.fileName)})
|
|
42
|
+
ctx.reply(requestId, true, {success: true, simulated: true})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const handleBlobs: SimTypeHandler = (ctx, type, payload, requestId) => {
|
|
46
|
+
if (!type.startsWith("miniapp_blob_")) return false
|
|
47
|
+
const store = storeFor(ctx)
|
|
48
|
+
try {
|
|
49
|
+
switch (type) {
|
|
50
|
+
case MiniappRequestType.BLOB_CREATE:
|
|
51
|
+
store.create(payload, requestId)
|
|
52
|
+
break
|
|
53
|
+
case MiniappRequestType.BLOB_WRITE:
|
|
54
|
+
store.write(payload, requestId)
|
|
55
|
+
break
|
|
56
|
+
case MiniappRequestType.BLOB_COMMIT:
|
|
57
|
+
store.commit(payload, requestId)
|
|
58
|
+
break
|
|
59
|
+
case MiniappRequestType.BLOB_ABORT:
|
|
60
|
+
store.abort(payload, requestId)
|
|
61
|
+
break
|
|
62
|
+
case MiniappRequestType.BLOB_SET_FROM_URL:
|
|
63
|
+
void store.setFromUrl(payload, requestId).catch((err) => store.fail(requestId, MiniappErrorCode.INTERNAL, err))
|
|
64
|
+
break
|
|
65
|
+
case MiniappRequestType.BLOB_IMPORT:
|
|
66
|
+
ctx.fake(type, "No file picker; answers null, as the phone does when the user cancels")
|
|
67
|
+
ctx.reply(requestId, true, null)
|
|
68
|
+
break
|
|
69
|
+
case MiniappRequestType.BLOB_GET:
|
|
70
|
+
store.get(payload, requestId)
|
|
71
|
+
break
|
|
72
|
+
case MiniappRequestType.BLOB_LIST:
|
|
73
|
+
store.list(requestId)
|
|
74
|
+
break
|
|
75
|
+
case MiniappRequestType.BLOB_USAGE:
|
|
76
|
+
store.usage(requestId)
|
|
77
|
+
break
|
|
78
|
+
case MiniappRequestType.BLOB_DELETE:
|
|
79
|
+
store.delete(payload, requestId)
|
|
80
|
+
break
|
|
81
|
+
case MiniappRequestType.BLOB_CLEAR:
|
|
82
|
+
store.clear(requestId)
|
|
83
|
+
break
|
|
84
|
+
case MiniappRequestType.BLOB_OPEN_READ:
|
|
85
|
+
store.openRead(payload, requestId)
|
|
86
|
+
break
|
|
87
|
+
case MiniappRequestType.BLOB_READ:
|
|
88
|
+
store.read(payload, requestId)
|
|
89
|
+
break
|
|
90
|
+
case MiniappRequestType.BLOB_CLOSE_READ:
|
|
91
|
+
store.closeRead(payload, requestId)
|
|
92
|
+
break
|
|
93
|
+
case MiniappRequestType.BLOB_SHARE:
|
|
94
|
+
share(ctx, store, payload, requestId)
|
|
95
|
+
break
|
|
96
|
+
default:
|
|
97
|
+
return false
|
|
98
|
+
}
|
|
99
|
+
} catch (err) {
|
|
100
|
+
ctx.fail(requestId, MiniappErrorCode.INTERNAL, err instanceof Error ? err.message : "Blob storage error")
|
|
101
|
+
}
|
|
102
|
+
return true
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export const BLOB_MESSAGES: Record<string, string> = {
|
|
106
|
+
[MiniappErrorCode.BLOB_NOT_FOUND]: "Blob not found",
|
|
107
|
+
[MiniappErrorCode.BLOB_QUOTA_EXCEEDED]: `Blob quota (${BLOB_QUOTA_BYTES} bytes) exceeded`,
|
|
108
|
+
[MiniappErrorCode.BLOB_HANDLE_INVALID]: "Unknown blob read handle",
|
|
109
|
+
[MiniappErrorCode.BLOB_WRITE_FAILED]: "Blob error",
|
|
110
|
+
[MiniappErrorCode.BLOB_DOWNLOAD_FAILED]: "Blob error",
|
|
111
|
+
[MiniappErrorCode.BLOB_IMPORT_FAILED]: "Blob error",
|
|
112
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type {SimDevice} from "../device.js"
|
|
2
|
+
|
|
3
|
+
export interface SimTypeContext {
|
|
4
|
+
readonly packageName: string
|
|
5
|
+
readonly device: SimDevice
|
|
6
|
+
readonly stateDir: string | undefined
|
|
7
|
+
readonly glassesConnected: boolean
|
|
8
|
+
declares(permission: string): boolean
|
|
9
|
+
requirePermission(requestId: string | undefined, permission: string, operation?: string): boolean
|
|
10
|
+
reply(requestId: string | undefined, ok: boolean, data?: unknown, error?: Record<string, unknown>): void
|
|
11
|
+
fail(requestId: string | undefined, code: string, message: string): void
|
|
12
|
+
deliver(payload: Record<string, unknown>): void
|
|
13
|
+
push(stream: string, data: unknown): void
|
|
14
|
+
fake(type: string, note: string): void
|
|
15
|
+
effect(kind: string, detail: Record<string, unknown>): void
|
|
16
|
+
log(level: "info" | "warn" | "error", message: string): void
|
|
17
|
+
onEnd(callback: () => void): void
|
|
18
|
+
}
|
|
19
|
+
export type SimTypeHandler = (ctx: SimTypeContext, type: string, payload: Record<string, unknown>, requestId?: string) => boolean
|
|
@@ -0,0 +1,102 @@
|
|
|
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 = "mentra-live") {
|
|
11
|
+
const events: SimHostEvent[] = []
|
|
12
|
+
const delivered: Record<string, unknown>[] = []
|
|
13
|
+
let deliver: (raw: string) => void = () => {}
|
|
14
|
+
const host = new SimHost({
|
|
15
|
+
manifest: {packageName: "com.example.sim", name: "Sim Test", ...manifest},
|
|
16
|
+
device: simDevice(device),
|
|
17
|
+
storage: createSimStorage(),
|
|
18
|
+
toBackground: (raw) =>
|
|
19
|
+
queueMicrotask(() => {
|
|
20
|
+
delivered.push((JSON.parse(raw) as {payload: Record<string, unknown>}).payload)
|
|
21
|
+
deliver(raw)
|
|
22
|
+
}),
|
|
23
|
+
emit: (event) => events.push(event),
|
|
24
|
+
frameLatencyMs: 0,
|
|
25
|
+
speechMsPerChar: 0,
|
|
26
|
+
})
|
|
27
|
+
const transport: Transport = {
|
|
28
|
+
open: async () => {},
|
|
29
|
+
send: (raw) => queueMicrotask(() => host.fromBackground(raw)),
|
|
30
|
+
onMessage: (handler) => {
|
|
31
|
+
deliver = handler
|
|
32
|
+
},
|
|
33
|
+
onDisconnect: () => {},
|
|
34
|
+
close: () => {},
|
|
35
|
+
isOpen: () => true,
|
|
36
|
+
}
|
|
37
|
+
host.backgroundStarted()
|
|
38
|
+
const session = new MiniappSession({transport, packageName: "com.example.sim"})
|
|
39
|
+
await session.connect()
|
|
40
|
+
const effects = () => events.flatMap((event) => (event.type === "effect" ? [event.effect] : []))
|
|
41
|
+
return {session, effects, delivered, host}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const camera = {permissions: [{type: "CAMERA"}]}
|
|
45
|
+
|
|
46
|
+
describe("camera", () => {
|
|
47
|
+
test("a photo on a camera model is a JPEG test card", async () => {
|
|
48
|
+
const {session} = await connect(camera)
|
|
49
|
+
const photo = await session.camera.takePhoto()
|
|
50
|
+
expect(photo.mimeType).toBe("image/jpeg")
|
|
51
|
+
const bytes = Buffer.from(photo.photoUrl.replace(/^data:image\/jpeg;base64,/, ""), "base64")
|
|
52
|
+
expect([...bytes.subarray(0, 3)]).toEqual([0xff, 0xd8, 0xff])
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test("a photo on glasses without a camera fails with the phone's native timeout", async () => {
|
|
56
|
+
const {session} = await connect(camera, "g2")
|
|
57
|
+
await expect(session.camera.takePhoto()).rejects.toMatchObject({
|
|
58
|
+
code: "request_timeout",
|
|
59
|
+
message: expect.stringMatching(/^photo request [0-9a-f]{4} timed out waiting for glasses response\.$/),
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test("with the glasses disconnected, a photo fails at once as on the phone", async () => {
|
|
64
|
+
const {session, host} = await connect(camera)
|
|
65
|
+
host.setGlassesConnected(false)
|
|
66
|
+
await expect(session.camera.takePhoto()).rejects.toMatchObject({code: "GLASSES_NOT_CONNECTED", message: "Glasses are not connected"})
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
describe("video recording", () => {
|
|
71
|
+
test("one recording at a time, stopped by its id", async () => {
|
|
72
|
+
const {session, effects} = await connect(camera)
|
|
73
|
+
const {recordingId} = await session.camera.startVideoRecording({fps: 15})
|
|
74
|
+
await expect(session.camera.startVideoRecording()).rejects.toMatchObject({
|
|
75
|
+
code: "ALREADY_RECORDING",
|
|
76
|
+
message: "A video recording is already in progress",
|
|
77
|
+
})
|
|
78
|
+
await session.camera.stopVideoRecording(recordingId)
|
|
79
|
+
await expect(session.camera.stopVideoRecording(recordingId)).rejects.toMatchObject({code: "RECORDING_NOT_OWNED"})
|
|
80
|
+
expect(effects().filter((effect) => effect.kind === "video")).toMatchObject([
|
|
81
|
+
{action: "start", recordingId, fps: 15},
|
|
82
|
+
{action: "stop", recordingId},
|
|
83
|
+
])
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
describe("streams", () => {
|
|
88
|
+
test("an unmanaged stream blocks a managed one until it stops", async () => {
|
|
89
|
+
const {session, delivered} = await connect(camera)
|
|
90
|
+
const started = await session.stream.startStream({direct: "rtmp://example.com/live"})
|
|
91
|
+
expect(delivered).toContainEqual(
|
|
92
|
+
expect.objectContaining({type: "miniapp_event", streamType: "stream_status", data: expect.objectContaining({streamId: started.streamId})}),
|
|
93
|
+
)
|
|
94
|
+
await expect(session.stream.startStream()).rejects.toMatchObject({
|
|
95
|
+
code: "STREAM_ALREADY_ACTIVE",
|
|
96
|
+
message: "An unmanaged stream is already active. Stop it before starting a managed stream.",
|
|
97
|
+
})
|
|
98
|
+
await session.stream.stop()
|
|
99
|
+
const managed = await session.stream.startStream({ingest: "whip"})
|
|
100
|
+
expect(managed).toMatchObject({mode: "webrtc", simulated: true})
|
|
101
|
+
})
|
|
102
|
+
})
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import {MiniappErrorCode, MiniappRequestType, MiniappResponseType} from "@glassly/miniapp/protocol"
|
|
2
|
+
|
|
3
|
+
import type {SimTypeContext, SimTypeHandler} from "./context.js"
|
|
4
|
+
|
|
5
|
+
const CAMERA_FOV_MIN = 62
|
|
6
|
+
const CAMERA_FOV_MAX = 118
|
|
7
|
+
const CAMERA_FOV_PRESETS: Record<string, number> = {narrow: 82, standard: 102, wide: 118}
|
|
8
|
+
const ROI_POSITIONS = ["center", "bottom", "top"]
|
|
9
|
+
const WARM_UP_DEFAULT_MS = 15_000
|
|
10
|
+
const WARM_UP_MAX_MS = 60_000
|
|
11
|
+
const NOT_LIVE = "This command requires Mentra Live glasses."
|
|
12
|
+
const STAND_IN_HOST = "glassly-sim.invalid"
|
|
13
|
+
|
|
14
|
+
export const HARDWARE_MESSAGES: Record<string, string> = {
|
|
15
|
+
GLASSES_NOT_CONNECTED: "Glasses are not connected",
|
|
16
|
+
ALREADY_RECORDING: "A video recording is already in progress",
|
|
17
|
+
INVALID_RECORDING_ID: "recordingId is required to stop a recording",
|
|
18
|
+
RECORDING_NOT_OWNED: "recordingId is not an active recording owned by this app",
|
|
19
|
+
STREAM_URL_REQUIRED: "streamUrl is required",
|
|
20
|
+
STREAM_DESTINATIONS_LOCKED: "Restream destinations cannot be modified on an already-running managed stream.",
|
|
21
|
+
CAPTURE_TIMEOUT:
|
|
22
|
+
"Photo capture did not complete. The take_photo command, media processing, transfer, or upload may have stalled.",
|
|
23
|
+
unsupported_device: NOT_LIVE,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface Stream {
|
|
27
|
+
kind: "unmanaged" | "managed"
|
|
28
|
+
streamId: string
|
|
29
|
+
result: Record<string, unknown>
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface HardwareState {
|
|
33
|
+
recordings: Set<string>
|
|
34
|
+
stream: Stream | undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const states = new WeakMap<SimTypeContext, HardwareState>()
|
|
38
|
+
let bleCounter = 0
|
|
39
|
+
let streamCounter = 0
|
|
40
|
+
|
|
41
|
+
function stateOf(ctx: SimTypeContext): HardwareState {
|
|
42
|
+
let state = states.get(ctx)
|
|
43
|
+
if (!state) states.set(ctx, (state = {recordings: new Set(), stream: undefined}))
|
|
44
|
+
return state
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const handleHardware: SimTypeHandler = (ctx, type, payload, requestId) => {
|
|
48
|
+
switch (type) {
|
|
49
|
+
case MiniappRequestType.PHOTO:
|
|
50
|
+
photo(ctx, payload, requestId).catch((error: Error) => ctx.fail(requestId, MiniappErrorCode.INTERNAL, error.message))
|
|
51
|
+
return true
|
|
52
|
+
case MiniappRequestType.CAMERA_WARM_UP:
|
|
53
|
+
warmUp(ctx, payload, requestId)
|
|
54
|
+
return true
|
|
55
|
+
case MiniappRequestType.CAMERA_FOV:
|
|
56
|
+
cameraFov(ctx, payload, requestId)
|
|
57
|
+
return true
|
|
58
|
+
case MiniappRequestType.VIDEO_RECORDING_START:
|
|
59
|
+
videoStart(ctx, payload, requestId)
|
|
60
|
+
return true
|
|
61
|
+
case MiniappRequestType.VIDEO_RECORDING_STOP:
|
|
62
|
+
videoStop(ctx, payload, requestId)
|
|
63
|
+
return true
|
|
64
|
+
case MiniappRequestType.STREAM_START:
|
|
65
|
+
streamStart(ctx, payload, requestId)
|
|
66
|
+
return true
|
|
67
|
+
case MiniappRequestType.MANAGED_STREAM_START:
|
|
68
|
+
managedStreamStart(ctx, payload, requestId)
|
|
69
|
+
return true
|
|
70
|
+
case MiniappRequestType.STREAM_STOP:
|
|
71
|
+
case MiniappRequestType.MANAGED_STREAM_STOP:
|
|
72
|
+
streamStop(ctx, payload, requestId)
|
|
73
|
+
return true
|
|
74
|
+
case MiniappRequestType.REQUEST_WIFI_SETUP:
|
|
75
|
+
ctx.fake(type, "the phone's Wi-Fi prompt resolves as if the user answered it")
|
|
76
|
+
ctx.effect("wifi_setup", {reason: payload.reason})
|
|
77
|
+
ctx.reply(requestId, true)
|
|
78
|
+
return true
|
|
79
|
+
case MiniappRequestType.SET_WIFI_ADB_STATE:
|
|
80
|
+
ctx.fail(requestId, MiniappErrorCode.NOT_PERMITTED, "setWifiAdbState is restricted to system apps")
|
|
81
|
+
return true
|
|
82
|
+
}
|
|
83
|
+
return false
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const hasCamera = (ctx: SimTypeContext) => ctx.device.capabilities.hasCamera === true
|
|
87
|
+
|
|
88
|
+
function glassesGone(ctx: SimTypeContext, requestId: string | undefined, extra: Record<string, unknown> = {}): boolean {
|
|
89
|
+
if (ctx.glassesConnected) return false
|
|
90
|
+
ctx.reply(requestId, false, undefined, {code: "GLASSES_NOT_CONNECTED", message: "Glasses are not connected", ...extra})
|
|
91
|
+
return true
|
|
92
|
+
}
|
|
93
|
+
const BLE = {stage: "command", transport: "ble"}
|
|
94
|
+
|
|
95
|
+
// Glasses without a camera never answer the BLE command; the phone gives up after its native timeout.
|
|
96
|
+
function timedOut(ctx: SimTypeContext, requestId: string | undefined, operation: string, seconds: number, extra = {}) {
|
|
97
|
+
ctx.log("warn", `${ctx.device.label} has no camera; the phone answers this after ${seconds} s`)
|
|
98
|
+
ctx.reply(requestId, false, undefined, {
|
|
99
|
+
code: "request_timeout",
|
|
100
|
+
message: `${operation} timed out waiting for glasses response.`,
|
|
101
|
+
...extra,
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function mintBleId(): string {
|
|
106
|
+
bleCounter = (bleCounter + 1) & 0xffff
|
|
107
|
+
return bleCounter.toString(16).padStart(4, "0")
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function photo(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string) {
|
|
111
|
+
if (!ctx.requirePermission(requestId, "CAMERA", MiniappRequestType.PHOTO)) return
|
|
112
|
+
const method = payload.transferMethod
|
|
113
|
+
if (method !== undefined && method !== "auto" && method !== "direct" && method !== "ble") {
|
|
114
|
+
return ctx.fail(
|
|
115
|
+
requestId,
|
|
116
|
+
MiniappErrorCode.INVALID_ARGUMENT,
|
|
117
|
+
`Invalid transferMethod ${JSON.stringify(method)}. Expected "auto", "direct", or "ble".`,
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
if (glassesGone(ctx, requestId, BLE)) return
|
|
121
|
+
const bleId = mintBleId()
|
|
122
|
+
if (!hasCamera(ctx)) return timedOut(ctx, requestId, `photo request ${bleId}`, 30, {stage: "command", transport: "ble"})
|
|
123
|
+
|
|
124
|
+
ctx.fake(MiniappRequestType.PHOTO, "a generated test card stands in for the glasses camera")
|
|
125
|
+
const card = await testCard(String(payload.size ?? "medium"), String(payload.mode ?? "photo"))
|
|
126
|
+
ctx.effect("photo", {size: payload.size, mode: payload.mode, bytes: card.bytes.length})
|
|
127
|
+
ctx.reply(requestId, true, {
|
|
128
|
+
photoUrl: `data:${card.mimeType};base64,${Buffer.from(card.bytes).toString("base64")}`,
|
|
129
|
+
mimeType: card.mimeType,
|
|
130
|
+
size: card.bytes.length,
|
|
131
|
+
requestId: `sim-photo-${bleId}`,
|
|
132
|
+
simulated: true,
|
|
133
|
+
})
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// A 1x1 PNG for hosts without a native canvas binding.
|
|
137
|
+
const FALLBACK_PNG =
|
|
138
|
+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkAAIAAAoAAv/lxKUAAAAASUVORK5CYII="
|
|
139
|
+
|
|
140
|
+
async function testCard(size: string, mode: string): Promise<{bytes: Uint8Array; mimeType: string}> {
|
|
141
|
+
let canvas: typeof import("@napi-rs/canvas")
|
|
142
|
+
try {
|
|
143
|
+
canvas = await import("@napi-rs/canvas")
|
|
144
|
+
} catch {
|
|
145
|
+
return {bytes: Buffer.from(FALLBACK_PNG, "base64"), mimeType: "image/png"}
|
|
146
|
+
}
|
|
147
|
+
const width = 480
|
|
148
|
+
const height = 270
|
|
149
|
+
const surface = canvas.createCanvas(width, height)
|
|
150
|
+
const g = surface.getContext("2d")
|
|
151
|
+
const bars = ["#c0c0c0", "#c0c000", "#00c0c0", "#00c000", "#c000c0", "#c00000", "#0000c0"]
|
|
152
|
+
bars.forEach((color, i) => {
|
|
153
|
+
g.fillStyle = color
|
|
154
|
+
g.fillRect(Math.floor((i * width) / bars.length), 0, Math.ceil(width / bars.length), height)
|
|
155
|
+
})
|
|
156
|
+
g.fillStyle = "rgba(0, 0, 0, 0.75)"
|
|
157
|
+
g.fillRect(0, height / 2 - 40, width, 80)
|
|
158
|
+
g.fillStyle = "#fff"
|
|
159
|
+
g.textAlign = "center"
|
|
160
|
+
g.font = "bold 24px sans-serif"
|
|
161
|
+
g.fillText("glassly sim test card", width / 2, height / 2 - 6)
|
|
162
|
+
g.font = "16px sans-serif"
|
|
163
|
+
g.fillText(`${mode} · ${size} · ${new Date().toISOString()}`, width / 2, height / 2 + 24)
|
|
164
|
+
return {bytes: surface.toBuffer("image/jpeg"), mimeType: "image/jpeg"}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function warmUp(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string) {
|
|
168
|
+
if (!ctx.requirePermission(requestId, "CAMERA", MiniappRequestType.CAMERA_WARM_UP)) return
|
|
169
|
+
if (!hasCamera(ctx)) {
|
|
170
|
+
return ctx.reply(requestId, false, undefined, {
|
|
171
|
+
code: "unsupported_device",
|
|
172
|
+
message: NOT_LIVE,
|
|
173
|
+
stage: "command",
|
|
174
|
+
transport: "ble",
|
|
175
|
+
})
|
|
176
|
+
}
|
|
177
|
+
if (glassesGone(ctx, requestId, BLE)) return
|
|
178
|
+
const raw = payload.durationMs
|
|
179
|
+
const requested = typeof raw === "number" && Number.isFinite(raw) && raw > 0 ? Math.round(raw) : WARM_UP_DEFAULT_MS
|
|
180
|
+
ctx.effect("camera_warm_up", {size: payload.size ?? "medium", mode: payload.mode ?? "photo", durationMs: Math.min(requested, WARM_UP_MAX_MS)})
|
|
181
|
+
ctx.reply(requestId, true)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function cameraFov(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string) {
|
|
185
|
+
if (!ctx.requirePermission(requestId, "CAMERA", MiniappRequestType.CAMERA_FOV)) return
|
|
186
|
+
if (!hasCamera(ctx)) return ctx.fail(requestId, "unsupported_device", NOT_LIVE)
|
|
187
|
+
let fov: number
|
|
188
|
+
let roiPosition = "center"
|
|
189
|
+
if (typeof payload.preset === "string") {
|
|
190
|
+
fov = CAMERA_FOV_PRESETS[payload.preset] ?? CAMERA_FOV_PRESETS.standard
|
|
191
|
+
} else {
|
|
192
|
+
const raw = typeof payload.fov === "number" && Number.isFinite(payload.fov) ? payload.fov : CAMERA_FOV_MAX
|
|
193
|
+
fov = Math.round(Math.min(CAMERA_FOV_MAX, Math.max(CAMERA_FOV_MIN, raw)))
|
|
194
|
+
roiPosition = roiName(payload.roiPosition ?? payload.roi_position)
|
|
195
|
+
}
|
|
196
|
+
ctx.effect("camera_fov", {fov, roiPosition})
|
|
197
|
+
ctx.reply(requestId, true, {requestId: `sim-fov-${mintBleId()}`, fov, roiPosition, timestamp: Date.now()})
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function roiName(value: unknown): string {
|
|
201
|
+
if (typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= 2) return ROI_POSITIONS[value]
|
|
202
|
+
return typeof value === "string" && ROI_POSITIONS.includes(value) ? value : "center"
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function videoStart(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string) {
|
|
206
|
+
if (!ctx.requirePermission(requestId, "CAMERA", MiniappRequestType.VIDEO_RECORDING_START)) return
|
|
207
|
+
if (glassesGone(ctx, requestId)) return
|
|
208
|
+
const state = stateOf(ctx)
|
|
209
|
+
if (state.recordings.size > 0) return ctx.fail(requestId, "ALREADY_RECORDING", HARDWARE_MESSAGES.ALREADY_RECORDING)
|
|
210
|
+
const recordingId = `miniapp_video_${Date.now()}_${Math.round(Math.random() * 1e6)}`
|
|
211
|
+
if (!hasCamera(ctx)) return timedOut(ctx, requestId, `start video recording ${recordingId}`, 15)
|
|
212
|
+
|
|
213
|
+
ctx.fake(MiniappRequestType.VIDEO_RECORDING_START, "the simulated glasses record nothing")
|
|
214
|
+
state.recordings.add(recordingId)
|
|
215
|
+
const {width, height, fps, sound, save} = payload
|
|
216
|
+
ctx.effect("video", {action: "start", recordingId, width, height, fps, sound: sound ?? true, save: save ?? false})
|
|
217
|
+
ctx.reply(requestId, true, {recordingId})
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function videoStop(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string) {
|
|
221
|
+
const recordingId = payload.recordingId
|
|
222
|
+
if (typeof recordingId !== "string" || !recordingId) {
|
|
223
|
+
return ctx.fail(requestId, "INVALID_RECORDING_ID", HARDWARE_MESSAGES.INVALID_RECORDING_ID)
|
|
224
|
+
}
|
|
225
|
+
const state = stateOf(ctx)
|
|
226
|
+
if (!state.recordings.has(recordingId)) {
|
|
227
|
+
return ctx.fail(requestId, "RECORDING_NOT_OWNED", HARDWARE_MESSAGES.RECORDING_NOT_OWNED)
|
|
228
|
+
}
|
|
229
|
+
state.recordings.delete(recordingId)
|
|
230
|
+
ctx.effect("video", {action: "stop", recordingId, uploadUrl: payload.uploadUrl})
|
|
231
|
+
if (typeof payload.uploadUrl === "string") ctx.log("info", "glassly sim uploads no video; the upload URL receives nothing")
|
|
232
|
+
ctx.reply(requestId, true)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function mintStreamId(prefix: "u" | "m"): string {
|
|
236
|
+
return `phone-${prefix}-${Date.now().toString(36)}-${++streamCounter}`
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// The glasses' first stream_status after a start, which the phone forwards to the owner.
|
|
240
|
+
function sendStatus(ctx: SimTypeContext, streamId: string) {
|
|
241
|
+
ctx.deliver({
|
|
242
|
+
type: MiniappResponseType.EVENT,
|
|
243
|
+
streamType: "stream_status",
|
|
244
|
+
data: {streamId, status: "streaming", source: "glasses", simulated: true},
|
|
245
|
+
})
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function streamStart(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string) {
|
|
249
|
+
if (typeof payload.streamUrl !== "string" || !payload.streamUrl) {
|
|
250
|
+
return ctx.fail(requestId, "STREAM_URL_REQUIRED", HARDWARE_MESSAGES.STREAM_URL_REQUIRED)
|
|
251
|
+
}
|
|
252
|
+
const state = stateOf(ctx)
|
|
253
|
+
if (state.stream) {
|
|
254
|
+
return ctx.fail(
|
|
255
|
+
requestId,
|
|
256
|
+
"STREAM_ALREADY_ACTIVE",
|
|
257
|
+
`A ${state.stream.kind} stream is already active. Stop it before starting a new one.`,
|
|
258
|
+
)
|
|
259
|
+
}
|
|
260
|
+
if (glassesGone(ctx, requestId, BLE)) return
|
|
261
|
+
const streamId = mintStreamId("u")
|
|
262
|
+
if (!hasCamera(ctx)) return timedOut(ctx, requestId, `start stream ${streamId}`, 30)
|
|
263
|
+
|
|
264
|
+
ctx.fake(MiniappRequestType.STREAM_START, "the simulated glasses publish nothing to the stream URL")
|
|
265
|
+
const result = {streamId, status: "streaming", simulated: true}
|
|
266
|
+
state.stream = {kind: "unmanaged", streamId, result}
|
|
267
|
+
ctx.effect("stream", {action: "start", streamId, streamUrl: payload.streamUrl})
|
|
268
|
+
ctx.reply(requestId, true, result)
|
|
269
|
+
sendStatus(ctx, streamId)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function managedStreamStart(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string) {
|
|
273
|
+
const state = stateOf(ctx)
|
|
274
|
+
if (state.stream?.kind === "unmanaged") {
|
|
275
|
+
return ctx.fail(
|
|
276
|
+
requestId,
|
|
277
|
+
"STREAM_ALREADY_ACTIVE",
|
|
278
|
+
"An unmanaged stream is already active. Stop it before starting a managed stream.",
|
|
279
|
+
)
|
|
280
|
+
}
|
|
281
|
+
if (state.stream) {
|
|
282
|
+
const destinations = payload.restreamDestinations
|
|
283
|
+
if (Array.isArray(destinations) && destinations.length > 0) {
|
|
284
|
+
return ctx.fail(requestId, "STREAM_DESTINATIONS_LOCKED", HARDWARE_MESSAGES.STREAM_DESTINATIONS_LOCKED)
|
|
285
|
+
}
|
|
286
|
+
return ctx.reply(requestId, true, state.stream.result)
|
|
287
|
+
}
|
|
288
|
+
if (glassesGone(ctx, requestId, BLE)) return
|
|
289
|
+
const streamId = mintStreamId("m")
|
|
290
|
+
if (!hasCamera(ctx)) return timedOut(ctx, requestId, `start stream ${streamId}`, 30)
|
|
291
|
+
|
|
292
|
+
ctx.fake(MiniappRequestType.MANAGED_STREAM_START, "no live input is provisioned; the playback URLs do not resolve")
|
|
293
|
+
const liveInputId = `sim-${streamId}`
|
|
294
|
+
const base = `https://${STAND_IN_HOST}/${liveInputId}`
|
|
295
|
+
const webrtc = payload.ingest === "whip"
|
|
296
|
+
const result = {
|
|
297
|
+
streamId,
|
|
298
|
+
status: "streaming",
|
|
299
|
+
liveInputId,
|
|
300
|
+
mode: webrtc ? "webrtc" : "hls",
|
|
301
|
+
hlsUrl: `${base}/manifest/video.m3u8`,
|
|
302
|
+
dashUrl: `${base}/manifest/video.mpd`,
|
|
303
|
+
...(webrtc ? {webrtcUrl: `${base}/webRTC/play`} : {}),
|
|
304
|
+
simulated: true,
|
|
305
|
+
}
|
|
306
|
+
state.stream = {kind: "managed", streamId, result}
|
|
307
|
+
ctx.effect("stream", {action: "start", streamId, managed: true, ingest: payload.ingest ?? "srt"})
|
|
308
|
+
ctx.reply(requestId, true, result)
|
|
309
|
+
sendStatus(ctx, streamId)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function streamStop(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string) {
|
|
313
|
+
const state = stateOf(ctx)
|
|
314
|
+
const streamId = payload.streamId
|
|
315
|
+
if (state.stream && (typeof streamId !== "string" || !streamId || streamId === state.stream.streamId)) {
|
|
316
|
+
ctx.effect("stream", {action: "stop", streamId: state.stream.streamId})
|
|
317
|
+
state.stream = undefined
|
|
318
|
+
}
|
|
319
|
+
ctx.reply(requestId, true)
|
|
320
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {AUDIO_MESSAGES, handleAudio} from "./audio.js"
|
|
2
|
+
import {BLOB_MESSAGES, handleBlobs} from "./blobs.js"
|
|
3
|
+
import type {SimTypeHandler} from "./context.js"
|
|
4
|
+
import {HARDWARE_MESSAGES, handleHardware} from "./hardware.js"
|
|
5
|
+
import {NAVIGATION_MESSAGES, handleNavigation} from "./navigation.js"
|
|
6
|
+
import {SERVICE_MESSAGES, handleServices} from "./services.js"
|
|
7
|
+
|
|
8
|
+
export type {SimTypeContext, SimTypeHandler} from "./context.js"
|
|
9
|
+
|
|
10
|
+
export const TYPE_HANDLERS: SimTypeHandler[] = [handleBlobs, handleServices, handleNavigation, handleHardware, handleAudio]
|
|
11
|
+
export const FORCED_MESSAGES: Record<string, string> = {
|
|
12
|
+
...BLOB_MESSAGES,
|
|
13
|
+
...SERVICE_MESSAGES,
|
|
14
|
+
...NAVIGATION_MESSAGES,
|
|
15
|
+
...HARDWARE_MESSAGES,
|
|
16
|
+
...AUDIO_MESSAGES,
|
|
17
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/// <reference types="bun-types" />
|
|
2
|
+
|
|
3
|
+
import {expect, test} from "bun:test"
|
|
4
|
+
import {MiniappSession, type Transport} from "@glassly/miniapp"
|
|
5
|
+
|
|
6
|
+
import {simDevice} from "../device.js"
|
|
7
|
+
import {SimHost, type SimManifest} from "../host.js"
|
|
8
|
+
import {createSimStorage} from "../storage.js"
|
|
9
|
+
|
|
10
|
+
const tripState = (session: MiniappSession) =>
|
|
11
|
+
session.sendRequest<{state: Record<string, unknown> | null}>({type: "miniapp_navigation_get_state"}).then((reply) => reply.state)
|
|
12
|
+
|
|
13
|
+
async function connect(manifest: Partial<SimManifest> = {}, device = "simulated") {
|
|
14
|
+
let deliver: (raw: string) => void = () => {}
|
|
15
|
+
const host = new SimHost({
|
|
16
|
+
manifest: {packageName: "com.example.sim", name: "Sim Test", permissions: [{type: "LOCATION"}], ...manifest},
|
|
17
|
+
device: simDevice(device),
|
|
18
|
+
storage: createSimStorage(),
|
|
19
|
+
toBackground: (raw) => queueMicrotask(() => deliver(raw)),
|
|
20
|
+
emit: () => {},
|
|
21
|
+
frameLatencyMs: 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
|
+
await session.connect()
|
|
36
|
+
return {host, session}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
test("a simulated trip departs, arrives, and is cleared by stop", async () => {
|
|
40
|
+
const {session} = await connect()
|
|
41
|
+
const kinds: string[] = []
|
|
42
|
+
session.navigation.onUpdate((update) => kinds.push(update.kind === "maneuver" ? update.maneuverType : update.kind))
|
|
43
|
+
|
|
44
|
+
const started = await session.navigation.start({lat: 37.78, lng: -122.41, simulate: true, speedMultiplier: 300})
|
|
45
|
+
expect(started.ok).toBe(true)
|
|
46
|
+
expect(await tripState(session)).toMatchObject({active: true, mode: "walking", stops: [{lat: 37.78, lng: -122.41}]})
|
|
47
|
+
|
|
48
|
+
await Bun.sleep(60)
|
|
49
|
+
expect(kinds).toEqual(["DEPART", "ARRIVE", "arrived"])
|
|
50
|
+
expect(await tripState(session)).toMatchObject({active: false})
|
|
51
|
+
|
|
52
|
+
session.navigation.stop()
|
|
53
|
+
await Bun.sleep(5)
|
|
54
|
+
expect(await tripState(session)).toBeNull()
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test("a second start during a trip resumes it", async () => {
|
|
58
|
+
const {session} = await connect()
|
|
59
|
+
await session.navigation.start({lat: 37.78, lng: -122.41})
|
|
60
|
+
const again = await session.sendRequest({type: "miniapp_navigation_start", lat: 37.79, lng: -122.42})
|
|
61
|
+
expect(again).toMatchObject({ok: true, resumed: true, snapshot: {active: true, stops: [{lat: 37.78, lng: -122.41}]}})
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test("G2 native navigation is live only during a trip and blocks the native dashboard", async () => {
|
|
65
|
+
const {session} = await connect({}, "g2")
|
|
66
|
+
expect(await session.display.sendNativeNavInfo({distance: "100 m"} as never)).toEqual({
|
|
67
|
+
status: "unsupported",
|
|
68
|
+
reason: "navigation mode not active",
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
await session.navigation.start({lat: 37.78, lng: -122.41})
|
|
72
|
+
expect(await session.display.sendNativeNavInfo({distance: "100 m"} as never)).toEqual({status: "sent"})
|
|
73
|
+
expect(await session.display.sendNativeNavMiniMap("")).toEqual({
|
|
74
|
+
status: "unsupported",
|
|
75
|
+
reason: "native navigation unavailable",
|
|
76
|
+
})
|
|
77
|
+
expect(await session.dashboard.showNativeDashboard()).toEqual({status: "unsupported", reason: "native navigation active"})
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test("custom firmware keeps the scene HUD, so native navigation stays off during a trip", async () => {
|
|
81
|
+
const {session} = await connect({}, "g2-cfw")
|
|
82
|
+
await session.navigation.start({lat: 37.78, lng: -122.41})
|
|
83
|
+
expect(await session.display.sendNativeNavInfo({distance: "100 m"} as never)).toEqual({
|
|
84
|
+
status: "unsupported",
|
|
85
|
+
reason: "navigation mode not active",
|
|
86
|
+
})
|
|
87
|
+
expect(await session.dashboard.showNativeDashboard()).toEqual({status: "shown"})
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test("a picked suggestion resolves to the place it named", async () => {
|
|
91
|
+
const {session} = await connect()
|
|
92
|
+
const {suggestions} = await session.navigation.placeAutocomplete({query: "coffee", sessionToken: "t"})
|
|
93
|
+
expect(suggestions?.length).toBeGreaterThan(0)
|
|
94
|
+
const {place} = await session.navigation.placeDetails({placeId: suggestions![0].placeId, sessionToken: "t"})
|
|
95
|
+
expect(place?.name).toBe(suggestions![0].mainText)
|
|
96
|
+
const route = await session.navigation.computeRoute({origin: {lat: 37.7749, lng: -122.4194}, stops: [place!]})
|
|
97
|
+
expect(route.routes?.[0].points.at(-1)).toEqual({lat: place!.lat, lng: place!.lng})
|
|
98
|
+
})
|