Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

5.2 KB
Raw Blame
import { spawnSync } from 'node:child_process';
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import {
  lockSlice,
  manifestWithoutMembers,
  profileEnvPrefix,
  wasmCacheKey,
} from './wasm-cache-key';

const LOCK = `# This file is automatically @generated by Cargo.
version = 4

[[package]]
name = "engine"
version = "0.1.0"
dependencies = [
 "png",
 "serde 1.0.200",
]

[[package]]
name = "png"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aaaa"

[[package]]
name = "serde"
version = "1.0.200"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbbb"

[[package]]
name = "serde"
version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccc"

[[package]]
name = "server"
version = "0.1.0"
dependencies = ["serde 1.0.100", "tokio"]

[[package]]
name = "tokio"
version = "1.40.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dddd"
`;

const MANIFEST = `[workspace]
resolver = "2"
members = [
  "crates/engine",
  "crates/server",
]

[workspace.dependencies]
png = "0.17"

[profile.engine-wasm]
inherits = "release"
lto = true
`;

const temporaryDirectories: string[] = [];
afterEach(() => {
  for (const directory of temporaryDirectories.splice(0)) {
    rmSync(directory, { recursive: true, force: true });
  }
});

function workspace(files: Record<string, string>): string {
  const root = mkdtempSync(join(tmpdir(), 'wasm-cache-key-'));
  temporaryDirectories.push(root);
  for (const [path, contents] of Object.entries(files)) {
    mkdirSync(dirname(join(root, path)), { recursive: true });
    writeFileSync(join(root, path), contents);
  }
  symlinkSync('../../AGENTS.md', join(root, 'crates/engine/CLAUDE.md'));
  spawnSync('git', ['init', '-q'], { cwd: root });
  spawnSync('git', ['add', '-A'], { cwd: root });
  return root;
}

const BASE_FILES = {
  'Cargo.toml': MANIFEST,
  'Cargo.lock': LOCK,
  'rust-toolchain.toml': '[toolchain]\nchannel = "1.94.0"\n',
  'nix/tools.nix': '{ wasm-pack = "0.15.0"; }\n',
  '.github/workspace-dep-closures.json': JSON.stringify({
    closures: { engine: ['crates/engine'], server: ['crates/server'] },
  }),
  'crates/engine/Cargo.toml': '[package]\nname = "engine"\nversion = "0.1.0"\n',
  'crates/engine/src/lib.rs': 'pub fn draw() {}\n',
  'crates/server/Cargo.toml': '[package]\nname = "server"\nversion = "0.1.0"\n',
  'crates/server/src/main.rs': 'fn main() {}\n',
};

const BUILD = ['wasm-pack', 'build', 'crates/engine', '--profile', 'engine-wasm'];

function key(files: Record<string, string>, env: Record<string, string> = {}) {
  return wasmCacheKey(workspace({ ...BASE_FILES, ...files }), 'engine', BUILD, env);
}

describe('lockSlice', () => {
  it('follows locked dependencies, choosing the named version', () => {
    const slice = lockSlice(LOCK, ['engine']).join('\n');
    expect(slice).toContain('name = "png"');
    expect(slice).toContain('checksum = "bbbb"');
    expect(slice).not.toContain('checksum = "cccc"');
    expect(slice).not.toContain('name = "tokio"');
  });
});

describe('manifestWithoutMembers', () => {
  it('drops only the member list', () => {
    const manifest = manifestWithoutMembers(MANIFEST);
    expect(manifest).not.toContain('crates/server');
    expect(manifest).toContain('resolver = "2"');
    expect(manifest).toContain('png = "0.17"');
    expect(manifest).toContain('[profile.engine-wasm]');
  });
});

describe('profileEnvPrefix', () => {
  it('reads --profile, and release otherwise', () => {
    expect(profileEnvPrefix(BUILD)).toBe('CARGO_PROFILE_ENGINE_WASM_');
    expect(profileEnvPrefix(['wasm-pack', 'build', '--release'])).toBe(
      'CARGO_PROFILE_RELEASE_'
    );
  });
});

describe('wasmCacheKey', () => {
  it('keeps the key when only packages outside the build change', () => {
    const base = key({});
    expect(key({ 'crates/server/src/main.rs': 'fn main() { run() }\n' })).toBe(base);
    expect(
      key({ 'Cargo.lock': LOCK.replace('version = "1.40.0"', 'version = "1.41.0"') })
    ).toBe(base);
    expect(
      key({ 'Cargo.toml': MANIFEST.replace('"crates/server",', '"crates/server",\n  "crates/new",') })
    ).toBe(base);
    expect(key({}, { CARGO_PROFILE_OTHER_WASM_LTO: 'off' })).toBe(base);
  });

  it('changes the key when anything the build reads changes', () => {
    const base = key({});
    const changed = [
      key({ 'crates/engine/src/lib.rs': 'pub fn draw() { paint() }\n' }),
      key({ 'Cargo.lock': LOCK.replace('checksum = "aaaa"', 'checksum = "eeee"') }),
      key({ 'Cargo.lock': LOCK.replace('checksum = "bbbb"', 'checksum = "ffff"') }),
      key({ 'Cargo.toml': MANIFEST.replace('png = "0.17"', 'png = "0.18"') }),
      key({ 'Cargo.toml': MANIFEST.replace('lto = true', 'lto = false') }),
      key({ 'rust-toolchain.toml': '[toolchain]\nchannel = "1.95.0"\n' }),
      key({ 'nix/tools.nix': '{ wasm-pack = "0.15.1"; }\n' }),
      key({}, { CARGO_PROFILE_ENGINE_WASM_LTO: 'off' }),
      wasmCacheKey(workspace(BASE_FILES), 'engine', [...BUILD, '--target', 'web'], {}),
    ];
    for (const other of changed) expect(other).not.toBe(base);
  });
});