MC/website/tests/cp-v2-preview-publication.test.mjs
2026-11-27 23:43:05 +00:00

27 lines
5.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {mkdtemp,mkdir,writeFile,readFile,readlink,rm,symlink} from 'node:fs/promises';
import {tmpdir} from 'node:os';
import path from 'node:path';
import {publishPreview} from '../ops/publish-cp-v2-preview.mjs';
const first='a'.repeat(40),second='b'.repeat(40);
async function fixture(fn){const root=await mkdtemp(path.join(tmpdir(),'cpv2-publish-'));const ciRoot=path.join(root,'ci'),previewRoot=path.join(root,'preview');await mkdir(ciRoot);try{await fn({ciRoot,previewRoot,requireRoot:false});}finally{await rm(root,{recursive:true,force:true});}}
async function artifact(options,revision=first){const source=path.join(options.ciRoot,'artifacts',revision,'website/control-panel/public/cp-v2');await mkdir(source,{recursive:true});for(const name of ['index.html','v2.js','v2.css','cp-v2-page-template.mjs','cp-v2-bureau-pages.mjs'])await writeFile(path.join(source,name),name+' '+revision);await writeFile(path.join(options.ciRoot,'built-commit'),revision+'\n');await writeFile(path.join(options.ciRoot,'pending-commit'),revision+'\n');return source;}
// Link publication is tested on Linux, matching the production filesystem. The
// trust and bound checks below also run on Windows without requiring symlinks.
test('publishes only an approved immutable release, preserves fallback and is idempotent',{skip:process.platform==='win32'},()=>fixture(async options=>{
await artifact(options);const result=await publishPreview(options);assert.equal(result.changed,true);assert.equal(result.revision,first);assert.equal(result.files,5);assert.equal(await readlink(path.join(options.previewRoot,'current')),path.join(options.previewRoot,'releases',first));const manifest=JSON.parse(await readFile(path.join(options.previewRoot,'current/revision.json'),'utf8'));assert.equal(manifest.revision,first);assert.equal(Object.keys(manifest.files).length,5);
assert.equal((await publishPreview(options)).changed,false);await artifact(options,second);await publishPreview(options);assert.equal(await readlink(path.join(options.previewRoot,'previous')),path.join(options.previewRoot,'releases',first));assert.equal(await readlink(path.join(options.previewRoot,'current')),path.join(options.previewRoot,'releases',second));
}));
test('mismatched approvals and invalid revisions are refused',()=>fixture(async options=>{await artifact(options);await writeFile(path.join(options.ciRoot,'pending-commit'),second);await assert.rejects(publishPreview(options),/matching CI-approved/);await writeFile(path.join(options.ciRoot,'built-commit'),'../../escape');await assert.rejects(publishPreview(options),/matching CI-approved/);}));
test('DuneGit, unsupported assets, directories and oversized files cannot be published',async()=>{
for(const entry of ['cp-v2-git-admin.mjs','.env','nested','oversized.mjs'])await fixture(async options=>{const source=await artifact(options);if(entry==='nested')await mkdir(path.join(source,entry));else await writeFile(path.join(source,entry),entry==='oversized.mjs'?'x'.repeat(1048577):'not publishable');await assert.rejects(publishPreview(options),/Unsupported preview asset|bound exceeded/);});
});
test('missing required assets fail closed',()=>fixture(async options=>{const source=await artifact(options);await rm(path.join(source,'v2.js'));await assert.rejects(publishPreview(options),/Missing preview asset/);}));
test('symlinked assets are refused',{skip:process.platform==='win32'},()=>fixture(async options=>{const source=await artifact(options);await writeFile(path.join(options.ciRoot,'private'),'private');await symlink(path.join(options.ciRoot,'private'),path.join(source,'extra.mjs'));await assert.rejects(publishPreview(options),/Unsupported preview asset/);}));
test('changed files cannot overwrite an immutable published release',{skip:process.platform==='win32'},()=>fixture(async options=>{const source=await artifact(options);await publishPreview(options);await writeFile(path.join(source,'v2.js'),'changed');await assert.rejects(publishPreview(options),/Immutable preview release differs/);}));
test('preview publication contains no service restart or database operation',async()=>{const source=await readFile(new URL('../ops/publish-cp-v2-preview.mjs',import.meta.url),'utf8');assert.doesNotMatch(source,/child_process|execFile|spawn\(|systemctl|pm2 |mysql|openDatabase/);const config=await readFile(new URL('../ops/cp-v2-preview.nginx.conf',import.meta.url),'utf8');assert.match(config,/auth_request \/_cp_v2_owner_check/);assert.match(config,/location = \/_cp_v2_owner_check \{\s+internal;/);assert.match(config,/proxy_pass http:\/\/127\.0\.0\.1:3003\/v2\/v2.css/);assert.match(config,/text\/javascript js mjs/);});
test('larger CP releases retain a strict eighty-file bound',()=>fixture(async options=>{const source=await artifact(options);for(let i=0;i<76;i++)await writeFile(path.join(source,'page-'+i+'.mjs'),'export {};');await assert.rejects(publishPreview(options),/Too many preview assets/);}));
test('bank expansion above the previous fifty-file bound publishes intact',{skip:process.platform==='win32'},()=>fixture(async options=>{const source=await artifact(options);for(let i=0;i<47;i++)await writeFile(path.join(source,'page-'+i+'.mjs'),'export {};');assert.equal((await publishPreview(options)).files,52);}));