Files
hermes-ui/tests/server/safe-file-store.test.ts
toor 1e9ea89d86
Build / build (push) Canceled after 0s
NPM Lockfile Check / npm ci --ignore-scripts (push) Canceled after 0s
Playwright / e2e (push) Canceled after 0s
chore: bootstrap hermes-ui skeleton from 0.6.29 snapshot
Import the client, server, skills, tests, and build scripts as the starting
point for the rebuild, then trim the surfaces this project does not ship:
Electron desktop packaging, the marketing website, and the ESP32 firmware
tree. CI keeps the web build, e2e, lockfile, release, and Docker workflows.

Rename the package and CLI entry points to hermes-ui, drop the prepare hook so
npm ci no longer triggers a build, and drop the removed tsconfig and nodemon
references. Rewrite AGENTS.md, ARCHITECTURE.md, DEVELOPMENT.md, and the READMEs
so the documented package map and hard rules match this repository.

reference_code/ is ignored: it is a read-only snapshot, not an input to the build.
2026-10-16 10:34:35 +08:00

55 lines
1.9 KiB
TypeScript

import { mkdtemp, readFile, rm, writeFile } from 'fs/promises'
import { tmpdir } from 'os'
import { join } from 'path'
import { afterEach, describe, expect, it } from 'vitest'
import YAML from 'js-yaml'
import { SafeFileStore } from '../../packages/server/src/services/safe-file-store'
const tempDirs: string[] = []
async function tempFile(name: string): Promise<string> {
const dir = await mkdtemp(join(tmpdir(), 'hermes-safe-file-store-'))
tempDirs.push(dir)
return join(dir, name)
}
afterEach(async () => {
await Promise.all(tempDirs.splice(0).map(dir => rm(dir, { recursive: true, force: true })))
})
describe('SafeFileStore', () => {
it('serializes concurrent YAML read-modify-write updates for the same file', async () => {
const store = new SafeFileStore()
const file = await tempFile('config.yaml')
await writeFile(file, 'model:\n default: old\n', 'utf-8')
await Promise.all([
store.updateYaml(file, async (cfg) => {
await new Promise(resolve => setTimeout(resolve, 25))
cfg.model.default = 'glm-5.1'
return cfg
}),
store.updateYaml(file, (cfg) => {
cfg.platforms = cfg.platforms || {}
cfg.platforms.api_server = { extra: { port: 8648 } }
return cfg
}),
])
const result = YAML.load(await readFile(file, 'utf-8')) as any
expect(result.model.default).toBe('glm-5.1')
expect(result.platforms.api_server.extra.port).toBe(8648)
})
it('backs up the previous content and writes through a temporary file', async () => {
const store = new SafeFileStore()
const file = await tempFile('config.yaml')
await writeFile(file, 'model:\n default: old\n', 'utf-8')
await store.writeText(file, 'model:\n default: new\n', { backup: true })
await expect(readFile(`${file}.bak`, 'utf-8')).resolves.toContain('default: old')
await expect(readFile(file, 'utf-8')).resolves.toContain('default: new')
})
})