Files
vibemake/scripts/cad-engine.test.mjs
T
2026-11-24 20:09:21 +08:00

42 lines
3.3 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { createHash } from 'node:crypto';
import { readCadEngine } from './cad-engine.mjs';
test('engine composition fails closed on missing or incompatible distribution',()=>{assert.throws(()=>readCadEngine());});
test('every required asset must exist with its exact digest',()=>{
const root=mkdtempSync(join(tmpdir(),'vm-cad-manifest-'));
try {
const prefix='/cad/_assets/test-build/';mkdirSync(join(root,'_assets/test-build'),{recursive:true});
const files=['engine.js','engine.wasm','font.otf'].map((name)=>{writeFileSync(join(root,'_assets/test-build',name),'asset');return {path:prefix+name,size:5,sha256:createHash('sha256').update('asset').digest('hex')};});
const manifest={bridgeVersion:1,engineKind:'solidcraft',kernel:'smlib',buildId:'test-build',files,js:files[0].path,wasm:files[1].path,chineseFont:files[2].path};
writeFileSync(join(root,'engine.json'),JSON.stringify(manifest));assert.equal(readCadEngine(root).manifest.buildId,'test-build');
writeFileSync(join(root,'_assets/test-build/engine.wasm'),'bad');assert.throws(()=>readCadEngine(root),/checksum/);
} finally {rmSync(root,{recursive:true,force:true});}
});
const { cadAgentTarget, isCadAgentPath, cadAgentBuildConfiguration } = await import('./cad-agent-proxy.mjs');
test('CAD Agent proxy recognizes only its versioned namespace and does not default to Java', () => {
assert.equal(cadAgentTarget(undefined), null);
for (const path of ['/api/cad-agent/v2', '/api/cad-agent/v2/capabilities', '/api/cad-agent/v2/runs/run-a/events?afterSequence=4']) assert.equal(isCadAgentPath(path), true);
for (const path of ['/api/cad/ai/chat', '/api/community/ai/chat', '/api/cad/workspace/projects', '/api/cad-agent/v20', '/api/cad-agent/v2-other']) assert.equal(isCadAgentPath(path), false);
assert.equal(cadAgentTarget('http://127.0.0.1:8072').origin, 'http://127.0.0.1:8072');
assert.equal(cadAgentTarget('https://cad-agent.example.com').origin, 'https://cad-agent.example.com');
for (const value of ['file:///tmp/test', 'https://secret:token@example.com', 'https://example.com/path', 'https://example.com?token=secret']) assert.throws(() => cadAgentTarget(value));
});
test('CAD Agent packaging requires an explicit runtime and safely injects a separately configured API base', () => {
assert.deepEqual(cadAgentBuildConfiguration({}), { runtime: 'java-v1', apiBase: '/api/cad-agent/v2', html: '' });
const config = cadAgentBuildConfiguration({ VIBEMAKE_CAD_AGENT_RUNTIME: 'deepagents', VIBEMAKE_CAD_AGENT_API_BASE: 'https://agent.example/api/cad-agent/v2' });
assert.equal(config.runtime, 'deepagents');
assert.ok(config.html.includes('name="cad-agent-runtime" content="deepagents"'));
assert.ok(config.html.includes('content="https://agent.example/api/cad-agent/v2"'));
for (const environment of [{ VIBEMAKE_CAD_AGENT_RUNTIME: 'typo' },
{ VIBEMAKE_CAD_AGENT_API_BASE: 'https://example/api/cad-agent/v2?token=secret' },
{ VIBEMAKE_CAD_AGENT_API_BASE: '//example/api/cad-agent/v2' },
{ VIBEMAKE_CAD_AGENT_API_BASE: 'http://remote/api/cad-agent/v2' }]) assert.throws(() => cadAgentBuildConfiguration(environment));
});