public-artifact.test.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { readFileSync } from 'node:fs';
  4. import { createHash } from 'node:crypto';
  5. import { spawnSync } from 'node:child_process';
  6. import { fileURLToPath } from 'node:url';
  7. import { extractMcodeToolsArtifact, MCODE_TOOLS_ARTIFACT } from '../scripts/lib/mcode-tools-artifact.mjs';
  8. test('public archive yields the exact embedded production tool artifact', async () => {
  9. const archive = readFileSync(new URL('../.cache/artifacts/code-0.3.11.tgz', import.meta.url));
  10. const extracted = await extractMcodeToolsArtifact(archive);
  11. const built = readFileSync(new URL('../dist/embedded/mcode-tools/cli.mjs', import.meta.url));
  12. assert.deepEqual(extracted.cli, built);
  13. assert.equal(createHash('sha256').update(built).digest('hex'), MCODE_TOOLS_ARTIFACT.sha256);
  14. assert.equal(JSON.parse(extracted.manifest).auth.mode, 'shared-broker');
  15. });
  16. test('modified public archives fail before any artifact is accepted', async () => {
  17. const archive = readFileSync(new URL('../.cache/artifacts/code-0.3.11.tgz', import.meta.url));
  18. archive[archive.length - 1] ^= 1;
  19. await assert.rejects(extractMcodeToolsArtifact(archive), /integrity mismatch/);
  20. });
  21. test('the built mcode-tools CLI starts independently and exposes its commands', () => {
  22. const cli = fileURLToPath(new URL('../dist/mcode-tools.js', import.meta.url));
  23. const result = spawnSync(process.execPath, [cli, '--help'], { encoding: 'utf8', timeout: 15000 });
  24. assert.equal(result.status, 0, result.stderr);
  25. assert.match(result.stdout, /mcode-tools/);
  26. });
  27. test('the built image preview worker processes a synthetic PNG and exits cleanly', { timeout: 15000 }, async (t) => {
  28. const { Worker } = await import('node:worker_threads');
  29. const worker = new Worker(new URL('../dist/image-preview-worker.js', import.meta.url), {
  30. workerData: {
  31. bytes: Buffer.from('iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGUlEQVR4nGP4n8LwnxLMMGrAqAGjBgwXAwCOqWIfmQV0zAAAAABJRU5ErkJggg==', 'base64'),
  32. mimeType: 'image/png',
  33. maxWidth: 960,
  34. },
  35. });
  36. // Allow normal Worker/WASM teardown after the reply. A message alone does
  37. // not prove a clean exit; force termination is only timeout/failure cleanup.
  38. t.after(() => worker.terminate());
  39. const preview = await new Promise((resolve, reject) => {
  40. let response;
  41. worker.once('message', (value) => { response = value; });
  42. worker.once('error', reject);
  43. worker.once('exit', (code) => {
  44. if (code !== 0) reject(new Error(`Preview worker exited with code ${code}`));
  45. else resolve(response);
  46. });
  47. });
  48. assert.equal(typeof preview, 'string');
  49. assert.deepEqual([...Buffer.from(preview, 'base64').subarray(0, 8)], [137, 80, 78, 71, 13, 10, 26, 10]);
  50. });