import { test } from 'node:test' import assert from 'node:assert/strict' import { mkdtemp, mkdir, writeFile, readFile, readdir, rm } from 'node:fs/promises' import os from 'node:os' import path from 'node:path' import { fetchPluginRelease } from './fetch-plugin-release.mjs' function upstream({ failMain = false } = {}) { return async url => { if (url.includes('/releases/tags/')) return Response.json({ html_url: 'https://github.com/example/plugin/releases/tag/2.0.0', assets: [ { name: 'manifest.json', browser_download_url: 'https://assets.test/manifest.json' }, { name: 'main.js', browser_download_url: 'https://assets.test/main.js' }, ] }) if (url.endsWith('/manifest.json')) return Response.json({ id: 'plugin', version: '2.0.0' }) if (url.endsWith('/main.js')) return new Response(failMain ? 'unavailable' : 'new release code', { status: failMain ? 503 : 200 }) if (url.endsWith('/LICENSE')) return new Response('Upstream license') throw new Error(`Unexpected fetch: ${url}`) } } test('replacing a fixture drops optional files absent from the new upstream release', async () => { const root = await mkdtemp(path.join(os.tmpdir(), 'reams-plugin-release-')) try { const directory = path.join(root, 'plugin') await mkdir(directory) await writeFile(path.join(directory, 'styles.css'), 'obsolete stylesheet') await fetchPluginRelease('example/plugin', '2.0.0', directory, upstream()) assert.equal(await readFile(path.join(directory, 'main.js'), 'utf8'), 'new release code') assert.equal(await readFile(path.join(directory, 'LICENSE'), 'utf8'), 'Upstream license') assert.equal((await readdir(directory)).includes('styles.css'), false) assert.deepEqual(await readdir(root), ['plugin']) } finally { await rm(root, { recursive: true, force: true }) } }) test('a partial download failure leaves the previous fixture entirely intact', async () => { const root = await mkdtemp(path.join(os.tmpdir(), 'reams-plugin-release-')) try { const directory = path.join(root, 'plugin') await mkdir(directory) const previous = { 'manifest.json': '{"id":"plugin","version":"1.0.0"}', 'main.js': 'previous code', 'styles.css': 'previous styles' } for (const [file, contents] of Object.entries(previous)) await writeFile(path.join(directory, file), contents) await assert.rejects(fetchPluginRelease('example/plugin', '2.0.0', directory, upstream({ failMain: true })), /main.js: HTTP 503/) for (const [file, contents] of Object.entries(previous)) assert.equal(await readFile(path.join(directory, file), 'utf8'), contents) assert.deepEqual(await readdir(root), ['plugin']) } finally { await rm(root, { recursive: true, force: true }) } })