Files
deepseek-harness/packages/credentials/credentials-local/tests/drain.spec.ts
Yichen Jiang 03b534de16 feat(credentials): move the store to .credentials.yaml and layer $DSH_HOME/.env
$DSH_HOME/.env carried two incompatible jobs. As credentials-local's writable
secret store it could not be hoisted into process.env — hoisting makes every
stored key read as a read-only launch override and blocks rotation from the
TUI and the web page. But its name and dotenv format promise an environment
file, so a DEEPSEEK_BASE_URL sitting beside a working DEEPSEEK_API_KEY in the
same file was silently ignored: only the credential provider read the
document, and it addresses credential references alone.

Split the two jobs into two files.

.credentials.yaml is the provider-managed store: a strict YAML mapping of
CredentialRef to non-empty string, no version field, no wrapper level. Because
it holds credentials and nothing else, a non-mapping root, a non-identifier
key, a non-string value, an empty string, a duplicate key, and malformed YAML
are all rejections rather than skipped entries — loud at boot and at a write,
warn-and-keep-last-good on a live reload. The dotenv physical-line editor
gives way to a patch of the parsed document, so comments and untouched entries
keep their formatting and any string value round-trips, multi-line included.
Writer lock, read-modify-write, atomic 0600 write under a 0700 directory,
watcher, self-write suppression, and quiescent disposal are unchanged.

$DSH_HOME/.env becomes the user's ordinary environment layer. app-boot's new
loadLayeredEnv loads the invoking directory's .env then the Harness home's,
giving user < project < inherited; the home resolves from the inherited
environment first, so a project .env cannot redirect it.

Credential precedence is unchanged: the live environment still wins read-only
over the file, and shadowed writes still reject. Whether a provider-managed
store should instead win over the environment is a separate decision.

No migration: a key already in $DSH_HOME/.env keeps resolving through the new
environment layer, as a read-only env source that shadows the stored one.
2026-08-04 14:50:38 +08:00

72 lines
2.9 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import { Context } from 'cordis'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { credentialRef } from '@deepseek-ai/dsh-credentials'
import { CredentialsLocal } from '../src/index.ts'
// The atomic write is the gated asynchronous hold point inside a queued
// write; gating it makes the dispose-versus-queued-write race fully
// deterministic. The lock helper passes through so the gated operation still
// runs inside its real acquire/release cycle.
vi.mock('@deepseek-ai/dsh-atomic-write', async (importOriginal) => {
const actual = await importOriginal<typeof import('@deepseek-ai/dsh-atomic-write')>()
let gate: Promise<void> = Promise.resolve()
return {
...actual,
writeFileAtomic: vi.fn(() => gate),
__setGate: (next: Promise<void>) => {
gate = next
},
}
})
async function setGate(next: Promise<void>): Promise<void> {
const mocked = await import('@deepseek-ai/dsh-atomic-write') as unknown as { __setGate: (next: Promise<void>) => void }
mocked.__setGate(next)
}
const KEY = credentialRef('DSH_CRED_DRAIN_A')
const OTHER = credentialRef('DSH_CRED_DRAIN_B')
const cleanups: Array<() => Promise<void>> = []
afterEach(async () => {
await setGate(Promise.resolve())
while (cleanups.length > 0) await cleanups.pop()!()
})
describe('write-drain teardown', () => {
it('lets the in-flight write land and fails the queued one after disposal', async () => {
const dir = await mkdtemp(join(tmpdir(), 'dsh-credentials-drain-'))
cleanups.push(() => rm(dir, { recursive: true, force: true }))
const ctx = new Context()
const fiber = ctx.plugin(CredentialsLocal, { path: join(dir, '.credentials.yaml'), watch: false })
await fiber
const service = ctx.credentials
let release!: () => void
await setGate(new Promise<void>((resolveGate) => {
release = resolveGate
}))
const first = service.set(KEY, 'one')
// Let the first task pass its liveness checks and park on the gate, so it
// is genuinely in-flight when disposal begins.
await new Promise(resolvePause => setTimeout(resolvePause, 5))
// Attach the rejection handler up front: the queued write fails while the
// drain is still awaited, before any later `await expect` could run.
const secondRejects = expect(service.set(OTHER, 'two')).rejects.toThrow(/disposed before the queued/)
const disposal = fiber.dispose()
// Give the drain disposer its first turn (set closed) before opening the gate.
await new Promise(resolvePause => setTimeout(resolvePause, 10))
release()
await disposal
await expect(first).resolves.toBeUndefined()
await secondRejects
expect(await service.resolve(KEY)).toEqual({ value: 'one', source: 'file' })
expect(await service.resolve(OTHER)).toBeUndefined()
})
})