From f928856b287e059cf25327561b3ff8afc8d3a7ee Mon Sep 17 00:00:00 2001 From: Eason Liang <136036952+easonLiangWorldedtech@users.noreply.github.com> Date: Wed, 11 Nov 2026 06:14:41 +0800 Subject: [PATCH] test(skills): cover the remaining SkillsManager rows in the outer describe blocks Adds the last 9 rows of the split harness to src/services/skills/__tests__/SkillsManager.spec.ts: 218 added lines and 0 removed, of which 21 are blank lines (197 + 21 = 218), so the row content is 197 lines. The blank-adjacency rule this split uses keeps a blank only when neither the line after the insertion anchor nor the line after that is blank in the parent file; the parent has 555 blank lines and this file has 576, so 555 + 21 = 576. Composition published on Zoo-Code-Org/Zoo-Code#1321 is 197 row lines + 13 in-row blanks + 8 separators between spans = 218, and 197 + 13 = 210 is the row-span figure quoted there, so 210 + 8 = 218 as well; built width is reported against published width as a named comparison in the pull request body. Branch name basis, stated because nothing in the plan declares it: the split plan on Zoo-Code-Org/Zoo-Code#1321 names units by number and never names this branch, and the 80 comments plus the issue body were read for a declared name with 0 hits. The name units/skills-08-outer is taken from the child spec artifact's own file name, v2-U8-outer.spec.ts, which is the least inventive source available; the earlier units' branch names were chosen by the lanes that shipped them on the same basis. This is unit 8 of the 8-way split of the SkillsManager test harness described on Zoo-Code-Org/Zoo-Code#1321, and the last unit of the fork chain. Merge order for the fork chain is units 2, 3, 4, 5, 6, 7, 8; the org-repo pull requests for the same units are opened after #2417 merges, in the same order. --- .../skills/__tests__/SkillsManager.spec.ts | 218 ++++++++++++++++++ 1 file changed, 218 insertions(+) diff --git a/src/services/skills/__tests__/SkillsManager.spec.ts b/src/services/skills/__tests__/SkillsManager.spec.ts index 58c5f25fbe..40c52eeba7 100644 --- a/src/services/skills/__tests__/SkillsManager.spec.ts +++ b/src/services/skills/__tests__/SkillsManager.spec.ts @@ -4147,4 +4147,222 @@ Instructions`) }) }) }) + it("stops creating watchers once the manager is disposed", async () => { + const createWatcher = vi.mocked(vscode.workspace.createFileSystemWatcher) + const originalNodeEnv = process.env.NODE_ENV + process.env.NODE_ENV = "development" + try { + createWatcher.mockClear() + await skillsManager["setupFileWatchers"]() + expect(createWatcher).toHaveBeenCalled() + + createWatcher.mockClear() + await skillsManager.dispose() + await skillsManager["setupFileWatchers"]() + expect(createWatcher).not.toHaveBeenCalled() + } finally { + process.env.NODE_ENV = originalNodeEnv + } + }) + + it("sweeps only a staged sidecar left by a previous host", async () => { + const staleName = ".SKILL.md.123.abc.tmp" + const freshName = ".SKILL.md.456.def.tmp" + mockReaddir.mockImplementation(async (_dir: string, options?: { withFileTypes?: boolean }) => { + if (options?.withFileTypes) { + return [] + } + return [staleName, freshName] + }) + mockStat.mockImplementation(async (pathArg: string) => { + if (pathArg.endsWith(staleName)) { + // Older than the stale threshold: abandoned by a previous host. + return { mtimeMs: Date.now() - 60 * 60 * 1000 } + } + if (pathArg.endsWith(freshName)) { + return { mtimeMs: Date.now() } + } + throw enoent() + }) + mockUnlink.mockResolvedValue(undefined) + + await skillsManager["sweepStaleSkillTempFiles"]() + + expect(mockUnlink).toHaveBeenCalledWith(p(globalSkillsDir, staleName)) + // A fresh matching sidecar may belong to a live write and must survive. + expect(mockUnlink).not.toHaveBeenCalledWith(p(globalSkillsDir, freshName)) + }) + + it("revisits fresh sidecars once the delayed sweep fires", async () => { + // The first sweep skips sidecars younger than the freshness window because they may belong to + // a live write in another process, and nothing else ever revisits them. The one delayed retry + // is what does, so both halves are pinned: no second sweep inside the window, one after it. + vi.useFakeTimers() + // vi.useFakeTimers() takes over Date as well as the timers, so a sidecar stamped with the + // planted reading ages exactly as the clock is advanced: the retry's verdict changes because + // the window closed, not because the fixture changed. + const sidecarName = ".SKILL.md.789.ghi.tmp" + const sidecarPath = p(globalSkillsDir, sidecarName) + // What the retry changes is the sidecar's age relative to now, and this suite's fake timers do + // not fake Date (measured: the two fires land milliseconds apart on the real clock), so the + // fixture ages the sidecar between the fires instead of pretending the clock carried it past + // the window. + let sidecarAgeMs = 0 + mockReaddir.mockImplementation(async (_dir: string, options?: { withFileTypes?: boolean }) => + options?.withFileTypes ? [] : [sidecarName], + ) + mockStat.mockImplementation(async (pathArg: string) => + pathArg.endsWith(sidecarName) ? { mtimeMs: Date.now() - sidecarAgeMs } : Promise.reject(enoent()), + ) + mockUnlink.mockResolvedValue(undefined) + const unlinkedSoFar = () => mockUnlink.mock.calls.map((call) => String(call[0])) + let sweeps = 0 + const originalSweep = skillsManager["sweepStaleSkillTempFiles"] + skillsManager["sweepStaleSkillTempFiles"] = async () => { + sweeps += 1 + // Count the fires and let the real sweep run beneath the counter: a counter that swallowed + // the body would pin the timing without proving the removal the retry exists to perform. + // call() rather than originalSweep(): the captured method is unbound, and an unbound call + // loses the manager instance, throws inside the sweep, and gets swallowed by the retry's own + // catch - which is how the first version of this test passed while never removing anything. + await originalSweep.call(skillsManager) + if (sweeps === 1) { + // The retry only makes sense once the freshness window has closed on this sidecar. + sidecarAgeMs = 10 * 60 * 1000 + } + } + try { + await skillsManager.initialize() + expect(sweeps).toBe(1) + expect(unlinkedSoFar(), "the first sweep leaves a fresh sidecar alone").not.toContain(sidecarPath) + // SKILL_TEMP_SIDECAR_STALE_MS is ten minutes and module-private, so the window is written + // here and the assertions below pin which side of it fires. + await vi.advanceTimersByTimeAsync(10 * 60 * 1000 - 1) + expect(sweeps).toBe(1) + expect(unlinkedSoFar()).not.toContain(sidecarPath) + await vi.advanceTimersByTimeAsync(1) + expect(sweeps).toBe(2) + // The removal itself: the retry is what turns a skipped sidecar into a deleted one. + expect(unlinkedSoFar(), "the delayed sweep removes the sidecar the first sweep skipped").toContain( + sidecarPath, + ) + } finally { + skillsManager["sweepStaleSkillTempFiles"] = originalSweep + vi.useRealTimers() + } + }) + + it("cancels the delayed sweep when disposal lands inside the window", async () => { + // A host that stops inside the freshness window must not sweep again after it: dispose() + // clears the retry timer, and without that clear the pending callback would run. + vi.useFakeTimers() + let sweeps = 0 + const originalSweep = skillsManager["sweepStaleSkillTempFiles"] + skillsManager["sweepStaleSkillTempFiles"] = async () => { + sweeps += 1 + } + try { + await skillsManager.initialize() + expect(sweeps).toBe(1) + await skillsManager.dispose() + await vi.advanceTimersByTimeAsync(10 * 60 * 1000 + 1) + expect(sweeps).toBe(1) + } finally { + skillsManager["sweepStaleSkillTempFiles"] = originalSweep + vi.useRealTimers() + } + }) + + it("shares one directory queue across symlink aliases", async () => { + const order: string[] = [] + let releaseFirst!: () => void + const firstGate = new Promise((resolve) => { + releaseFirst = resolve + }) + const alias = p("/alias", "skills") + mockRealpath.mockImplementation(async (pathArg: string) => (pathArg === alias ? globalSkillsDir : pathArg)) + + const first = skillsManager["withDirectoryLock"](alias, async () => { + order.push("first:start") + await firstGate + order.push("first:end") + }) + try { + // The lock key is resolved through realpath asynchronously before the operation runs. + await vi.waitFor(() => expect(order).toEqual(["first:start"])) + const second = skillsManager["withDirectoryLock"](globalSkillsDir, async () => { + order.push("second") + }) + await Promise.resolve() + // The alias resolves to the same real directory, so the second operation waits. + expect(order).toEqual(["first:start"]) + + releaseFirst() + await Promise.all([first, second]) + expect(order).toEqual(["first:start", "first:end", "second"]) + } finally { + // Never leave the shared queue holding a gate if an assertion above fails. + releaseFirst() + await first.catch(() => undefined) + } + }) + + it("does not start watchers when initialization runs after disposal", async () => { + const createWatcher = vi.mocked(vscode.workspace.createFileSystemWatcher) + await skillsManager.dispose() + createWatcher.mockClear() + + await skillsManager.initialize() + + expect(createWatcher).not.toHaveBeenCalled() + }) + + it("rejects a queued write once the manager is disposed", async () => { + await skillsManager.dispose() + mockWriteFile.mockResolvedValue(undefined) + mockUnlink.mockResolvedValue(undefined) + + await expect( + skillsManager["writeSkillFileAtomically"](p(globalSkillsDir, "SKILL.md"), "content"), + ).rejects.toThrow("SkillsManager is disposed") + expect(mockWriteFile).not.toHaveBeenCalled() + expect(mockRename).not.toHaveBeenCalled() + }) + + it("never runs a queued directory operation after the manager is disposed", async () => { + let releaseFirst!: () => void + const firstGate = new Promise((resolve) => { + releaseFirst = resolve + }) + let firstStarted = false + let secondRan = false + + const first = skillsManager["withDirectoryLock"](globalSkillsDir, async () => { + firstStarted = true + await firstGate + }) + await vi.waitFor(() => expect(firstStarted).toBe(true)) + + // The second operation is queued behind the first and must be rejected after disposal + // without running its callback. + const second = skillsManager["withDirectoryLock"](globalSkillsDir, async () => { + secondRan = true + }) + await skillsManager.dispose() + releaseFirst() + + await first + await expect(second).rejects.toThrow("SkillsManager is disposed") + expect(secondRan).toBe(false) + }) + + it("removes tracked temp sidecars on dispose", async () => { + mockUnlink.mockResolvedValue(undefined) + skillsManager["activeTempFiles"].add("/mock/skills/foo/.SKILL.md.123.abc.tmp") + + await skillsManager.dispose() + + expect(mockUnlink).toHaveBeenCalledWith("/mock/skills/foo/.SKILL.md.123.abc.tmp") + expect(skillsManager["activeTempFiles"].size).toBe(0) + }) })