-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathhost.test.ts
More file actions
100 lines (88 loc) · 4.04 KB
/
Copy pathhost.test.ts
File metadata and controls
100 lines (88 loc) · 4.04 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
92
93
94
95
96
97
98
99
100
import { describe, expect, expectTypeOf, it, vi } from "vitest";
import { AhpHost } from "../src/index.js";
import type {
HostExitedNotification,
HostStartRequest,
HostStartResult,
HostLocalServerOptions,
HostGitHubEnvironmentOptions,
} from "../src/generated/rpc.js";
const info = {
hostId: "host-1",
url: "ws://127.0.0.1:12345",
token: "test-only-token",
};
describe("AhpHost", () => {
it("keeps generated listener options optional but nonnullable", () => {
expectTypeOf<HostStartRequest>().toEqualTypeOf<{
hostId: string;
localServer?: HostLocalServerOptions;
githubEnvironment?: HostGitHubEnvironmentOptions;
sessionFactory?: boolean;
resumeFactory?: boolean;
}>();
expectTypeOf<HostStartResult["token"]>().toEqualTypeOf<string | undefined>();
expectTypeOf<AhpHost["token"]>().toEqualTypeOf<string | undefined>();
expectTypeOf<HostStartResult["pid"]>().toEqualTypeOf<number | undefined>();
expectTypeOf<AhpHost["pid"]>().toEqualTypeOf<number | undefined>();
expectTypeOf<AhpHost["url"]>().toEqualTypeOf<string | undefined>();
expectTypeOf<AhpHost["environmentId"]>().toEqualTypeOf<string | undefined>();
expectTypeOf<HostExitedNotification["exitCode"]>().toEqualTypeOf<
number | null | undefined
>();
expectTypeOf<HostExitedNotification["error"]>().toEqualTypeOf<string | null | undefined>();
});
it("exposes connection information without starting a process or exposing closed", () => {
const dispose = vi.fn();
const host = new AhpHost(info, dispose, vi.fn());
expect(host.hostId).toBe(info.hostId);
expect(host.url).toBe(info.url);
expect(host.token).toBe(info.token);
expect(host.pid).toBeUndefined();
expect(host).not.toHaveProperty("closed");
expect(dispose).not.toHaveBeenCalled();
});
it("preserves an optional legacy separate host process ID", () => {
const host = new AhpHost({ ...info, pid: 1234 }, vi.fn(), vi.fn());
expect(host.pid).toBe(1234);
});
it("supports GitHub-only hosts without a local URL, token, or process", () => {
const host = new AhpHost(
{ hostId: "remote", environmentId: "environment-1" },
vi.fn(),
vi.fn()
);
expect(host.environmentId).toBe("environment-1");
expect(host.url).toBeUndefined();
expect(host.token).toBeUndefined();
expect(host.pid).toBeUndefined();
});
it("supports a listener without a connection token", () => {
const { token: _token, ...withoutToken } = info;
const host = new AhpHost(withoutToken, vi.fn(), vi.fn());
expect(host.token).toBeUndefined();
});
it("forwards concurrent, repeated, and async disposal calls independently", async () => {
const dispose = vi.fn(async () => {});
const host = new AhpHost(info, dispose, vi.fn());
await Promise.all([host.dispose(), host.dispose()]);
await host.dispose();
await host[Symbol.asyncDispose]();
expect(dispose).toHaveBeenCalledTimes(4);
});
it("returns the disposal promise without handling or retrying failures", async () => {
const failure = Promise.reject(new Error("connection write failed"));
const dispose = vi.fn(() => failure);
const host = new AhpHost(info, dispose, vi.fn());
expect(host.dispose()).toBe(failure);
await expect(failure).rejects.toThrow("connection write failed");
expect(dispose).toHaveBeenCalledOnce();
});
it("forwards existing session publication without creating or copying a session", async () => {
const result = { sessionId: "resident", sessionUri: "copilot:/resident" };
const publish = vi.fn(async () => result);
const host = new AhpHost(info, vi.fn(), publish);
await expect(host.publishSession("resident")).resolves.toBe(result);
expect(publish).toHaveBeenCalledExactlyOnceWith("resident");
});
});