mirror of
https://github.com/deepseek-ai/deepseek-harness
synced 2026-08-15 21:04:50 +00:00
Confirmed and fixed, each with a regression test that failed first:
- Concurrent update() lost patches (merge over one stale snapshot):
per-namespace serialized write queues; a failed write cannot poison
the queue for later writers.
- Fixed-name .tmp write followed planted symlinks and kept stale modes:
random-suffix sibling, exclusive-create (wx), 0600, cleanup on
failure, then rename.
- A throwing settings/updated listener escaped commit and permanently
wedged the provider reload chain (rejected refreshTask): commit now
contains listener failures (INVARIANT-coded errors still propagate),
async watcher rejections are adopted and contained
(watch callbacks are officially void | Promise<void>), and the
provider chains refreshes on a settled tail with an error log.
- No way to remove a user override: scope/service replace(section)
sets the user section wholesale; replace({}) re-inherits base and
schema defaults.
- The three-primitive provider contract did not hold (base never
called load()): the base Service.init loads and publishes once;
settings-local delegates via yield* super[Service.init]().
- Dispose did not quiesce: teardown flags closed, closes the watcher,
then awaits queued/in-flight reloads; closed is re-checked across
await points.
- Invariant now checks the authoritative relation with the seam's own
deepEqualJson: emitted next must equal settings.get(ns), and
next/prev must differ structurally (cosmokit dependency dropped).
- New docs/core-data-structures/settings.{md,zh.md} with type-equiv
blocks + manifest entries; catalog types moved from exemptions to
LINK_MAP; website page registered.
Both packages stay at per-file 100% coverage.
173 lines
6.4 KiB
TypeScript
173 lines
6.4 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { Context } from 'cordis'
|
|
import z from 'schemastery'
|
|
import { chmod, mkdtemp, rm, writeFile } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { settingsNamespace } from '@deepseek-ai/dsh-settings'
|
|
import { SettingsLocal } from '../src/index.ts'
|
|
|
|
// chokidar is the nondeterministic OS boundary: faking it lets these tests
|
|
// drive the event pipeline (error events, races with unreadable files)
|
|
// deterministically. Real end-to-end watching stays covered by local.spec.ts.
|
|
vi.mock('chokidar', async () => {
|
|
const { EventEmitter } = await import('node:events')
|
|
class FakeWatcher extends EventEmitter {
|
|
close = vi.fn(() => Promise.resolve())
|
|
}
|
|
const instances: Array<{ path: string; options: unknown; watcher: InstanceType<typeof FakeWatcher> }> = []
|
|
return {
|
|
watch: vi.fn((path: string, options: unknown) => {
|
|
const watcher = new FakeWatcher()
|
|
instances.push({ path, options, watcher })
|
|
return watcher
|
|
}),
|
|
__instances: instances,
|
|
}
|
|
})
|
|
|
|
interface FakeChokidar {
|
|
__instances: Array<{
|
|
path: string
|
|
options: { awaitWriteFinish: { stabilityThreshold: number; pollInterval: number } }
|
|
watcher: import('node:events').EventEmitter
|
|
}>
|
|
}
|
|
|
|
async function fakeInstances(): Promise<FakeChokidar['__instances']> {
|
|
const chokidar = await import('chokidar') as unknown as FakeChokidar
|
|
return chokidar.__instances
|
|
}
|
|
|
|
const ThemeSchema: z<{ theme: string }> = z.object({
|
|
theme: z.string().default('dark'),
|
|
})
|
|
|
|
const cleanups: Array<() => Promise<void>> = []
|
|
|
|
afterEach(async () => {
|
|
while (cleanups.length > 0) await cleanups.pop()!()
|
|
;(await fakeInstances()).length = 0
|
|
})
|
|
|
|
async function tempDir(): Promise<string> {
|
|
const dir = await mkdtemp(join(tmpdir(), 'dsh-settings-watch-'))
|
|
cleanups.push(() => rm(dir, { recursive: true, force: true }))
|
|
return dir
|
|
}
|
|
|
|
async function boot(config: ConstructorParameters<typeof SettingsLocal>[1]): Promise<Context> {
|
|
const ctx = new Context()
|
|
const fiber = ctx.plugin(SettingsLocal, config)
|
|
cleanups.push(async () => { await fiber.dispose() })
|
|
await fiber
|
|
return ctx
|
|
}
|
|
|
|
describe('watcher pipeline', () => {
|
|
it('clamps the write-settle poll interval for a zero debounce', async () => {
|
|
const dir = await tempDir()
|
|
await boot({ path: join(dir, 'settings.yaml'), debounceMs: 0 })
|
|
const [instance] = await fakeInstances()
|
|
expect(instance!.options.awaitWriteFinish).toEqual({ stabilityThreshold: 0, pollInterval: 1 })
|
|
})
|
|
|
|
it('survives a watcher error and keeps publishing later edits', async () => {
|
|
const dir = await tempDir()
|
|
const path = join(dir, 'settings.yaml')
|
|
const ctx = await boot({ path, debounceMs: 5 })
|
|
const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema)
|
|
const [instance] = await fakeInstances()
|
|
|
|
instance!.watcher.emit('error', new Error('watch backend failure'))
|
|
expect(scope.get()).toEqual({ theme: 'dark' })
|
|
|
|
await writeFile(path, 'ui-theme:\n theme: light\n')
|
|
instance!.watcher.emit('all', 'change', path)
|
|
await vi.waitFor(() => {
|
|
expect(scope.get()).toEqual({ theme: 'light' })
|
|
})
|
|
})
|
|
|
|
it('keeps the last good document when the file turns unreadable at runtime', async () => {
|
|
const dir = await tempDir()
|
|
const path = join(dir, 'settings.yaml')
|
|
await writeFile(path, 'ui-theme:\n theme: light\n')
|
|
const ctx = await boot({ path, debounceMs: 5 })
|
|
const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema)
|
|
|
|
await chmod(path, 0o000)
|
|
cleanups.push(() => chmod(path, 0o600))
|
|
const [instance] = await fakeInstances()
|
|
instance!.watcher.emit('all', 'change', path)
|
|
// The warn-and-keep path is asynchronous; give the serialized refresh a turn.
|
|
await new Promise(resolve => setTimeout(resolve, 50))
|
|
expect(scope.get()).toEqual({ theme: 'light' })
|
|
})
|
|
|
|
it('keeps the reload queue alive after an invariant violation escapes a commit', async () => {
|
|
const dir = await tempDir()
|
|
const path = join(dir, 'settings.yaml')
|
|
await writeFile(path, 'ui-theme:\n theme: light\n')
|
|
const ctx = await boot({ path, debounceMs: 5 })
|
|
const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema)
|
|
let arm = true
|
|
ctx.on('settings/updated', () => {
|
|
if (!arm) return
|
|
throw Object.assign(new Error('forged relation'), { code: 'INVARIANT' })
|
|
})
|
|
const [instance] = await fakeInstances()
|
|
|
|
await writeFile(path, 'ui-theme:\n theme: broken-commit\n')
|
|
instance!.watcher.emit('all', 'change', path)
|
|
await vi.waitFor(() => {
|
|
expect(scope.get().theme).toBe('broken-commit')
|
|
})
|
|
|
|
arm = false
|
|
await writeFile(path, 'ui-theme:\n theme: recovered\n')
|
|
instance!.watcher.emit('all', 'change', path)
|
|
await vi.waitFor(() => {
|
|
expect(scope.get().theme).toBe('recovered')
|
|
})
|
|
})
|
|
|
|
it('quiesces the refresh pipeline before dispose completes', async () => {
|
|
const dir = await tempDir()
|
|
const path = join(dir, 'settings.yaml')
|
|
await writeFile(path, 'ui-theme:\n theme: light\n')
|
|
const ctx = new Context()
|
|
const fiber = ctx.plugin(SettingsLocal, { path, debounceMs: 5 })
|
|
await fiber
|
|
ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema)
|
|
let disposed = false
|
|
let postDisposeCommits = 0
|
|
ctx.on('settings/updated', () => {
|
|
if (disposed) postDisposeCommits += 1
|
|
})
|
|
|
|
await writeFile(path, 'ui-theme:\n theme: darker\n')
|
|
const [instance] = await fakeInstances()
|
|
// Two queued refreshes: dispose interrupts one mid-flight and the other
|
|
// before it starts, so both closed guards must hold.
|
|
instance!.watcher.emit('all', 'change', path)
|
|
instance!.watcher.emit('all', 'change', path)
|
|
await fiber.dispose()
|
|
disposed = true
|
|
instance!.watcher.emit('all', 'change', path)
|
|
await new Promise(resolve => setTimeout(resolve, 100))
|
|
expect(postDisposeCommits).toBe(0)
|
|
})
|
|
|
|
it('treats an event for a still-absent file as a no-op', async () => {
|
|
const dir = await tempDir()
|
|
const path = join(dir, 'settings.yaml')
|
|
const ctx = await boot({ path, debounceMs: 5 })
|
|
const scope = ctx.settings.register(settingsNamespace('ui-theme'), ThemeSchema)
|
|
const [instance] = await fakeInstances()
|
|
instance!.watcher.emit('all', 'add', path)
|
|
await new Promise(resolve => setTimeout(resolve, 50))
|
|
expect(scope.get()).toEqual({ theme: 'dark' })
|
|
})
|
|
})
|