Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@tannerlinsley/imessage

Read, search, draft, and explicitly send iMessages on a Mac, using only built-in macOS tools.

FollowFork

Files

compile.test.ts

47 lines · 1.6 KB · TypeScript
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 });
  }
});