|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - Executes the actual native extension bundle in an isolated VM. |
| 2 | +import * as NodeChildProcess from "node:child_process"; |
| 3 | +import * as NodeFS from "node:fs"; |
| 4 | +import * as NodeOS from "node:os"; |
| 5 | +import * as NodePath from "node:path"; |
| 6 | +import * as NodeVM from "node:vm"; |
| 7 | +import { afterAll, beforeAll, expect, it } from "vite-plus/test"; |
| 8 | + |
| 9 | +const mobileRoot = new URL("../apps/mobile/", import.meta.url).pathname; |
| 10 | +const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-widget-runtime-")); |
| 11 | +const context = NodeVM.createContext({ console }); |
| 12 | +const now = Date.parse("2026-10-01T12:00:00Z"); |
| 13 | +NodeVM.runInContext(`Date.now = () => ${now}`, context); |
| 14 | + |
| 15 | +beforeAll(() => { |
| 16 | + const bundle = NodePath.join(directory, "ExpoWidgets.bundle"); |
| 17 | + NodeChildProcess.execFileSync( |
| 18 | + process.execPath, |
| 19 | + ["node_modules/expo-widgets/scripts/build-bundle.mjs", ".", "ios", bundle], |
| 20 | + { |
| 21 | + cwd: mobileRoot, |
| 22 | + stdio: "pipe", |
| 23 | + }, |
| 24 | + ); |
| 25 | + // Execute the real extension runtime, including Expo UI and Expo's React stub. |
| 26 | + // Mocked layout tests cannot catch failures while this bundle initializes. |
| 27 | + NodeVM.runInContext(NodeFS.readFileSync(bundle, "utf8"), context); |
| 28 | +}, 120_000); |
| 29 | + |
| 30 | +afterAll(() => NodeFS.rmSync(directory, { recursive: true, force: true })); |
| 31 | + |
| 32 | +function render(name: string, props: unknown, environment: object) { |
| 33 | + const layout = NodeChildProcess.execFileSync( |
| 34 | + process.execPath, |
| 35 | + [ |
| 36 | + "--input-type=commonjs", |
| 37 | + "-e", |
| 38 | + ` |
| 39 | + const { createRequire } = require('node:module'); |
| 40 | + const vm = require('node:vm'); |
| 41 | + const requireMobile = createRequire(process.cwd() + '/package.json'); |
| 42 | + const babel = requireMobile(requireMobile.resolve('@babel/core', { |
| 43 | + paths: [requireMobile.resolve('babel-preset-expo')] |
| 44 | + })); |
| 45 | + const result = babel.transformFileSync('src/widgets/${name}.tsx', { |
| 46 | + caller: { name: 'metro', platform: 'ios', supportsStaticESM: false, isDev: false } |
| 47 | + }); |
| 48 | + const capture = (_, layout) => process.stdout.write(layout); |
| 49 | + vm.runInNewContext(result.code, { |
| 50 | + exports: {}, |
| 51 | + require: name => name === 'expo-widgets' |
| 52 | + ? { createWidget: capture, createLiveActivity: capture } : {} |
| 53 | + }); |
| 54 | + `, |
| 55 | + ], |
| 56 | + { cwd: mobileRoot, encoding: "utf8" }, |
| 57 | + ); |
| 58 | + NodeVM.runInContext(`globalThis.__expoWidgetLayout = (${layout})`, context); |
| 59 | + return NodeVM.runInContext( |
| 60 | + `JSON.stringify(__expoWidgetRender(${JSON.stringify(props)}, ${JSON.stringify(environment)}))`, |
| 61 | + context, |
| 62 | + ) as string; |
| 63 | +} |
| 64 | + |
| 65 | +it("renders the Live Activity banner and Dynamic Island with the bundled runtime", () => { |
| 66 | + const result = JSON.parse( |
| 67 | + render( |
| 68 | + "AgentActivity", |
| 69 | + { |
| 70 | + title: "T3 Code", |
| 71 | + subtitle: "Agent work in progress", |
| 72 | + activeCount: 1, |
| 73 | + activities: [ |
| 74 | + { |
| 75 | + phase: "running", |
| 76 | + threadTitle: "Audit widgets", |
| 77 | + projectTitle: "T3", |
| 78 | + status: "Working", |
| 79 | + deepLink: "/threads/test", |
| 80 | + }, |
| 81 | + ], |
| 82 | + }, |
| 83 | + { colorScheme: "dark", isStale: false, timestamp: now }, |
| 84 | + ), |
| 85 | + ); |
| 86 | + expect(JSON.stringify(result.banner)).toContain("Audit widgets"); |
| 87 | + expect(JSON.stringify(result.compactLeading)).toContain("ImageView"); |
| 88 | + expect(JSON.stringify(result.compactTrailing)).toContain("1"); |
| 89 | +}); |
| 90 | + |
| 91 | +it.each(["systemSmall", "systemMedium", "systemLarge", "accessoryRectangular"])( |
| 92 | + "renders fresh usage and its expired state in %s", |
| 93 | + (widgetFamily) => { |
| 94 | + const props = { |
| 95 | + checkedAt: now, |
| 96 | + providers: [ |
| 97 | + { |
| 98 | + name: "Codex", |
| 99 | + detail: "Subscription remaining", |
| 100 | + windows: [{ remaining: 70, label: "Session", kind: "session", reset: "Resets soon" }], |
| 101 | + expiresAt: now + 60_000, |
| 102 | + totalWindows: 1, |
| 103 | + }, |
| 104 | + ], |
| 105 | + }; |
| 106 | + const environment = { widgetFamily, colorScheme: "dark", widgetRenderingMode: "fullColor" }; |
| 107 | + expect(render("SubscriptionUsage", props, { ...environment, timestamp: now })).toContain("70"); |
| 108 | + expect( |
| 109 | + render("SubscriptionUsage", props, { ...environment, timestamp: now + 60_001 }), |
| 110 | + ).toContain("Open T3 to refresh"); |
| 111 | + }, |
| 112 | +); |
0 commit comments