Skip to content

Commit 7d3fccb

Browse files
committed
test: wait for complete permission call records
1 parent 64a944d commit 7d3fccb

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

tests/zcode-client.test.mjs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ function newTestBroker(options) {
2626
return new ZCodeBrokerClass({ ...options, ...(ownershipPath === undefined ? {} : { ownershipPath }) });
2727
}
2828

29+
async function readRecordedCalls(record) {
30+
let content;
31+
try { content = await readFile(record, 'utf8'); } catch (error) {
32+
if (error?.code === 'ENOENT') return [];
33+
throw error;
34+
}
35+
return content.split('\n').flatMap((line) => {
36+
if (!line.trim()) return [];
37+
try { return [JSON.parse(line)]; } catch { return []; }
38+
});
39+
}
40+
41+
async function waitForRecordedCalls(record, predicate, timeoutMs = 500) {
42+
const deadline = Date.now() + timeoutMs;
43+
let calls = [];
44+
while (true) {
45+
calls = await readRecordedCalls(record);
46+
if (predicate(calls) || Date.now() >= deadline) return calls;
47+
await new Promise((resolvePromise) => setTimeout(resolvePromise, Math.min(5, deadline - Date.now())));
48+
}
49+
}
50+
2951
async function withClient(callback, env = {}, options = {}) {
3052
const directory = await mkdtemp(join(tmpdir(), 'zcode-client-'));
3153
const record = join(directory, 'calls.jsonl');
@@ -158,7 +180,7 @@ test('permission response must be an offered option and replay is rejected', asy
158180
client.setPermissionHandler(async () => ({ decision: 'allow', reason: 'not offered' }));
159181
await client.send(created.session.sessionId, 'permission');
160182
await client.waitForCompletion(created.session.sessionId);
161-
const calls = (await readFile(record, 'utf8')).trim().split('\n').map((line) => JSON.parse(line));
183+
const calls = await waitForRecordedCalls(record, (entries) => entries.filter((entry) => entry.error).length >= 2);
162184
assert.equal(calls.filter((entry) => entry.error).length, 2);
163185
}, { FAKE_ZCODE_PERMISSION: '1', FAKE_ZCODE_PERMISSION_REPLAY: '1' });
164186
});

0 commit comments

Comments
 (0)