-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathclient_api.e2e.test.ts
More file actions
91 lines (73 loc) · 3.43 KB
/
client_api.e2e.test.ts
File metadata and controls
91 lines (73 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { describe, expect, it } from "vitest";
import { approveAll } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
describe("Client session management", async () => {
const { copilotClient: client } = await createSdkTestContext();
async function waitFor(predicate: () => Promise<boolean>, timeoutMs = 10_000): Promise<void> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (await predicate()) return;
await new Promise((resolve) => setTimeout(resolve, 50));
}
throw new Error(`Condition was not met within ${timeoutMs}ms`);
}
async function assertFailure(
action: () => Promise<unknown>,
expectedMessage: string
): Promise<void> {
await expect(action()).rejects.toSatisfy((err: unknown) => {
const text = err instanceof Error ? `${err.message}\n${err.stack ?? ""}` : String(err);
expect(text.toLowerCase()).toContain(expectedMessage.toLowerCase());
return true;
});
}
it("should get null last session id before any sessions exist", async () => {
await client.start();
const result = await client.getLastSessionId();
expect(result).toBeFalsy();
});
it("should delete session by id", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
const sessionId = session.sessionId;
await session.sendAndWait({ prompt: "Say OK." });
await waitFor(async () =>
(await client.listSessions()).some((s) => s.sessionId === sessionId)
);
await session.disconnect();
await client.deleteSession(sessionId);
const metadata = await client.getSessionMetadata(sessionId);
expect(metadata).toBeFalsy();
}, 60_000);
it("should report error when deleting unknown session id", async () => {
await client.start();
const unknownSessionId = "00000000-0000-0000-0000-000000000000";
await assertFailure(
() => client.deleteSession(unknownSessionId),
`Failed to delete session ${unknownSessionId}`
);
});
it("should track last session id after session created", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
await session.sendAndWait({ prompt: "Say OK." });
const sessionId = session.sessionId;
await session.disconnect();
const lastId = await client.getLastSessionId();
expect(lastId).toBe(sessionId);
});
it("should get null foreground session id in headless mode", async () => {
await client.start();
const sessionId = await client.getForegroundSessionId();
expect(sessionId).toBeFalsy();
});
it("should report error when setting foreground session in headless mode", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
await assertFailure(
() => client.setForegroundSessionId(session.sessionId),
"Not running in TUI+server mode"
);
await session.disconnect();
});
});