|
| 1 | +import * as NodeModule from "node:module"; |
| 2 | +import * as NodeURL from "node:url"; |
| 3 | +import * as NodeVM from "node:vm"; |
| 4 | +import * as Contracts from "@t3tools/contracts"; |
| 5 | +import * as Option from "effect/Option"; |
| 6 | +import * as Schema from "effect/Schema"; |
| 7 | +import { expect, it } from "vite-plus/test"; |
| 8 | + |
| 9 | +const require = NodeModule.createRequire(import.meta.url); |
| 10 | +const babel: { |
| 11 | + transformFileSync: ( |
| 12 | + filename: string, |
| 13 | + options: Record<string, unknown>, |
| 14 | + ) => { code?: string | null } | null; |
| 15 | +} = require("@babel/core"); |
| 16 | + |
| 17 | +it("extracts thread links when Hermes clones Babel's named-group regex for matchAll", () => { |
| 18 | + const compiled = babel.transformFileSync( |
| 19 | + NodeURL.fileURLToPath( |
| 20 | + new URL("../../../../packages/shared/src/threadLinks.ts", import.meta.url), |
| 21 | + ), |
| 22 | + { |
| 23 | + babelrc: false, |
| 24 | + configFile: false, |
| 25 | + presets: [[require.resolve("babel-preset-expo"), { enableBabelRuntime: false }]], |
| 26 | + caller: { |
| 27 | + name: "metro", |
| 28 | + bundler: "metro", |
| 29 | + platform: "android", |
| 30 | + isDev: false, |
| 31 | + isServer: false, |
| 32 | + isNodeModule: false, |
| 33 | + supportsStaticESM: false, |
| 34 | + supportsReactCompiler: true, |
| 35 | + }, |
| 36 | + }, |
| 37 | + ); |
| 38 | + if (!compiled?.code) throw new Error("Mobile thread links did not compile"); |
| 39 | + class CloningRegExp extends RegExp { |
| 40 | + // Hermes's matchAll clone loses Babel's wrapper and its named-group mapping. |
| 41 | + override [Symbol.matchAll](value: string) { |
| 42 | + return new RegExp(this.source, this.flags)[Symbol.matchAll](value); |
| 43 | + } |
| 44 | + } |
| 45 | + const exports = {} as typeof import("@t3tools/shared/threadLinks"); |
| 46 | + const dependencies: Readonly<Record<string, unknown>> = { |
| 47 | + "@t3tools/contracts": Contracts, |
| 48 | + "effect/Option": Option, |
| 49 | + "effect/Schema": Schema, |
| 50 | + }; |
| 51 | + NodeVM.runInNewContext(compiled.code, { |
| 52 | + exports, |
| 53 | + RegExp: new Proxy(CloningRegExp, { |
| 54 | + apply: (target, _thisArg, args) => Reflect.construct(target, args), |
| 55 | + }), |
| 56 | + require: (name: string) => dependencies[name], |
| 57 | + }); |
| 58 | + const environmentId = Contracts.EnvironmentId.make("here"); |
| 59 | + expect( |
| 60 | + exports.threadLinkRefs( |
| 61 | + "[Dictation](t3-thread://v2/here/thread-1) <t3-thread://v2/there/thread-2> `<t3-thread://v2/code/literal>`", |
| 62 | + environmentId, |
| 63 | + ), |
| 64 | + ).toEqual([ |
| 65 | + { environmentId, threadId: "thread-1" }, |
| 66 | + { environmentId: "there", threadId: "thread-2" }, |
| 67 | + ]); |
| 68 | +}); |
0 commit comments