149 lines
10 KiB
JavaScript
149 lines
10 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
|
|
import { createHash } from 'node:crypto';
|
|
import { execFileSync } from 'node:child_process';
|
|
import { tmpdir } from 'node:os';
|
|
import { dirname, join, resolve } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const project = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
const put = (root, path, value = '') => {
|
|
mkdirSync(dirname(join(root, path)), { recursive: true });
|
|
writeFileSync(join(root, path), value);
|
|
};
|
|
function fixture(t) {
|
|
const root = mkdtempSync(join(tmpdir(), 'vibemake-build-isolation-'));
|
|
t.after(() => rmSync(root, { recursive: true, force: true }));
|
|
for (const script of ['build.mjs', 'build-cad.mjs', 'cad-engine.mjs', 'lib/version-site-assets.mjs']) {
|
|
const target = join(root, 'scripts', script);
|
|
mkdirSync(dirname(target), { recursive: true });
|
|
cpSync(join(project, 'scripts', script), target);
|
|
}
|
|
symlinkSync(join(project, 'node_modules'), join(root, 'node_modules'), 'dir');
|
|
put(root, 'scripts/meshes.mjs', 'export async function compressRobot() { return {files: 0, before: 0, after: 0}; }');
|
|
for (const dir of ['assets', 'data', 'vendor', 'play']) mkdirSync(join(root, dir));
|
|
for (const name of ['index.html', 'explore.html', 'project.html', 'studio.html', 'workspace.html']) {
|
|
put(root, name, '<script type="module" src="/js/workspace.js"></script>');
|
|
}
|
|
put(root, 'js/api.js', "export const BUILD_ENV = 'auto'; export const auth = {};");
|
|
put(root, 'js/workspace.js', "import { CadApi } from './cad/api.js'; console.log(CadApi);");
|
|
put(root, 'css/style.css', 'body {}');
|
|
for (const name of ['favicon.svg', 'favicon.ico', 'apple-touch-icon.png', 'site.webmanifest', 'icon-16.png', 'icon-32.png', 'icon-48.png', 'icon-64.png', 'icon-192.png', 'icon-512.png']) put(root, name);
|
|
put(root, 'robots/robots.json', '[{"id":"so101","urdf":"robots/so101/so101.urdf"}]');
|
|
put(root, 'robots/so101/so101.urdf', '<robot/>');
|
|
return root;
|
|
}
|
|
|
|
test('production and dev packages build without CAD or PCB sources and preserve deployed modules', t => {
|
|
const root = fixture(t);
|
|
const website = join(root, 'website');
|
|
const protectedFiles = ['cad/index.html', 'cad/engine.json', 'pcb/index.html', 'js/cad/app.js', 'css/cad.css'];
|
|
for (const path of protectedFiles) put(website, path, `published:${path}`);
|
|
for (const environment of ['auto', 'dev']) {
|
|
// The first build has no CAD files. The second has stale/invalid local files.
|
|
if (environment === 'dev') {
|
|
put(root, 'cad/engine.json', 'invalid engine manifest');
|
|
put(root, 'js/cad/app.js', 'stale CAD application');
|
|
put(root, 'css/cad.css', 'stale CAD styles');
|
|
put(root, 'pcb/index.html', 'stale PCB application');
|
|
}
|
|
const output = environment === 'dev' ? 'dist-dev' : 'dist';
|
|
for (const path of protectedFiles) put(root, `${output}/${path}`, 'stale previous package');
|
|
execFileSync(process.execPath, [join(root, 'scripts/build.mjs'), `--env=${environment}`], {
|
|
env: { ...process.env, PCB_DIST: join(root, 'missing-pcb-build') }, stdio: 'pipe',
|
|
});
|
|
const archive = join(root, `${output}.zip`);
|
|
const entries = execFileSync('unzip', ['-Z1', archive], { encoding: 'utf8' }).trim().split('\n');
|
|
assert.ok(entries.includes('workspace/index.html'));
|
|
assert.ok(!entries.some(path => /^(?:cad\/|pcb\/|js\/cad\/|css\/cad\.css$)/.test(path)));
|
|
for (const path of protectedFiles) assert.equal(existsSync(join(root, output, path)), false);
|
|
assert.match(readFileSync(join(root, output, 'js/workspace.js'), 'utf8'), /["']\/cad\/js\/cad\/api\.js["']/);
|
|
execFileSync('unzip', ['-oq', archive, '-d', website]);
|
|
for (const path of protectedFiles) assert.equal(readFileSync(join(website, path), 'utf8'), `published:${path}`);
|
|
assert.equal(JSON.parse(readFileSync(join(root, output, 'build-info.json'))).scope, 'site');
|
|
}
|
|
assert.ok(existsSync(join(root, 'dist.zip')));
|
|
assert.ok(existsSync(join(root, 'dist-dev.zip')));
|
|
});
|
|
|
|
test('the self-contained CAD package extracts directly into cad without replacing the main site or PCB', t => {
|
|
const root = fixture(t);
|
|
const prefix = '/cad/_assets/release/';
|
|
const files = ['engine.js', 'engine.wasm', 'font.otf'].map(name => {
|
|
const bytes = `CAD ${name}`;
|
|
put(root, `cad/_assets/release/${name}`, bytes);
|
|
return { path: prefix + name, size: Buffer.byteLength(bytes), sha256: createHash('sha256').update(bytes).digest('hex') };
|
|
});
|
|
put(root, 'cad/engine.json', JSON.stringify({ bridgeVersion: 1, engineKind: 'solidcraft', kernel: 'smlib', buildId: 'release', files, js: files[0].path, wasm: files[1].path, chineseFont: files[2].path }));
|
|
put(root, 'index.html', '<script type="importmap">{"imports":{"three":"/vendor/three.js"}}</script>');
|
|
const page = '<html><head><link href="/css/fonts.css"><link href="/css/style.css"><link href="/css/cad.css"><link rel="icon" href="/favicon.ico"></head><body><a href="/workspace/">Workspace</a><script src="/js/shell.js"></script><script type="module" src="/js/cad/app.js"></script></body></html>';
|
|
put(root, 'cad.html', page);
|
|
put(root, 'cad/editor/index.html', page);
|
|
put(root, 'cad/manifest.webmanifest', '{"icons":[{"src":"/icon-192.png"}]}');
|
|
put(root, 'cad/worker.js', "importScripts('/cad/sw.js');");
|
|
put(root, 'js/cad/app.js', "import { auth } from '../api.js'; import('three'); import('../late.js'); console.log(auth);");
|
|
put(root, 'js/late.js', "export const endpoint = '/api/cad/workspace/projects'; export const environment = new URL('../assets/environments/studio.hdr', import.meta.url);");
|
|
put(root, 'js/shell.js', "document.body.dataset.logo = '/assets/brand/make-cad.svg';");
|
|
put(root, 'css/fonts.css', '@font-face {src: url(../vendor/font.woff2)}');
|
|
put(root, 'css/cad.css', '.cad-page {}');
|
|
put(root, 'vendor/three.js', 'export const REVISION = 160;');
|
|
put(root, 'vendor/font.woff2', 'font');
|
|
put(root, 'assets/brand/make-cad.svg', '<svg/>');
|
|
put(root, 'assets/environments/studio.hdr', 'hdr');
|
|
put(root, 'data/community.json', '{}');
|
|
execFileSync(process.execPath, [join(root, 'scripts/build-cad.mjs')], { stdio: 'pipe' });
|
|
const archive = join(root, 'dist-cad.zip');
|
|
const entries = execFileSync('unzip', ['-Z1', archive], { encoding: 'utf8' }).trim().split('\n');
|
|
for (const path of ['index.html', 'editor/index.html', 'engine.json', 'sw.js', '_assets/release/engine.wasm', 'js/cad/app.js', 'js/api.js', 'js/late.js', 'css/cad.css', 'css/style.css', 'css/fonts.css', 'vendor/font.woff2', 'assets/brand/make-cad.svg', 'favicon.ico', 'robots/robots.json', 'data/community.json']) assert.ok(entries.includes(path), path);
|
|
assert.ok(!entries.some(path => /^(?:cad\/|pcb\/|js\/workspace\.js$)/.test(path)));
|
|
const info = JSON.parse(readFileSync(join(root, 'dist-cad/build-info.json'), 'utf8'));
|
|
for (const path of ['index.html', 'editor/index.html']) {
|
|
const html = readFileSync(join(root, 'dist-cad', path), 'utf8');
|
|
assert.match(html, /href="\/cad\/css\/style\.css\?v=/);
|
|
assert.match(html, /src="\/cad\/js\/cad\/app\.js\?v=/);
|
|
assert.match(html, /href="\/cad\/favicon.ico"/);
|
|
assert.match(html, /href="\/workspace\/"/);
|
|
assert.match(html, /"three":\s*"\/cad\/vendor\/three\.js"/);
|
|
assert.ok(html.includes(info.buildId));
|
|
}
|
|
assert.match(readFileSync(join(root, 'dist-cad/js/cad/app.js'), 'utf8'), /api\.js\?v=/);
|
|
assert.match(readFileSync(join(root, 'dist-cad/js/shell.js'), 'utf8'), /\/cad\/assets\/brand\/make-cad\.svg/);
|
|
assert.match(readFileSync(join(root, 'dist-cad/js/late.js'), 'utf8'), /\/api\/cad\/workspace\/projects/);
|
|
assert.match(readFileSync(join(root, 'dist-cad/js/late.js'), 'utf8'), /\.\.\/assets\/environments\/studio\.hdr/);
|
|
assert.match(readFileSync(join(root, 'dist-cad/css/fonts.css'), 'utf8'), /\.\.\/vendor\/font\.woff2/);
|
|
assert.match(readFileSync(join(root, 'dist-cad/manifest.webmanifest'), 'utf8'), /\/cad\/icon-192\.png/);
|
|
const website = join(root, 'website');
|
|
const protectedFiles = ['index.html', 'pcb/index.html', 'js/api.js', 'css/style.css', 'cad/_assets/previous/engine.wasm'];
|
|
for (const path of protectedFiles) put(website, path, `published:${path}`);
|
|
execFileSync('unzip', ['-oq', archive, '-d', join(website, 'cad')]);
|
|
for (const path of protectedFiles) assert.equal(readFileSync(join(website, path), 'utf8'), `published:${path}`);
|
|
assert.ok(existsSync(join(website, 'cad/index.html')));
|
|
assert.ok(existsSync(join(website, 'cad/editor/index.html')));
|
|
assert.equal(existsSync(join(website, 'cad/cad')), false);
|
|
|
|
// SolidCraft supplies its current build directly; an old local install must not be used.
|
|
const freshEngine = join(root, 'fresh-engine');
|
|
cpSync(join(root, 'cad/_assets'), join(freshEngine, '_assets'), { recursive: true });
|
|
cpSync(join(root, 'cad/engine.json'), join(freshEngine, 'engine.json'));
|
|
put(root, 'cad/engine.json', 'invalid old local engine');
|
|
const fullDirectory = join(root, 'solidcraft-output/vibemake-cad');
|
|
const fullArchive = join(root, 'solidcraft-output/vibemake-cad.zip');
|
|
const env = {
|
|
...process.env,
|
|
VIBEMAKE_CAD_ENGINE_DIST: freshEngine,
|
|
VIBEMAKE_CAD_PACKAGE_DIR: fullDirectory,
|
|
VIBEMAKE_CAD_PACKAGE_ZIP: fullArchive,
|
|
};
|
|
execFileSync(process.execPath, [join(root, 'scripts/build-cad.mjs')], { env, stdio: 'pipe' });
|
|
const fullEntries = execFileSync('unzip', ['-Z1', fullArchive], { encoding: 'utf8' }).trim().split('\n');
|
|
for (const path of ['index.html', 'editor/index.html', 'engine.json', 'js/cad/app.js', 'css/cad.css']) assert.ok(fullEntries.includes(path), path);
|
|
const fullInfo = JSON.parse(readFileSync(join(fullDirectory, 'build-info.json'), 'utf8'));
|
|
assert.equal(fullInfo.archive, 'vibemake-cad.zip');
|
|
assert.equal(fullInfo.engineBuildId, 'release');
|
|
const previousArchive = readFileSync(fullArchive);
|
|
rmSync(join(root, 'cad.html'));
|
|
assert.throws(() => execFileSync(process.execPath, [join(root, 'scripts/build-cad.mjs')], { env, stdio: 'pipe' }));
|
|
assert.deepEqual(readFileSync(fullArchive), previousArchive, 'missing HTML must not replace a successful release ZIP');
|
|
});
|