修复固件哈希兼容性并完善部署配置
This commit is contained in:
+48
-1
@@ -2,7 +2,7 @@ import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import path from 'node:path'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { createWebToolsServer, MAX_JSON_BODY_SIZE } from '../server/index.mjs'
|
||||
import { assertProductionBuild, createWebToolsServer, MAX_JSON_BODY_SIZE } from '../server/index.mjs'
|
||||
|
||||
const temporaryDirectories = []
|
||||
const servers = []
|
||||
@@ -47,6 +47,12 @@ async function createFixtureServer() {
|
||||
}
|
||||
|
||||
describe('HTTP server', () => {
|
||||
it('fails before listening when the production build is missing', async () => {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), 'web-tools-missing-build-'))
|
||||
temporaryDirectories.push(directory)
|
||||
await expect(assertProductionBuild(path.join(directory, 'dist'))).rejects.toThrow('npm.cmd run build')
|
||||
})
|
||||
|
||||
it('serves direct SPA navigation but returns 404 for missing assets', async () => {
|
||||
const { baseUrl } = await createFixtureServer()
|
||||
const page = await fetch(`${baseUrl}/tools/base64`, { headers: { Accept: 'text/html' } })
|
||||
@@ -88,6 +94,47 @@ describe('HTTP server', () => {
|
||||
expect(JSON.parse(await readFile(path.join(dataDir, 'navigation.json'), 'utf8'))).toEqual(updated)
|
||||
})
|
||||
|
||||
it('normalizes an existing navigation file when a later release adds a tool', async () => {
|
||||
const { baseUrl, dataDir } = await createFixtureServer()
|
||||
const legacy = { version: 1, categories: [{ id: 'encoding', tools: ['hex'] }, { id: 'crypto', tools: ['cmac'] }] }
|
||||
await mkdir(dataDir, { recursive: true })
|
||||
await writeFile(path.join(dataDir, 'navigation.json'), JSON.stringify(legacy))
|
||||
|
||||
const response = await fetch(`${baseUrl}/api/navigation`)
|
||||
expect(await response.json()).toEqual(legacy)
|
||||
|
||||
const expandedCatalog = { ...catalog, tools: [...catalog.tools, { id: 'base64', category: 'encoding' }] }
|
||||
const expandedDefaults = { ...defaults, categories: [{ id: 'encoding', tools: ['hex', 'base64'] }, ...defaults.categories.slice(1)] }
|
||||
const secondServer = await createWebToolsServer({
|
||||
staticRoot: path.join(dataDir, 'missing-dist'),
|
||||
dataDir,
|
||||
catalog: expandedCatalog,
|
||||
defaults: expandedDefaults,
|
||||
logger: { error: () => undefined },
|
||||
})
|
||||
servers.push(secondServer)
|
||||
await new Promise((resolve, reject) => {
|
||||
secondServer.once('error', reject)
|
||||
secondServer.listen(0, '127.0.0.1', resolve)
|
||||
})
|
||||
const address = secondServer.address()
|
||||
const upgraded = await fetch(`http://127.0.0.1:${address.port}/api/navigation`)
|
||||
expect(await upgraded.json()).toEqual({
|
||||
version: 1,
|
||||
categories: [
|
||||
{ id: 'encoding', tools: ['hex', 'base64'] },
|
||||
{ id: 'crypto', tools: ['cmac'] },
|
||||
],
|
||||
})
|
||||
expect(JSON.parse(await readFile(path.join(dataDir, 'navigation.json'), 'utf8'))).toEqual({
|
||||
version: 1,
|
||||
categories: [
|
||||
{ id: 'encoding', tools: ['hex', 'base64'] },
|
||||
{ id: 'crypto', tools: ['cmac'] },
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects invalid, oversized and cross-site navigation writes', async () => {
|
||||
const { baseUrl } = await createFixtureServer()
|
||||
const invalid = await fetch(`${baseUrl}/api/navigation`, {
|
||||
|
||||
Reference in New Issue
Block a user