Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

test(companions): avoid repeated large shelf fixture scans

Chris9eca15c0605c
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botSkillQueryIndex.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botSkillQueryIndex.test.ts
index 9c039ced48..fc20ef8257 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botSkillQueryIndex.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botSkillQueryIndex.test.ts
@@ -1,4 +1,4 @@
-import { promises as fs } from 'node:fs';
+import { promises as fs, type WatchListener, type WatchOptions } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
@@ -6,11 +6,14 @@ import { buildBotSkillQueryIndex, queryBotSkillIndex, searchBotSkillQueryIndex }
import { botSkillRootDir, botSkillsDir, listBotSkills, saveBotSkill, deleteBotSkill } from '../botSkillStore';
import { listBotSkillsForSession } from '../botSkillService';
-const observation = vi.hoisted(() => ({ native: true }));
+const observation = vi.hoisted(() => ({ native: true, changes: new Map<string, (filename: string) => void>() }));
vi.mock('node:fs', async importOriginal => {
const actual = await importOriginal<typeof import('node:fs')>();
- return { ...actual, watch: (...args: Parameters<typeof actual.watch>) => observation.native
- ? actual.watch(...args) : { close() {}, on() { return this; } } };
+ return { ...actual, watch: (file: string, options: Pick<WatchOptions, 'recursive' | 'persistent'>, listener: WatchListener<string>) => {
+ if (observation.native) return actual.watch(file, options, listener);
+ observation.changes.set(file, filename => listener('change', filename));
+ return { close() { observation.changes.delete(file); }, on() { return this; } };
+ } };
});
let home: string;
@@ -19,7 +22,7 @@ const root = () => botSkillRootDir(home, bot);
const deps = () => ({ userDataDir: home, resolveBotId: async () => ({ ok: true as const, botId: bot }) });
const query = (params = {}) => queryBotSkillIndex(root(), home, bot, params);
beforeEach(async () => { home = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-skill-query-')); });
-afterEach(async () => { observation.native = true; vi.restoreAllMocks(); await fs.rm(home, { recursive: true, force: true }); });
+afterEach(async () => { observation.native = true; observation.changes.clear(); vi.restoreAllMocks(); await fs.rm(home, { recursive: true, force: true }); });
async function write(slug: string, name: string, description = 'Searchable', disabled = false, body = 'Steps') {
const dir = path.join(root(), disabled ? 'disabled-skills' : 'skills', slug);
await fs.mkdir(dir, { recursive: true });
@@ -85,6 +88,10 @@ it('preserves full metadata matching, locale ordering, ties, disabled duplicates
});
it('refreshes saved/deleted/disabled/external metadata and missing catalogs without mixing owners', async () => {
+ // Exercise the actual watcher callback deterministically. Native notification
+ // timing is covered by the runtime projection's hand-edit test; it can miss
+ // changes made immediately after watch() during this multi-step query test.
+ observation.native = false;
await write('alpha', 'Alpha', 'Original');
await write('off', 'Off', 'Disabled', true);
expect((await query()).total).toBe(2);
@@ -93,8 +100,11 @@ it('refreshes saved/deleted/disabled/external metadata and missing catalogs with
await deleteBotSkill(home, bot, 'alpha');
expect((await query()).skills.map(item => item.slug)).toEqual(['off']);
await write('off', 'Off', 'Hand edited disabled', true);
- // Native filesystem notifications can be coalesced (not synchronous with writeFile).
- await vi.waitFor(async () => expect(await query({ query: 'edited' })).toMatchObject({ total: 1 }), { timeout: 5000 });
+ expect(await query({ query: 'edited' })).toMatchObject({ total: 0 });
+ const changed = observation.changes.get(path.join(root(), 'disabled-skills'));
+ expect(changed).toBeTypeOf('function');
+ changed!(path.join('off', 'SKILL.md'));
+ expect(await query({ query: 'edited' })).toMatchObject({ total: 1 });
await fs.mkdir(botSkillsDir(home, bot), { recursive: true });
await fs.rename(path.join(root(), 'disabled-skills/off'), path.join(root(), 'skills/off'));
expect((await query()).skills[0].enabled).toBeUndefined();
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botSkillRuntimeProjection.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botSkillRuntimeProjection.test.ts
index 52159a9276..ef36596529 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botSkillRuntimeProjection.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botSkillRuntimeProjection.test.ts
@@ -99,7 +99,7 @@ describe('complete personal Skills with bounded startup projection', () => {
expect(await fs.readFile(path.join(disabled, 'oversized', 'SKILL.md'), 'utf8')).toBe(source);
});
- it('keeps every Skill in a large shelf discoverable, including the final page and later additions', async () => {
+ it('keeps every Skill in a large shelf discoverable, including the final page', async () => {
const count = 2048;
// Bounded filesystem concurrency; these are real files consumed by the store.
for (let start = 0; start < count; start += 32) {
@@ -110,7 +110,7 @@ describe('complete personal Skills with bounded startup projection', () => {
}
await writeSkill('disabled', '---\nname: disabled\ndescription: Leave disabled\n---\nDo not run\n', true);
const mounts = await assertBoundedNativeMounts();
- let catalog = (await fs.readFile(path.join(mounts.pluginRoot, 'catalog.jsonl'), 'utf8')).trim().split('\n').map(line => JSON.parse(line));
+ const catalog = (await fs.readFile(path.join(mounts.pluginRoot, 'catalog.jsonl'), 'utf8')).trim().split('\n').map(line => JSON.parse(line));
expect(catalog).toHaveLength(count);
expect(new Set(catalog.map(item => item.slug)).size).toBe(count);
const last = catalog.find(item => item.slug === 'skill-02047');
@@ -130,16 +130,27 @@ describe('complete personal Skills with bounded startup projection', () => {
const lastPage = await listBotSkillsForSession({ callerSessionId: 'session', query: 'workflow', offset: count - 1 }, deps());
expect(lastPage).toMatchObject({ ok: true, skills: [{ slug: 'skill-02047' }] });
expect(lastPage).not.toHaveProperty('nextOffset');
+ }, 30_000);
+
+ it('refreshes a projected catalog after additions and rebuilds it after deletion', async () => {
+ // Enter the same discovery projection by metadata size. Mutation/recovery
+ // do not need two more scans of the 2,048-file completeness fixture above.
+ const source = `---\nname: oversized\ndescription: ${'Long description '.repeat(30)}\n---\nOriginal instructions\n`;
+ const original = await writeSkill('oversized', source);
+ const mounts = await assertBoundedNativeMounts();
+ const catalogPath = path.join(mounts.pluginRoot, 'catalog.jsonl');
+ expect((await fs.readFile(catalogPath, 'utf8')).trim().split('\n')).toHaveLength(1);
await writeSkill('z-new', '---\nname: z-new\ndescription: Newly learned\n---\nNew steps\n');
await collectBotOwnSkillMounts(botId, deps());
- catalog = (await fs.readFile(path.join(mounts.pluginRoot, 'catalog.jsonl'), 'utf8')).trim().split('\n').map(line => JSON.parse(line));
- expect(catalog).toHaveLength(count + 1);
- expect(catalog.some(item => item.slug === 'z-new')).toBe(true);
- await fs.unlink(path.join(mounts.pluginRoot, 'catalog.jsonl'));
+ const catalog = (await fs.readFile(catalogPath, 'utf8')).trim().split('\n').map(line => JSON.parse(line));
+ expect(catalog.map(item => item.slug).sort()).toEqual(['oversized', 'z-new']);
+ expect(await fs.readFile(catalog.find(item => item.slug === 'z-new').filePath, 'utf8')).toContain('New steps');
+ await fs.unlink(catalogPath);
await collectBotOwnSkillMounts(botId, deps());
- expect((await fs.readFile(path.join(mounts.pluginRoot, 'catalog.jsonl'), 'utf8')).trim().split('\n')).toHaveLength(count + 1);
+ expect((await fs.readFile(catalogPath, 'utf8')).trim().split('\n').map(line => JSON.parse(line))).toEqual(catalog);
+ expect(await fs.readFile(original, 'utf8')).toBe(source);
expect(await fs.readdir(path.join(mounts.pluginRoot))).not.toEqual(expect.arrayContaining([expect.stringMatching(/\.tmp$/)]));
- }, 30_000);
+ });
it('writes a 100,000-entry stream before enumeration finishes, with atomic failure recovery', async () => {
const root = botSkillRootDir(userDataDir, botId);