Files
Webklar-Kundenbereich/test/previewIndexParity.test.js

61 lines
3.3 KiB
JavaScript

import { test } from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { previewDeployInternals as legacy } from '../server/services/previewDeploy.js'
import { createJobs } from '../runner/src/jobs.js'
const FIXTURES = {
doener: { 'Doener_Nation_Website.html': '<html>Döner</html>', '.webklar-preview.json': '{"type":"static"}' },
leer: { 'readme.txt': 'x' },
mehrere: { 'a.html': 'aaaa', 'z.html': 'zzzzzzzz', 'css/s.css': 'c' },
bevorzugt: { 'about.html': 'AAAAAAAAAA', 'main.html': 'm' },
index: { 'index.html': 'I', 'other.html': 'o' },
typStatic: { 'index.html': 'I', 'package.json': '{"scripts":{"build":"x"}}', '.webklar-preview.json': '{"type":"static"}', 'a.css': 'c' },
unterordner: { 'site/home.html': 'H', 'site/a.css': 'c', 'root.html': 'r' },
}
function walk(dir, base = dir) {
return fs.readdirSync(dir, { withFileTypes: true }).flatMap((e) => {
const p = path.join(dir, e.name)
return e.isDirectory() ? walk(p, base) : [path.relative(base, p)]
}).sort()
}
async function viaRunner(files, index) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'parity-r-'))
const config = { giteaUrl: 'https://g', giteaToken: 'tok_SECRET_123456', previewsRoot: path.join(root, 'p'), workRoot: path.join(root, 'w'), cacheRoot: path.join(root, 'c'), minFreeBytes: 1, buildTimeoutMs: 1e6 }
const git = async ({ dir }) => {
for (const [f, c] of Object.entries(files)) { fs.mkdirSync(path.dirname(path.join(dir, f)), { recursive: true }); fs.writeFileSync(path.join(dir, f), c) }
fs.mkdirSync(path.join(dir, '.git'), { recursive: true }); fs.writeFileSync(path.join(dir, '.git/config'), 'x')
}
const jobs = createJobs({ config, docker: { run: async () => ({ code: 0 }) }, git, mem: () => 1e10, fs: { chownR: async () => {} }, setTimer: () => {} })
const id = jobs.submit({ sub: 'demo', repo: 'o/r', sha: 'abcdef1', branch: 'main', mode: 'auto', env: {}, displayName: 'Demo', ...(index ? { index } : {}) })
for (let i = 0; i < 400 && !['done', 'failed'].includes(jobs.get(id).state); i++) await new Promise((r) => setTimeout(r, 5))
assert.equal(jobs.get(id).state, 'done')
return path.join(config.previewsRoot, 'demo')
}
async function viaLegacy(files, index) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'parity-l-'))
const src = path.join(root, 'src')
for (const [f, c] of Object.entries(files)) { fs.mkdirSync(path.dirname(path.join(src, f)), { recursive: true }); fs.writeFileSync(path.join(src, f), c) }
fs.mkdirSync(path.join(src, '.git'), { recursive: true }); fs.writeFileSync(path.join(src, '.git/config'), 'x')
const target = path.join(root, 'out')
await legacy.deployStatic(src, target, index || '')
await legacy.ensureIndexHtml(target, 'Demo')
return target
}
for (const [name, files] of Object.entries(FIXTURES)) {
for (const index of name === 'unterordner' ? ['', 'site', 'fehlt'] : ['']) {
test(`Parität Altpfad/Runner: ${name}${index ? ` (index=${index})` : ''}`, async () => {
const a = await viaLegacy(files, index)
const b = await viaRunner(files, index)
assert.deepEqual(walk(b), walk(a))
for (const f of walk(a)) assert.deepEqual(fs.readFileSync(path.join(b, f)), fs.readFileSync(path.join(a, f)), f)
})
}
}