Download mobile/modules/engine/src/services/__tests__/AppRegistryPublisherIdentity.test.ts from Anirudh1950/MentraOS: direct link, hf CLI and curl.
- Browser
- Download file 5.66 kB
-
https://huggingface.co/Anirudh1950/MentraOS/resolve/main/mobile/modules/engine/src/services/__tests__/AppRegistryPublisherIdentity.test.ts
- Command line
-
hf download hf://Anirudh1950/MentraOS/mobile/modules/engine/src/services/__tests__/AppRegistryPublisherIdentity.test.ts
-
curl -L -o AppRegistryPublisherIdentity.test.ts https://huggingface.co/Anirudh1950/MentraOS/resolve/main/mobile/modules/engine/src/services/__tests__/AppRegistryPublisherIdentity.test.ts
5.66 kB
| import {beforeEach, describe, expect, mock, test} from "bun:test" | |
| import {result as Res} from "typesafe-ts" | |
| import type {MiniappReleaseIdentity} from "../AppRegistry" | |
| import {assertPublisherIdentityPolicy} from "../publisherIdentityPolicy" | |
| const values = new Map<string, unknown>() | |
| const installationValues = new Map<string, string>() | |
| let failedWrite: string | undefined | |
| mock.module("../../utils/storage/storage", () => ({ | |
| storage: { | |
| load: (key: string) => (values.has(key) ? Res.ok(values.get(key)) : Res.error(new Error("Missing key"))), | |
| save: (key: string, value: unknown) => { | |
| if (key === failedWrite) return Res.error(new Error("Storage write failed")) | |
| values.set(key, value) | |
| return Res.ok(undefined) | |
| }, | |
| remove: (key: string) => { | |
| values.delete(key) | |
| return Res.ok(undefined) | |
| }, | |
| }, | |
| })) | |
| mock.module("react-native-mmkv", () => ({ | |
| createMMKV: () => ({ | |
| getString: (key: string) => installationValues.get(key), | |
| set: (key: string, value: string) => installationValues.set(key, value), | |
| remove: (key: string) => installationValues.delete(key), | |
| }), | |
| })) | |
| mock.module("../../runtime/bootstrap", () => ({ | |
| assertMiniappDevelopmentAllowed: () => {}, | |
| getConfigValues: () => ({}), | |
| isDevMiniappAllowed: () => true, | |
| isInstalledMiniappAllowed: () => true, | |
| isLocalMiniappPackageAllowed: () => true, | |
| isOfflineSystemMiniappAllowed: () => true, | |
| isMiniappAvailable: () => true, | |
| })) | |
| mock.module("../../stores/settings", () => ({ | |
| SETTINGS: {super_mode: {key: "super_mode"}}, | |
| useSettingsStore: {getState: () => ({getSetting: () => false})}, | |
| })) | |
| mock.module("expo/fetch", () => ({fetch: mock()})) | |
| mock.module("expo-file-system", () => ({ | |
| Directory: class { | |
| exists = false | |
| }, | |
| File: class {}, | |
| Paths: {cache: {list: () => []}, document: "file:///test-documents"}, | |
| })) | |
| // Extraction checks each unpacked file's real path through Crust. | |
| mock.module("@mentra/crust", () => ({default: {canonicalFilePath: (path: string) => path}})) | |
| mock.module("react-native-zip-archive", () => ({unzip: mock()})) | |
| mock.module("../../utils/storage/zip", () => ({printDirectory: mock()})) | |
| // Exercise the real metadata transaction, with only native I/O replaced. The | |
| // fingerprints represent already-verified metadata; no bundle is signed here. | |
| const {default: registry} = await import("../AppRegistry") | |
| const packageName = "com.example.publisher" | |
| const publisherKey = `miniapp_publisher_identity:${packageName}` | |
| const activeKey = `${packageName}_active_version` | |
| function finalize(version: string, identity: MiniappReleaseIdentity) { | |
| return registry["finalizeInstall"](packageName, version, identity, () => {}) | |
| } | |
| beforeEach(() => { | |
| values.clear() | |
| installationValues.clear() | |
| failedWrite = undefined | |
| }) | |
| describe("AppRegistry publisher identity finalization", () => { | |
| test("retains verified publisher and release identity when logout clears session storage", () => { | |
| finalize("1.0.0", {source: "direct_download", publisherKeyFingerprint: "publisher-a"}).apply() | |
| values.clear() | |
| expect(registry.getReleaseIdentity(packageName, "1.0.0")?.source).toBe("direct_download") | |
| expect(registry.getPublisherKeyFingerprint(packageName)).toBe("publisher-a") | |
| expect(() => | |
| assertPublisherIdentityPolicy({ | |
| packageName, | |
| candidateFingerprint: "publisher-b", | |
| installedFingerprint: registry.getPublisherKeyFingerprint(packageName), | |
| }), | |
| ).toThrow("signature mismatch") | |
| }) | |
| test("an unsigned dev snapshot leaves an unsigned package unpinned", () => { | |
| const identity: MiniappReleaseIdentity = {source: "dev_snapshot"} | |
| finalize("dev-123", identity).apply() | |
| expect(registry.getPublisherKeyFingerprint(packageName)).toBeNull() | |
| expect(registry.getReleaseIdentity(packageName, "dev-123")).toEqual(identity) | |
| expect(values.get(activeKey)).toBe("dev-123") | |
| }) | |
| test("a signed dev snapshot pins its publisher like any other signed install", () => { | |
| const identity: MiniappReleaseIdentity = {source: "dev_snapshot", publisherKeyFingerprint: "publisher-b"} | |
| finalize("dev-123", identity).apply() | |
| expect(registry.getPublisherKeyFingerprint(packageName)).toBe("publisher-b") | |
| expect(registry.getReleaseIdentity(packageName, "dev-123")).toEqual(identity) | |
| const validateRelease = (fingerprint?: string) => | |
| assertPublisherIdentityPolicy({ | |
| packageName, | |
| candidateFingerprint: fingerprint, | |
| installedFingerprint: registry.getPublisherKeyFingerprint(packageName), | |
| }) | |
| expect(() => validateRelease("publisher-b")).not.toThrow() | |
| expect(() => validateRelease("publisher-a")).toThrow("signature mismatch") | |
| expect(() => validateRelease()).toThrow("Unsigned bundle cannot replace signed miniapp") | |
| }) | |
| test("restores the production pin and release metadata if snapshot activation fails", () => { | |
| values.set(publisherKey, "publisher-a") | |
| values.set(activeKey, "1.0.0") | |
| registry.getPublisherKeyFingerprint(packageName) | |
| const before = new Map(values) | |
| const installationBefore = new Map(installationValues) | |
| const transaction = finalize("dev-123", {source: "dev_snapshot", publisherKeyFingerprint: "publisher-b"}) | |
| failedWrite = activeKey | |
| expect(() => transaction.apply()).toThrow("Storage write failed") | |
| failedWrite = undefined | |
| transaction.rollback() | |
| expect(values).toEqual(before) | |
| expect(installationValues).toEqual(installationBefore) | |
| expect(registry.getReleaseIdentity(packageName, "dev-123")).toBeNull() | |
| }) | |
| }) | |