test(agent-presets): stabilize generation race coverage
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
|
import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from 'node:fs/promises'
|
||||||
import { tmpdir } from 'node:os'
|
import { tmpdir } from 'node:os'
|
||||||
import { dirname, join } from 'node:path'
|
import { dirname, join } from 'node:path'
|
||||||
import { fileURLToPath, pathToFileURL } from 'node:url'
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
||||||
@@ -542,6 +542,34 @@ describe('editing a composition file', () => {
|
|||||||
expect(livePresetMounts().filter(mount => mount.presetId === 'raced')).toHaveLength(2)
|
expect(livePresetMounts().filter(mount => mount.presetId === 'raced')).toHaveLength(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('keeps a newer generation pointer when a stale refresh loses the swap race', async () => {
|
||||||
|
const { scoped, path } = await editable('guarded-refresh')
|
||||||
|
const preset = await scoped.agentPresets.resolve('guarded-refresh')
|
||||||
|
await agentOn(scoped, 'sess-guarded-refresh-seed', 'guarded-refresh')
|
||||||
|
const service = scoped.agentPresets as unknown as {
|
||||||
|
standing: Map<string, Promise<{
|
||||||
|
key: unknown
|
||||||
|
scope: unknown
|
||||||
|
stamp: { mtimeMs: number; size: number }
|
||||||
|
}>>
|
||||||
|
ensureStanding(current: typeof preset): Promise<unknown>
|
||||||
|
}
|
||||||
|
const stalePromise = service.standing.get(preset.id)!
|
||||||
|
const stale = await stalePromise
|
||||||
|
await writeFile(path, rowFor('afterwards'))
|
||||||
|
const { mtimeMs, size } = await stat(path)
|
||||||
|
const newer = { ...stale, stamp: { mtimeMs, size } }
|
||||||
|
const newerPromise = Promise.resolve(newer)
|
||||||
|
|
||||||
|
// `await pending` yields before the guarded delete, letting the winning
|
||||||
|
// refresher replace the pointer deterministically instead of by timing.
|
||||||
|
const refresh = service.ensureStanding(preset)
|
||||||
|
service.standing.set(preset.id, newerPromise)
|
||||||
|
|
||||||
|
expect(await refresh).toBe(newer)
|
||||||
|
expect(service.standing.get(preset.id)).toBe(newerPromise)
|
||||||
|
})
|
||||||
|
|
||||||
it('hands a host reader the standing key without starting an agent', async () => {
|
it('hands a host reader the standing key without starting an agent', async () => {
|
||||||
const { scoped } = await editable('cold-read')
|
const { scoped } = await editable('cold-read')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user