import { test } from "node:test"; import assert from "node:assert/strict"; import { runInNewContext } from "node:vm"; import { spawnSync } from "node:child_process"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { nativeSource } from "../src/native.ts"; test("both Messages scripts compile without running or sending", () => { const root = mkdtempSync(join(tmpdir(), "imessage-compile-")); try { for (const target of [{ recipient: "test@example.invalid" }, { chatId: 1 }]) { let captured = ""; const sandbox: any = { ObjC: { import() {} }, $: {} }; runInNewContext(nativeSource, sandbox); sandbox.query = (sql: string) => sql.includes("MAX(ROWID)") ? [{ id: 1 }] : [ { guid: "iMessage;-;test@example.invalid", service_name: "iMessage", }, ]; sandbox.task = (file: string, args: string[]) => { assert.equal(file, "/usr/bin/osascript"); captured = args[1]; return { code: 1, stdout: "", stderr: "Test intercept, never execute" }; }; sandbox.nativeMain({ action: "send", ...target, text: "Synthetic preview, do not send.", }); assert.ok(captured); const compiled = spawnSync( "/usr/bin/osacompile", ["-o", join(root, "test.scpt"), "-e", captured], { encoding: "utf8", timeout: 15000 }, ); assert.equal(compiled.status, 0, compiled.stderr); } } finally { rmSync(root, { recursive: true, force: true }); } });