File size: 22,236 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | import {afterAll, beforeEach, describe, expect, test} from "bun:test"
import {readFileSync} from "node:fs"
import {MiniappErrorCode} from "@mentra/miniapp"
import {configure, getConfigValues, resetForTests} from "../../runtime/bootstrap"
import {DeviceTypes, getModelCapabilities} from "../../types"
import type {ClientApp} from "../../types"
import type {InstallMiniappErrorCode, InstallMiniappOptions, InstallMiniappResult} from "../installMiniapp"
import {projectSystemActions} from "../manifestActions"
import {checkMiniappInstallCompatibility} from "../miniappInstallCompatibility"
import {isHostTrustedSystemMiniapp, isSystemMiniappPackage} from "../SystemMiniappPolicy"
// Exercise the real install request handlers without booting native audio or
// Bluetooth, following the runtime's other isolated handler suites.
const source = readFileSync(new URL("../LocalMiniappRuntime.ts", import.meta.url), "utf8")
const methods = [
"isSystemPackage",
"requireSystemListCaller",
"buildMiniappInfo",
"currentInstallCompatibilityPolicy",
"evaluateInstallCompatibility",
"hasDevelopmentSelection",
"handleMiniappsInstall",
"handleMiniappsInstallCheck",
"uninstallRefusal",
"handleMiniappsUninstall",
]
.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} }`)
const SYSTEM_MINIAPP_PACKAGE_SET = new Set(["com.mentra.ai"])
// installMiniapp.ts loads the whole runtime; its error class has this shape.
class InstallMiniappError extends Error {
constructor(
readonly code: InstallMiniappErrorCode,
message: string,
) {
super(message)
}
}
const ai = "com.mentra.ai"
const notes = "com.mentra.notes"
const weather = "com.example.weather"
const url = "https://weather.example.com/"
interface Result {
ok: boolean
data?: unknown
error?: {code: string; message: string; reason?: string}
}
interface Host {
connectedApps: Map<string, {hostTrustedSystem: boolean}>
installSessionEpoch: number
interopApps: () => ClientApp[]
stopInteropApp: (pkg: string) => Promise<void>
sendResult: (pkg: string, requestId: string | undefined, ok: boolean, data?: unknown, error?: unknown) => void
auditInterop: (event: Record<string, unknown>) => void
isSystemPackage: (pkg: string) => boolean
buildMiniappInfo: (app: ClientApp) => Record<string, unknown>
handleMiniappsInstall: (pkg: string, payload: Record<string, unknown>, requestId?: string) => Promise<void>
handleMiniappsInstallCheck: (pkg: string, payload: Record<string, unknown>, requestId?: string) => void
handleMiniappsUninstall: (pkg: string, payload: Record<string, unknown>, requestId?: string) => Promise<void>
}
let identities: Record<string, {source: string; storePackageName?: string; sourceUrl?: string} | null>
/** Packages whose context started as a local or development build. */
const localBuilds = new Set<string>()
let reasons: Record<string, string[]>
let devRecords: Array<{packageName: string}>
let installs: Array<{url: string; options: InstallMiniappOptions}>
let installImplementation: (url: string, options: InstallMiniappOptions) => Promise<InstallMiniappResult>
let uninstalled: string[]
let uninstallImplementation: (pkg: string) => Promise<{is_error: () => boolean; error?: Error}>
type UninstallResult = {is_error: () => boolean; error?: Error}
const HostClass = new Function(
"SYSTEM_MINIAPP_PACKAGE_SET",
"MiniappErrorCode",
"appRegistry",
"getDevAppRecords",
"installMiniapp",
"InstallMiniappError",
"useAppStatusStore",
"getConfigValues",
"useSettingsStore",
"ISLAND_SETTINGS_KEYS",
"getModelCapabilities",
"DeviceTypes",
"checkMiniappInstallCompatibility",
"isSystemMiniappPackage",
"isHostTrustedSystemMiniapp",
"projectSystemActions",
"isLocalBuildContext",
`${compiled}; return Host`,
)(
SYSTEM_MINIAPP_PACKAGE_SET,
MiniappErrorCode,
{
getActiveReleaseVersion: (pkg: string) => (identities[pkg] ? "1.0.0" : null),
getReleaseIdentity: (pkg: string) => identities[pkg] ?? null,
getInstallReasons: (pkg: string) => reasons[pkg] ?? (identities[pkg] ? ["explicit"] : []),
getSelectedDevSnapshot: () => null,
},
() => devRecords,
async (target: string, options: InstallMiniappOptions) => {
installs.push({url: target, options})
return installImplementation(target, options)
},
InstallMiniappError,
{
getState: () => ({
// The real store runs beforeRemove inside AppRegistry's serialized uninstall, before anything is removed.
uninstall: async (pkg: string, _version?: string, options?: {beforeRemove?: () => void}): Promise<UninstallResult> => {
try {
options?.beforeRemove?.()
} catch (error) {
return {is_error: () => true, error: error as Error}
}
uninstalled.push(pkg)
return uninstallImplementation(pkg)
},
}),
},
getConfigValues,
{getState: () => ({getSetting: () => DeviceTypes.LIVE})},
{defaultWearable: "default_wearable"},
getModelCapabilities,
DeviceTypes,
checkMiniappInstallCompatibility,
isSystemMiniappPackage,
isHostTrustedSystemMiniapp,
projectSystemActions,
(packageName: string) => localBuilds.has(packageName),
) as new () => Host
function app(packageName: string, overrides: Partial<ClientApp> = {}): ClientApp {
const listed: Partial<ClientApp> = {
packageName,
name: packageName,
version: "1.0.0",
running: false,
local: true,
actions: [
{id: "open", description: "Open.", lifecycle: "persistent", audience: "system"},
{id: "maintain", description: "Maintain.", lifecycle: "transient", audience: "host"},
],
...overrides,
}
return listed as ClientApp
}
function isTrusted(listed: ClientApp): boolean {
return isHostTrustedSystemMiniapp(listed.packageName, identities[listed.packageName])
}
function harness(apps: ClientApp[] = [app(ai)]) {
const host = new HostClass()
const results: Result[] = []
const audits: Array<Record<string, unknown>> = []
const stopped: string[] = []
// Each listed miniapp runs the release its tile names, which the launcher trusted.
host.connectedApps = new Map(
apps.map((listed) => [listed.packageName, {hostTrustedSystem: !listed.isMiniappDev && isTrusted(listed)}]),
)
host.installSessionEpoch = 0
host.interopApps = () => apps
host.stopInteropApp = async (pkg) => {
stopped.push(pkg)
}
host.auditInterop = (event) => audits.push(event)
host.sendResult = (_pkg, _id, ok, data, error) => results.push({ok, data, error: error as Result["error"]})
return {host, results, audits, stopped}
}
beforeEach(() => {
resetForTests()
configure({
auth: {},
config: {
bundledSystemMiniappPackages: [ai, notes],
hostVersion: "3.4.0",
supportedMiniappSdkRange: ">=0.3.0 <4.0.0",
},
})
identities = {[ai]: {source: "bundled_asset"}, [notes]: {source: "bundled_asset"}}
reasons = {}
localBuilds.clear()
devRecords = []
installs = []
installImplementation = async () => ({status: "installed", packageName: weather, version: "1.2.0"})
uninstalled = []
uninstallImplementation = async () => ({is_error: () => false})
})
afterAll(resetForTests)
describe("installing from a system miniapp", () => {
test("a miniapp on the system list installs a miniapp URL headlessly, through installMiniapp", async () => {
const h = harness()
await h.host.handleMiniappsInstall(ai, {url: ` ${url} `}, "r1")
expect(installs).toEqual([{url, options: {source: "system"}}])
expect(installs[0].options.confirm).toBeUndefined()
expect(h.results).toEqual([
{ok: true, data: {status: "installed", packageName: weather, version: "1.2.0"}, error: undefined},
])
expect(h.audits).toEqual([{caller: ai, op: "install", target: weather, ok: true}])
})
test("answers what installMiniapp answers: opened and deferred are not failures", async () => {
const h = harness()
installImplementation = async () => ({status: "opened", packageName: weather, version: "1.2.0"})
await h.host.handleMiniappsInstall(ai, {url}, "r1")
installImplementation = async () => ({status: "deferred", packageName: weather, version: "1.1.0"})
await h.host.handleMiniappsInstall(ai, {url}, "r2")
expect(h.results.map((result) => result.data)).toEqual([
{status: "opened", packageName: weather, version: "1.2.0"},
{status: "deferred", packageName: weather, version: "1.1.0"},
])
})
test("keeps its system privileges after an update from a Mentra host, or from the retired Mentra Store", async () => {
identities[ai] = {source: "url", sourceUrl: "https://miniapps.mentraglass.com/com.mentra.ai/"}
await harness().host.handleMiniappsInstall(ai, {url}, "r1")
identities[ai] = {source: "system_store", storePackageName: "com.mentra.store"}
await harness().host.handleMiniappsInstall(ai, {url}, "r2")
expect(installs).toHaveLength(2)
})
test.each([
["a third-party miniapp", weather, [app(weather)], {[weather]: {source: "url", sourceUrl: url}}],
["a bundled miniapp that isn't on the system list", notes, [app(notes)], {}],
["a development build", "com.example.tool", [app("com.example.tool", {isMiniappDev: true})], {}],
["a system-list package installed from a release QR", ai, [app(ai)], {[ai]: {source: "direct_download"}}],
[
"a system-list package from a host that can't serve its name",
ai,
[app(ai)],
{[ai]: {source: "url", sourceUrl: "https://ai.example.com/"}},
],
[
"a system-list package another Store updated",
ai,
[app(ai)],
{[ai]: {source: "system_store", storePackageName: "com.oem.store"}},
],
["the retired Store miniapp", "com.mentra.store", [], {"com.mentra.store": {source: "bundled_asset"}}],
])("%s gets NOT_PERMITTED, and nothing installs", async (_name, caller, apps, overrides) => {
Object.assign(identities, overrides)
const h = harness(apps)
h.host.connectedApps.set(caller, {hostTrustedSystem: true})
await h.host.handleMiniappsInstall(caller, {url}, "r1")
expect(h.results).toEqual([
{ok: false, data: undefined, error: expect.objectContaining({code: MiniappErrorCode.NOT_PERMITTED})},
])
expect(h.audits).toEqual([{caller, op: "install", ok: false, errorCode: MiniappErrorCode.NOT_PERMITTED}])
expect(installs).toEqual([])
})
test("a context that only reuses a system package's name gets NOT_PERMITTED, whatever its tile says", async () => {
// The installed com.mentra.ai is the bundled release, but what runs under its name is a development build.
const h = harness()
h.host.connectedApps.set(ai, {hostTrustedSystem: false})
await h.host.handleMiniappsInstall(ai, {url}, "r1")
h.host.handleMiniappsInstallCheck(ai, {}, "r2")
await h.host.handleMiniappsUninstall(ai, {packageName: weather}, "r3")
expect(h.results.map((result) => result.error?.code)).toEqual([
MiniappErrorCode.NOT_PERMITTED,
MiniappErrorCode.NOT_PERMITTED,
MiniappErrorCode.NOT_PERMITTED,
])
expect(installs).toEqual([])
h.host.connectedApps.delete(ai)
await h.host.handleMiniappsInstall(ai, {url}, "r4")
expect(h.results[3].error?.code).toBe(MiniappErrorCode.NOT_PERMITTED)
expect(installs).toEqual([])
})
test.each([{}, {url: ""}, {url: " "}, {url: 42}])("refuses %j without installing", async (payload) => {
const h = harness()
await h.host.handleMiniappsInstall(ai, payload, "r1")
expect(h.results[0].error?.code).toBe(MiniappErrorCode.INVALID_ARGUMENT)
expect(installs).toEqual([])
})
test("names the package the caller agreed to, for installMiniapp to hold the URL to", async () => {
const h = harness()
await h.host.handleMiniappsInstall(ai, {url, packageName: ` ${weather} `}, "r1")
expect(installs).toEqual([{url, options: {source: "system", expectedPackageName: weather}}])
expect(h.results[0].ok).toBe(true)
})
test.each([{packageName: ""}, {packageName: " "}, {packageName: 42}, {packageName: null}])(
"refuses a URL with packageName %j without installing",
async (extra) => {
const h = harness()
await h.host.handleMiniappsInstall(ai, {url, ...extra}, "r1")
expect(h.results[0].error?.code).toBe(MiniappErrorCode.INVALID_ARGUMENT)
expect(installs).toEqual([])
},
)
test("the old release request installs nothing: an install names a URL", async () => {
const h = harness()
await h.host.handleMiniappsInstall(
ai,
{packageName: weather, version: "1.2.0", bundleUrl: `${url}weather.zip`, bundleSha256: "a".repeat(64)},
"r1",
)
expect(h.results[0].error?.code).toBe(MiniappErrorCode.INVALID_ARGUMENT)
expect(installs).toEqual([])
})
test("a failed install answers INSTALL_FAILED, with installMiniapp's code as its reason", async () => {
const h = harness()
for (const code of ["archived", "domain_mismatch", "needs_newer_app", "access_denied", "network"] as const) {
installImplementation = async () => {
throw new InstallMiniappError(code, `failed: ${code}`)
}
await h.host.handleMiniappsInstall(ai, {url}, code)
}
installImplementation = async () => {
throw new Error("disk full")
}
await h.host.handleMiniappsInstall(ai, {url}, "other")
expect(h.results.map((result) => result.error)).toEqual([
{code: MiniappErrorCode.INSTALL_FAILED, message: "failed: archived", reason: "archived"},
{code: MiniappErrorCode.INSTALL_FAILED, message: "failed: domain_mismatch", reason: "domain_mismatch"},
{code: MiniappErrorCode.INSTALL_FAILED, message: "failed: needs_newer_app", reason: "needs_newer_app"},
{code: MiniappErrorCode.INSTALL_FAILED, message: "failed: access_denied", reason: "access_denied"},
{code: MiniappErrorCode.INSTALL_FAILED, message: "failed: network", reason: "network"},
{code: MiniappErrorCode.INSTALL_FAILED, message: "disk full"},
])
expect(h.audits.every((event) => event.ok === false && event.errorCode === MiniappErrorCode.INSTALL_FAILED)).toBe(
true,
)
})
})
describe("compatibility checks", () => {
test("answer a system-list miniapp, and refuse any other caller", () => {
const h = harness([app(ai), app(weather)])
h.host.handleMiniappsInstallCheck(ai, {minHostVersion: "9.0.0"}, "r1")
h.host.handleMiniappsInstallCheck(ai, {minHostVersion: "3.2.1", sdkVersion: "3.2.1"}, "r2")
h.host.handleMiniappsInstallCheck(weather, {}, "r3")
expect(h.results[0]).toMatchObject({ok: true, data: {compatible: false, blocker: "host"}})
expect(h.results[1]).toEqual({ok: true, data: {compatible: true}, error: undefined})
expect(h.results[2].error?.code).toBe(MiniappErrorCode.NOT_PERMITTED)
})
})
describe("uninstalling from a system miniapp", () => {
test("a miniapp installed from its URL uninstalls, stopping it first when it runs", async () => {
identities[weather] = {source: "url", sourceUrl: url}
const h = harness([app(ai), app(weather, {running: true})])
await h.host.handleMiniappsUninstall(ai, {packageName: weather}, "r1")
expect(h.stopped).toEqual([weather])
expect(uninstalled).toEqual([weather])
expect(h.results).toEqual([{ok: true, data: undefined, error: undefined}])
expect(h.audits).toEqual([{caller: ai, op: "uninstall", target: weather, ok: true}])
})
test.each([
["bundled with the Mentra App", notes, {[notes]: {source: "url", sourceUrl: "https://miniapps.mentraglass.com/x/"}}, {}],
["a workspace's deployment release", weather, {[weather]: {source: "deployment_manifest"}}, {}],
["a Fleet assignment", weather, {[weather]: {source: "url", sourceUrl: url}}, {[weather]: ["explicit", "assignment"]}],
["a release-QR install", weather, {[weather]: {source: "direct_download"}}, {}],
["a Store's install", weather, {[weather]: {source: "store", storePackageName: "com.mentra.store"}}, {}],
])("a miniapp %s stays", async (_name, target, overrides, overrideReasons) => {
Object.assign(identities, overrides)
Object.assign(reasons, overrideReasons)
const h = harness([app(ai), app(target)])
await h.host.handleMiniappsUninstall(ai, {packageName: target}, "r1")
expect(h.results[0].error?.code).toBe(MiniappErrorCode.NOT_PERMITTED)
expect(uninstalled).toEqual([])
})
test("a Fleet assignment that arrives while the miniapp stops keeps it installed", async () => {
identities[weather] = {source: "url", sourceUrl: url}
const h = harness([app(ai), app(weather, {running: true})])
h.host.stopInteropApp = async () => {
reasons[weather] = ["explicit", "assignment"]
}
await h.host.handleMiniappsUninstall(ai, {packageName: weather}, "r1")
expect(h.results[0].error?.code).toBe(MiniappErrorCode.NOT_PERMITTED)
expect(uninstalled).toEqual([])
})
test("an engine session that ends while the miniapp stops uninstalls nothing", async () => {
identities[weather] = {source: "url", sourceUrl: url}
const h = harness([app(ai), app(weather, {running: true})])
h.host.stopInteropApp = async () => {
h.host.installSessionEpoch += 1
}
await h.host.handleMiniappsUninstall(ai, {packageName: weather}, "r1")
expect(h.results[0].error).toEqual({
code: MiniappErrorCode.INTERNAL,
message: `The uninstall of ${weather} was cancelled`,
})
expect(uninstalled).toEqual([])
})
test("a development build of a URL install stays", async () => {
identities[weather] = {source: "url", sourceUrl: url}
devRecords = [{packageName: weather}]
const h = harness([app(ai), app(weather)])
await h.host.handleMiniappsUninstall(ai, {packageName: weather}, "r1")
expect(h.results[0].error?.code).toBe(MiniappErrorCode.NOT_PERMITTED)
expect(uninstalled).toEqual([])
})
test("a package that isn't installed is APP_NOT_FOUND, and a missing name INVALID_ARGUMENT", async () => {
const h = harness()
await h.host.handleMiniappsUninstall(ai, {packageName: weather}, "r1")
await h.host.handleMiniappsUninstall(ai, {}, "r2")
expect(h.results.map((result) => result.error?.code)).toEqual([
MiniappErrorCode.APP_NOT_FOUND,
MiniappErrorCode.INVALID_ARGUMENT,
])
})
test("any caller off the system list gets NOT_PERMITTED", async () => {
identities[weather] = {source: "url", sourceUrl: url}
const h = harness([app(weather), app("com.example.tool", {isMiniappDev: true})])
await h.host.handleMiniappsUninstall(weather, {packageName: weather}, "r1")
await h.host.handleMiniappsUninstall("com.example.tool", {packageName: weather}, "r2")
expect(h.results.map((result) => result.error?.code)).toEqual([
MiniappErrorCode.NOT_PERMITTED,
MiniappErrorCode.NOT_PERMITTED,
])
expect(uninstalled).toEqual([])
})
test("an uninstall the host refuses answers INTERNAL", async () => {
identities[weather] = {source: "url", sourceUrl: url}
uninstallImplementation = async () => ({is_error: () => true, error: new Error("busy")})
const h = harness([app(ai), app(weather)])
await h.host.handleMiniappsUninstall(ai, {packageName: weather}, "r1")
expect(h.results[0].error).toEqual({code: MiniappErrorCode.INTERNAL, message: "busy"})
})
})
describe("what a system miniapp sees", () => {
test("lists which miniapps the build bundles, never who updates them or host-only actions", () => {
const {host} = harness()
identities[weather] = {source: "url", sourceUrl: url}
const listed = host.buildMiniappInfo(app(notes))
expect(listed).toMatchObject({system: true, actions: [{id: "open", description: "Open.", lifecycle: "persistent"}]})
expect(listed).not.toHaveProperty("systemStoreOwnerPackageName")
expect(host.buildMiniappInfo(app(weather))).toMatchObject({system: false})
expect(host.buildMiniappInfo(app(weather))).not.toHaveProperty("storeOwnerPackageName")
})
test("a local or development build never gets system privileges, whatever name it claims", async () => {
const h = harness([app(ai), app(weather, {isMiniappDev: true})])
expect(h.host.isSystemPackage(ai)).toBe(true)
// A dev build of an ordinary package is an ordinary caller.
expect(h.host.isSystemPackage(weather)).toBe(false)
// A local build claiming a system package's name, even over its bundled release and a trusted context.
localBuilds.add(ai)
expect(h.host.isSystemPackage(ai)).toBe(false)
await h.host.handleMiniappsInstall(ai, {url}, "r1")
expect(h.results[0].error?.code).toBe(MiniappErrorCode.NOT_PERMITTED)
expect(installs).toEqual([])
})
test("the retired Store gets no privileges from its live context", () => {
const {host} = harness([])
host.connectedApps.set("com.mentra.store", {hostTrustedSystem: true})
expect(host.isSystemPackage("com.mentra.store")).toBe(false)
})
})
describe("what every miniapp is told", () => {
test("each CONNECT_ACK advertises miniappInstall and telemetry, so a miniapp knows install({url}) and capture are there", () => {
const declared = [...source.matchAll(/^\s*hostFeatures: (\{[^}\n]*\}),$/gm)]
expect(declared).toHaveLength(1)
const features = new Function(`return (${declared[0]![1]})`)() as Record<string, unknown>
expect(features).toEqual({captureAudio: true, initReady: true, transcriptionListeners: true, miniappInstall: true, telemetry: true})
})
})
|