发布 0.2.1 并完善工具边界与安全性

This commit is contained in:
mid
2026-08-10 14:49:43 +08:00
parent aca004c513
commit 32b7cc827c
47 changed files with 681 additions and 172 deletions
+30 -1
View File
@@ -1,9 +1,10 @@
import { mkdtemp, readFile, rm } from 'node:fs/promises'
import { 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 {
normalizeNavigation,
readOrCreateNavigation,
validateNavigation,
writeJsonAtomic,
} from '../server/navigation.mjs'
@@ -41,6 +42,34 @@ describe('navigation config', () => {
})
})
it('migrates the legacy ASCII/Hex ID without changing its saved position', async () => {
const directory = await mkdtemp(path.join(tmpdir(), 'web-tools-navigation-'))
temporaryDirectories.push(directory)
const filePath = path.join(directory, 'navigation.json')
const utf8Catalog = {
categories: [{ id: 'encoding' }],
tools: [
{ id: 'utf8-hex', category: 'encoding' },
{ id: 'base64', category: 'encoding' },
],
}
const utf8Defaults = {
version: 1,
categories: [{ id: 'encoding', tools: ['utf8-hex', 'base64'] }],
}
await writeFile(filePath, JSON.stringify({
version: 1,
categories: [{ id: 'encoding', tools: ['base64', 'ascii-hex'] }],
}))
const expected = {
version: 1,
categories: [{ id: 'encoding', tools: ['base64', 'utf8-hex'] }],
}
await expect(readOrCreateNavigation(filePath, utf8Catalog, utf8Defaults)).resolves.toEqual(expected)
expect(JSON.parse(await readFile(filePath, 'utf8'))).toEqual(expected)
})
it('rejects duplicate or incomplete configurations', () => {
expect(() => validateNavigation({ version: 1, categories: [{ id: 'encoding', tools: ['hex', 'hex'] }] }, catalog)).toThrow('重复')
expect(() => validateNavigation({ version: 1, categories: [{ id: 'encoding', tools: ['hex'] }, { id: 'crypto', tools: ['cmac'] }] }, catalog)).toThrow('全部')