File size: 5,381 Bytes
8a29801
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/// <reference types="bun-types" />

import {describe, expect, test} from "bun:test"
import {readFileSync} from "node:fs"

import {
  StreamPreviewError,
  type StreamPreviewHostPort,
  type StreamPreviewStartRequest,
  type StreamPreviewStatusEvent,
} from "../streamPreviewPort"

// The real preview handlers, extracted from the runtime source like the SoftAP suite does, so the
// identity plumbing under test is the one that ships without loading the Expo app singleton.
const source = readFileSync(new URL("../LocalMiniappRuntime.ts", import.meta.url), "utf8")
const methods = ["hasManifestPermission", "handleStreamPreviewStart", "handleStreamPreviewStop"]
  .map((name) => {
    const start = source.search(new RegExp(`^  (?:private|public) (?:async )?${name}\\(`, "m"))
    if (start < 0) throw new Error(`Missing runtime method ${name}`)
    const rest = source.slice(start)
    const end = rest.search(/^ {2}}$/m)
    return rest.slice(0, end + 3)
  })
  .join("\n")
const compiled = new Bun.Transpiler({loader: "ts"}).transformSync(`class Host { ${methods} }`)

function build(previewHost: StreamPreviewHostPort | null, permissions = [{type: "CAMERA"}]) {
  const Host = new Function(
    "getStreamPreviewHost",
    "MiniappResponseType",
    "LOG_TAG",
    "console",
    `${compiled}; return Host`,
  )(() => previewHost, {STREAM_PREVIEW_STATUS: "miniapp_stream_preview_status"}, "test", {
    warn: () => {},
    log: () => {},
  })
  const host = new Host()
  const results: Array<{ok: boolean; data?: unknown; error?: {code: string}}> = []
  const pushes: unknown[] = []
  host.connectedApps = new Map([["com.test.call", {runtimeId: "rt-1", installedManifest: {permissions}}]])
  host.sendResult = (_pkg: string, _id: string, ok: boolean, data?: unknown, error?: {code: string}) =>
    results.push({ok, data, error})
  host.sendToMiniapp = (_pkg: string, payload: unknown) => pushes.push(payload)
  return {host, results, pushes}
}

function fakePort() {
  const starts: StreamPreviewStartRequest[] = []
  const stops: unknown[] = []
  let notify: ((event: StreamPreviewStatusEvent) => void) | null = null
  const port: StreamPreviewHostPort = {
    async start(request, n) {
      starts.push(request)
      notify = n
      return {handleId: "h1", previewTraceId: "p1", source: "call"}
    },
    async stop(request) {
      stops.push(request)
    },
    releaseRuntime() {},
  }
  return {port, starts, stops, notify: (event: StreamPreviewStatusEvent) => notify?.(event)}
}

describe("LocalMiniappRuntime stream preview handlers", () => {
  test("start forwards identity and the CAMERA declaration, and replies with the lease", async () => {
    const fake = fakePort()
    const {host, results} = build(fake.port)
    await host.handleStreamPreviewStart("com.test.call", {source: "call"}, "r1")
    expect(fake.starts).toEqual([{packageName: "com.test.call", runtimeId: "rt-1", source: "call", hasCamera: true}])
    expect(results).toEqual([
      {ok: true, data: {handleId: "h1", previewTraceId: "p1", source: "call"}, error: undefined},
    ])
  })

  test("a missing CAMERA declaration is passed on for the coordinator to refuse", async () => {
    const fake = fakePort()
    const {host} = build(fake.port, [])
    await host.handleStreamPreviewStart("com.test.call", {source: "call"}, "r1")
    expect(fake.starts[0]!.hasCamera).toBe(false)
  })

  test("typed refusals keep their code", async () => {
    const port: StreamPreviewHostPort = {
      start: async () => {
        throw new StreamPreviewError("not_meeting_owner", "Only the meeting owner may preview its call")
      },
      stop: async () => {},
      releaseRuntime: () => {},
    }
    const {host, results} = build(port)
    await host.handleStreamPreviewStart("com.test.call", {source: "call"}, "r1")
    expect(results[0]).toMatchObject({ok: false, error: {code: "not_meeting_owner"}})
  })

  test("a host without a coordinator answers unsupported", async () => {
    const {host, results} = build(null)
    await host.handleStreamPreviewStart("com.test.call", {source: "call"}, "r1")
    expect(results[0]).toMatchObject({ok: false, error: {code: "unsupported"}})
  })

  test("status pushes reach the runtime that took the lease, never its respawned successor", async () => {
    const fake = fakePort()
    const {host, pushes} = build(fake.port)
    await host.handleStreamPreviewStart("com.test.call", {source: "call"}, "r1")
    fake.notify({handleId: "h1", state: "held"})
    host.connectedApps.set("com.test.call", {runtimeId: "rt-2", installedManifest: {permissions: []}})
    fake.notify({handleId: "h1", state: "ended", reason: "runtime_replaced"})
    expect(pushes).toEqual([{type: "miniapp_stream_preview_status", handleId: "h1", state: "held"}])
  })

  test("stop carries the runtime identity and always succeeds from the miniapp's side", async () => {
    const fake = fakePort()
    const {host, results} = build(fake.port)
    await host.handleStreamPreviewStop("com.test.call", {handleId: "h1"}, "r2")
    await host.handleStreamPreviewStop("com.test.call", {}, "r3")
    expect(fake.stops).toEqual([{packageName: "com.test.call", runtimeId: "rt-1", handleId: "h1"}])
    expect(results.map((r) => r.ok)).toEqual([true, true])
  })
})