Skip to content

Commit 9a948ba

Browse files
committed
test: add edge case tests to achieve 100% coverage
1 parent 9fbd832 commit 9a948ba

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

tests/diary.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,70 @@ describe('Agent Diaries Core Logic', () => {
107107
expect(await agent.hasProcessedTask('Task 15')).toBe(true); // oldest in memory
108108
expect(await agent.hasProcessedTask('Task 14')).toBe(false); // evicted from memory
109109
});
110+
111+
it('edge case: should handle empty or null task titles gracefully', async () => {
112+
// Tests line 48 in diary.ts
113+
const s1 = AgentDiary.normalizeSignature('');
114+
const s2 = AgentDiary.normalizeSignature(null as any);
115+
const s3 = AgentDiary.normalizeSignature(undefined as any);
116+
expect(s1).toBe('');
117+
expect(s2).toBe('');
118+
expect(s3).toBe('');
119+
});
120+
121+
it('edge case: default storage initialization', () => {
122+
// Tests line 31 in diary.ts
123+
const agent = new AgentDiary({ agentId: 'default-storage-agent' });
124+
expect(agent).toBeDefined();
125+
// Clean up default storage dir created by this test
126+
const defaultDir = path.join(process.cwd(), '.agent-diaries');
127+
if (fs.existsSync(defaultDir)) {
128+
fs.rmSync(defaultDir, { recursive: true, force: true });
129+
}
130+
});
131+
132+
it('storage edge case: creating base directory if it does not exist', () => {
133+
// Tests line 17 in storage.ts
134+
const newDir = path.join(__dirname, '.new-test-dir');
135+
if (fs.existsSync(newDir)) {
136+
fs.rmSync(newDir, { recursive: true, force: true });
137+
}
138+
const newStorage = new LocalFileStorage({ baseDir: newDir });
139+
expect(fs.existsSync(newDir)).toBe(true);
140+
fs.rmSync(newDir, { recursive: true, force: true });
141+
});
142+
143+
it('storage edge case: handling corrupted JSON read', async () => {
144+
// Tests lines 36-37 in storage.ts
145+
const agent = new AgentDiary({ agentId: 'corrupt-agent', storage });
146+
await agent.writeTaskResult('Task 1', 'Success');
147+
148+
const filePath = path.join(TEST_DIR, 'diary_corrupt-agent.json');
149+
fs.writeFileSync(filePath, '{ bad json ]');
150+
151+
// Attempting to read should catch error and return empty state
152+
const state = await agent.readDiary();
153+
expect(state.runCount).toBe(0);
154+
});
155+
156+
it('storage edge case: handling file write errors', async () => {
157+
// Tests lines 47-48 in storage.ts
158+
// To force a write error on all OSes, we create a file, then pass it as the baseDir.
159+
// writeFile will throw ENOTDIR when it tries to write inside a file.
160+
const fileAsDir = path.join(__dirname, '.file-as-dir');
161+
fs.writeFileSync(fileAsDir, 'not a dir');
162+
const invalidStorage = new LocalFileStorage({ baseDir: fileAsDir });
163+
164+
let caughtError = false;
165+
try {
166+
await invalidStorage.set('test-key', { test: 1 } as any);
167+
} catch (e) {
168+
caughtError = true;
169+
}
170+
171+
expect(caughtError).toBe(true);
172+
173+
// Cleanup
174+
fs.rmSync(fileAsDir, { force: true });
175+
});
110176
});

0 commit comments

Comments
 (0)