import { test, before, after, beforeEach } from 'node:test' import assert from 'node:assert/strict' import http from 'node:http' let srv, mod, hits const g = { status: 200, body: null } before(async () => { srv = http.createServer((req, res) => { hits++ res.setHeader('Content-Type', 'application/json') res.statusCode = g.status res.end(JSON.stringify(g.body)) }) await new Promise((r) => srv.listen(0, '127.0.0.1', r)) process.env.GITEA_BASE_URL = `http://127.0.0.1:${srv.address().port}` process.env.GITEA_API_TOKEN = 'tok' mod = await import('../server/services/previewDevScript.js') }) after(() => { srv.closeAllConnections(); srv.close() }) beforeEach(() => { hits = 0; g.status = 200; mod.clearDevScriptCache() }) const pkg = (o) => ({ content: Buffer.from(JSON.stringify(o)).toString('base64') }) test('dev-Skript vorhanden => true', async () => { g.body = pkg({ scripts: { dev: 'vite' } }) assert.equal(await mod.repoHasDevScript('o/r', 'main'), true) }) test('package.json fehlt (404) => false', async () => { g.status = 404; g.body = { message: 'nf' } assert.equal(await mod.repoHasDevScript('o/r', 'main'), false) }) test('kein oder leeres dev-Skript => false', async () => { g.body = pkg({ scripts: { build: 'x' } }) assert.equal(await mod.repoHasDevScript('o/r', 'main'), false) mod.clearDevScriptCache() g.body = pkg({ scripts: { dev: ' ' } }) assert.equal(await mod.repoHasDevScript('o/r', 'main'), false) }) test('Gitea-Fehler => null', async () => { g.status = 500; g.body = { message: 'boom' } assert.equal(await mod.repoHasDevScript('o/r', 'main'), null) }) test('Cache 60 s je Repo+Branch', async () => { g.body = pkg({ scripts: { dev: 'vite' } }) await mod.repoHasDevScript('o/r', 'main') await mod.repoHasDevScript('o/r', 'main') assert.equal(hits, 1) await mod.repoHasDevScript('o/r', 'other') assert.equal(hits, 2) })