MC/website/tests/public-exchange-publication.test.mjs
2026-11-27 23:43:05 +00:00

10 lines
2.1 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';
import {mkdtempSync,readFileSync,existsSync,rmSync,writeFileSync,mkdirSync}from'node:fs';import {tmpdir}from'node:os';import path from'node:path';import {pathToFileURL,fileURLToPath}from'node:url';
import {publishExchangeAssets}from'../scripts/public-exchange-assets.mjs';
test('the public Exchange publishes and resolves the same item-template import graph as the CP',async()=>{
const folder=mkdtempSync(path.join(tmpdir(),'dune-public-exchange-'));
try{const files=publishExchangeAssets(fileURLToPath(new URL('../control-panel/public/',import.meta.url)),folder);assert(files.includes('item-design-model.mjs'));for(const file of files.filter(f=>/\.(mjs|js)$/.test(f))){const source=readFileSync(path.join(folder,file),'utf8');for(const match of source.matchAll(/\bfrom\s*['"](\.[^'"]+)['"]/g))assert(existsSync(path.resolve(folder,match[1])),file+' → '+match[1]);}const model=await import(pathToFileURL(path.join(folder,'item-design-model.mjs')));assert.equal(model.itemDesignTemplate('dr_s1_legendary_mk14_helmet').equipmentMark,14);assert.match(readFileSync(path.join(folder,'codex-exchange.js'),'utf8'),/from '\.\/item-design-model\.mjs'/);}finally{assert(path.resolve(folder).startsWith(path.resolve(tmpdir())+path.sep+'dune-'));rmSync(folder,{recursive:true,force:true});}
});
test('a missing future import fails the publisher before replacing an existing public module',()=>{
const folder=mkdtempSync(path.join(tmpdir(),'dune-exchange-import-')),source=path.join(folder,'source'),output=path.join(folder,'output');mkdirSync(path.join(source,'cp-v2'),{recursive:true});mkdirSync(output);writeFileSync(path.join(source,'cp-v2/cp-v2-exchange-design.mjs'),"import {missing} from '../not-published.mjs';");writeFileSync(path.join(output,'codex-exchange.js'),'existing release');try{assert.throws(()=>publishExchangeAssets(source,output),/Unpublished Exchange dependency/);assert.equal(readFileSync(path.join(output,'codex-exchange.js'),'utf8'),'existing release');}finally{assert(path.resolve(folder).startsWith(path.resolve(tmpdir())+path.sep+'dune-'));rmSync(folder,{recursive:true,force:true});}
});