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(plugins): stabilize library identity recovery on Windows

DavidShen9a0793706d14
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/libraryBinding.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/libraryBinding.test.ts
index c2242a9829..d1c4b9f546 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/libraryBinding.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/libraryBinding.test.ts
@@ -205,6 +205,21 @@ describe('LibraryBindingStore', () => {
expect(await fs.promises.readFile(path.join(candidate, 'hello', 'keep.txt'), 'utf8')).toBe('original');
});
+ // Windows CI can round distinct native inode values to the same Number.
+ // Model an observable identity change for these recovery unit tests while
+ // retaining real files, the production recovery entry, and restoration/retry.
+ async function mockChangedDirectoryIdentity(root: string, recordedInode: number) {
+ const realRoot = await fs.promises.realpath(root);
+ const lstat = fs.promises.lstat.bind(fs.promises);
+ return vi.spyOn(fs.promises, 'lstat').mockImplementation(async (...args) => {
+ const stat = await lstat(...args);
+ if (args[0] === realRoot) {
+ Object.assign(stat, { ino: recordedInode === 1 ? 2 : 1 });
+ }
+ return stat;
+ });
+ }
+
it('rejects a foreign target even with an unfinished relocation journal and retries after restoration', async () => {
await interruptRelocation('folder');
const target = path.join(candidate, '_ns__acme__hello');
@@ -212,11 +227,14 @@ describe('LibraryBindingStore', () => {
await fs.promises.rename(target, original);
await fs.promises.mkdir(target);
await fs.promises.writeFile(path.join(target, 'foreign.txt'), 'foreign');
+ const { libraryIdentity } = JSON.parse(await fs.promises.readFile(bindingFile, 'utf8')).pendingRelocation;
+ const targetIdentity = await mockChangedDirectoryIdentity(target, libraryIdentity.ino);
const restarted = new LibraryBindingStore(deps);
await expect(restarted.relocateBinding('hello', '_ns__acme__hello')).rejects.toThrow('directory identity changed');
await expect(restarted.getBinding('_ns__acme__hello')).rejects.toThrow('directory identity changed');
expect(await fs.promises.readFile(path.join(target, 'foreign.txt'), 'utf8')).toBe('foreign');
expect(JSON.parse(await fs.promises.readFile(bindingFile, 'utf8')).bindings.hello.generation).toBe(1);
+ targetIdentity.mockRestore();
await fs.promises.rm(target, { recursive: true });
await fs.promises.rename(original, target);
await restarted.relocateBinding('hello', '_ns__acme__hello');
@@ -229,18 +247,8 @@ describe('LibraryBindingStore', () => {
const saved = path.join(tmp, 'saved-parent');
await fs.promises.rename(candidate, saved);
await fs.promises.mkdir(candidate);
- // Windows inode numbers can exceed Number.MAX_SAFE_INTEGER, so two real
- // directories may compare equal. Exercise this root-rejection branch with
- // a distinct identity, then restore native stats for the successful retry.
- const { rootIdentity, record } = JSON.parse(contents).pendingRelocation;
- const lstat = fs.promises.lstat.bind(fs.promises);
- const parentIdentity = vi.spyOn(fs.promises, 'lstat').mockImplementation(async (...args) => {
- const stat = await lstat(...args);
- if (args[0] === record.realPathAtGrant) {
- Object.assign(stat, { ino: rootIdentity.ino === 1 ? 2 : 1 });
- }
- return stat;
- });
+ const { rootIdentity } = JSON.parse(contents).pendingRelocation;
+ const parentIdentity = await mockChangedDirectoryIdentity(candidate, rootIdentity.ino);
const restarted = new LibraryBindingStore(deps);
await expect(restarted.relocateBinding('hello', '_ns__acme__hello')).rejects.toThrow('root identity changed');
expect(await fs.promises.readFile(bindingFile, 'utf8')).toBe(contents);
@@ -258,10 +266,13 @@ describe('LibraryBindingStore', () => {
await fs.promises.rename(source, original);
await fs.promises.mkdir(source);
await fs.promises.writeFile(path.join(source, 'foreign.txt'), 'foreign');
+ const { libraryIdentity } = JSON.parse(await fs.promises.readFile(bindingFile, 'utf8')).pendingRelocation;
+ const sourceIdentity = await mockChangedDirectoryIdentity(source, libraryIdentity.ino);
const restarted = new LibraryBindingStore(deps);
await expect(restarted.relocateBinding('hello', '_ns__acme__hello')).rejects.toThrow('directory identity changed');
expect(await fs.promises.readFile(path.join(source, 'foreign.txt'), 'utf8')).toBe('foreign');
expect(fs.existsSync(path.join(candidate, '_ns__acme__hello'))).toBe(false);
+ sourceIdentity.mockRestore();
await fs.promises.rm(source, { recursive: true });
await fs.promises.rename(original, source);
await restarted.relocateBinding('hello', '_ns__acme__hello');