127 lines
8.5 KiB
JavaScript
127 lines
8.5 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { createServer } from 'node:http';
|
|
import { once } from 'node:events';
|
|
import { mkdtemp, mkdir, readFile, writeFile, rm } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { join, dirname } from 'node:path';
|
|
import { createSiteApplication } from '../server/site-app.mjs';
|
|
|
|
const deferred = () => { let resolve; const promise = new Promise(r => resolve = r); return { promise, resolve }; };
|
|
|
|
test('save/render publication builds current content and reliably sends changes', async t => {
|
|
const root = await mkdtemp(join(tmpdir(), 'cms-publication-test-'));
|
|
const site = join(root, 'site');
|
|
const configPath = join(root, 'mission.json');
|
|
const fixtures = {
|
|
'content/pages/home.json': JSON.stringify({ title: 'Alpha', seo: 'SEO A', regions: [] }),
|
|
'content/knowledge.json': JSON.stringify({ title: 'Knowledge A' }),
|
|
'content/block-templates/home.json': '[]',
|
|
'templates/pages/home/shell.html': '<html></html>',
|
|
'tools/render-home.mjs': 'import {readFile,writeFile} from "node:fs/promises";const p=JSON.parse(await readFile("content/pages/home.json"));await writeFile("index.html",`<title>${p.seo}</title><h1>${p.title}</h1><img src="assets/photo.png">`);',
|
|
'tools/render-knowledge.mjs': 'import {readFile,writeFile,mkdir} from "node:fs/promises";const k=JSON.parse(await readFile("content/knowledge.json"));await mkdir("knowledge",{recursive:true});await writeFile("knowledge/index.html",`<h1>${k.title}</h1>`);',
|
|
'index.html': 'stale HTML',
|
|
'assets/photo.png': 'image-a',
|
|
};
|
|
for (const [path, value] of Object.entries(fixtures)) {
|
|
await mkdir(dirname(join(site, path)), { recursive: true });
|
|
await writeFile(join(site, path), value);
|
|
}
|
|
await writeFile(configPath, JSON.stringify({ id: 'mission', name: 'Mission', siteRoot: site, deploy: { enabled: false } }));
|
|
const remote = new Map(); const uploads = [];
|
|
let connectCount = 0, failTransfer = false, gate = null;
|
|
const app = await createSiteApplication({ projectId: 'mission', projectRoot: site, projectsRoot: root, hooks: {
|
|
transaction: fn => fn(), validateDeploy: async () => {},
|
|
writeConfig: async (_id, value) => writeFile(configPath, JSON.stringify(value)),
|
|
createSftpClient: () => ({
|
|
connect: async () => { connectCount++; }, mkdir: async () => {}, end: async () => {},
|
|
stat: async path => { if (!remote.has(path)) throw new Error('missing'); return { size: remote.get(path).length, modifyTime: 1000 }; },
|
|
fastPut: async (local, path, options) => {
|
|
if (failTransfer) throw new Error('fixture SFTP rejected transfer');
|
|
if (gate) { const current = gate; gate = null; current.entered.resolve(); await current.release.promise; }
|
|
const data = await readFile(local); remote.set(path, data); uploads.push(path); options.step(data.length);
|
|
},
|
|
}),
|
|
} });
|
|
const server = createServer(app.handle); server.listen(0, '127.0.0.1'); await once(server, 'listening');
|
|
t.after(async () => { await new Promise(resolve => server.close(resolve)); await rm(root, { recursive: true, force: true }); });
|
|
const base = `http://127.0.0.1:${server.address().port}`;
|
|
async function api(path, method = 'GET', body) {
|
|
const response = await fetch(base + path, { method, headers: { 'content-type': 'application/json' }, body: body === undefined ? undefined : JSON.stringify(body) });
|
|
const payload = await response.json(); assert.ok(response.ok, JSON.stringify(payload)); return payload;
|
|
}
|
|
async function configure(enabled = true, automatic = true) {
|
|
const config = await api('/api/project/config');
|
|
return api('/api/project/config', 'PUT', { ...config, deploy: { ...config.deploy, enabled, deployOnRender: automatic, method: 'sftp', host: 'fixture.invalid', port: 22, username: 'test', password: 'test-only', remotePath: 'httpdocs' } });
|
|
}
|
|
async function finished(id) {
|
|
const deadline = Date.now() + 10000;
|
|
while (Date.now() < deadline) {
|
|
const { job } = await api(`/api/project/publish/status?jobId=${id}`);
|
|
if (['completed', 'failed'].includes(job.state)) return job;
|
|
await new Promise(resolve => setTimeout(resolve, 15));
|
|
}
|
|
throw new Error('publication timeout');
|
|
}
|
|
const text = path => remote.get(`httpdocs/${path}`)?.toString();
|
|
await configure();
|
|
await t.test('manual Publish builds both pages instead of uploading stale HTML', async () => {
|
|
const { job } = await api('/api/project/publish/start', 'POST');
|
|
assert.equal((await finished(job.id)).state, 'completed');
|
|
assert.match(text('index.html'), /Alpha/); assert.equal(text('knowledge/index.html'), '<h1>Knowledge A</h1>');
|
|
assert.equal(text('assets/photo.png'), 'image-a');
|
|
assert.ok(![...remote.keys()].some(path => /content|secrets|publish-state/.test(path)));
|
|
});
|
|
await t.test('one-letter content/SEO and same-size image edits are not skipped; unchanged files are', async () => {
|
|
const before = uploads.length;
|
|
await api('/api/page/home', 'PUT', { title: 'Alphb', seo: 'SEO B', regions: [] });
|
|
await writeFile(join(site, 'assets/photo.png'), 'image-b');
|
|
const result = await api('/api/render/home', 'POST');
|
|
assert.ok(result.publishJob.id); const job = await finished(result.publishJob.id);
|
|
assert.equal(job.state, 'completed'); assert.equal(job.uploadedFiles, 2); assert.equal(job.skippedFiles, 1);
|
|
assert.match(text('index.html'), /Alphb/); assert.match(text('index.html'), /SEO B/);
|
|
assert.equal(text('assets/photo.png'), 'image-b'); assert.equal(uploads.length - before, 2);
|
|
});
|
|
await t.test('knowledge Save/render publishes its updated page', async () => {
|
|
await api('/api/knowledge', 'PUT', { title: 'Knowledge B' });
|
|
const result = await api('/api/render/knowledge', 'POST'); const job = await finished(result.publishJob.id);
|
|
assert.equal(job.state, 'completed'); assert.equal(job.uploadedFiles, 1);
|
|
assert.equal(text('knowledge/index.html'), '<h1>Knowledge B</h1>');
|
|
});
|
|
await t.test('edits saved during an active transfer trigger another pass and preserve newest revision', async () => {
|
|
await api('/api/page/home', 'PUT', { title: 'First', seo: 'SEO C', regions: [] });
|
|
const blocked = { entered: deferred(), release: deferred() }; gate = blocked;
|
|
const first = await api('/api/render/home', 'POST');
|
|
await blocked.entered.promise;
|
|
await api('/api/page/home', 'PUT', { title: 'Latest', seo: 'SEO D', regions: [] });
|
|
await writeFile(join(site, 'assets/photo.png'), 'image-c');
|
|
const second = await api('/api/render/home', 'POST');
|
|
assert.equal(first.publishJob.id, second.publishJob.id); assert.equal(second.publishJob.pendingChanges, true);
|
|
blocked.release.resolve();
|
|
assert.equal((await finished(first.publishJob.id)).state, 'completed');
|
|
assert.match(text('index.html'), /Latest/); assert.match(text('index.html'), /SEO D/); assert.equal(text('assets/photo.png'), 'image-c');
|
|
});
|
|
await t.test('SFTP failure is terminal and visible; retry rebuilds and sends pending changes', async () => {
|
|
await api('/api/knowledge', 'PUT', { title: 'Knowledge C' }); failTransfer = true;
|
|
const result = await api('/api/render/knowledge', 'POST'); const failed = await finished(result.publishJob.id);
|
|
assert.equal(failed.state, 'failed'); assert.match(failed.error, /SFTP rejected/);
|
|
assert.equal(text('knowledge/index.html'), '<h1>Knowledge B</h1>');
|
|
failTransfer = false;
|
|
const retry = await api('/api/project/publish/start', 'POST'); assert.equal((await finished(retry.job.id)).state, 'completed');
|
|
assert.equal(text('knowledge/index.html'), '<h1>Knowledge C</h1>');
|
|
});
|
|
await t.test('build failure never transfers stale pages', async () => {
|
|
const before = connectCount; await writeFile(join(site, 'tools/render-home.mjs'), 'throw new Error("broken template")');
|
|
const { job } = await api('/api/project/publish/start', 'POST');
|
|
assert.equal((await finished(job.id)).state, 'failed'); assert.equal(connectCount, before);
|
|
await writeFile(join(site, 'tools/render-home.mjs'), fixtures['tools/render-home.mjs']);
|
|
});
|
|
await t.test('disabled Agent-style deployment only rebuilds preview', async () => {
|
|
await configure(false); const before = connectCount;
|
|
await api('/api/page/home', 'PUT', { title: 'Local', seo: '', regions: [] });
|
|
const result = await api('/api/render/home', 'POST');
|
|
assert.equal(result.publish, null); assert.equal(result.publishJob, undefined); assert.equal(connectCount, before);
|
|
assert.match(await readFile(join(site, 'index.html'), 'utf8'), /Local/); assert.match(text('index.html'), /Latest/);
|
|
});
|
|
});
|