Repository navigation
Expand file tree
/
Copy patht3.ts
More file actions
296 lines (276 loc) · 11.8 KB
/
Copy patht3.ts
File metadata and controls
296 lines (276 loc) · 11.8 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// `test` for web scenarios: e2e's web test plus `t3`, which seeds state and opens the signed-in
// app. `t3-mobile.ts` is the same for the iOS app.
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as NodeWS from "@effect/platform-node/NodeWS";
import { test as base } from "@e2e-dev/web";
import { expect } from "e2e";
import {
CommandId,
EnvironmentHttpApi,
ORCHESTRATION_PROTOCOL_VERSION,
type ProjectMutation,
ProjectId,
WsRpcGroup,
} from "@t3tools/contracts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as HttpApiClient from "effect/http-api/HttpApiClient";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as Schedule from "effect/Schedule";
import * as Schema from "effect/Schema";
import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
import { RpcClient, RpcSerialization } from "effect/rpc";
import { Socket } from "effect/socket";
import { APP_DIR, T3_HOME } from "../e2e.config.ts";
import { run, ScenarioError } from "./shell.ts";
export interface T3State {
/** Creates a git repository holding `files` and adds it as a project. Returns its path. */
readonly project: (
title: string,
files?: Readonly<Record<string, string | Uint8Array>>,
) => Promise<string>;
/**
* Starts a thread in a project and plays one replayed turn per message before returning.
* `model` is a dev replay recording: showcase, todo-list, approval, question, multi-turn,
* command-output.
*/
readonly thread: (input: {
readonly project: string;
readonly title: string;
readonly messages: ReadonlyArray<string>;
readonly model?: string;
}) => Promise<T3Thread>;
}
export interface T3Thread {
readonly threadId: string;
}
export interface T3 extends T3State {
/**
* Opens the app, signed in, at a path or on a thread `t3.thread` returned. Opening the thread
* by its link is the reliable way to reach it: a sidebar row covers its own title text.
*/
readonly open: (target?: string | T3Thread) => Promise<void>;
}
/** e2e's fixtures are promise functions; this is the one place an Effect becomes a promise. */
export const runPromise = <A, E>(effect: Effect.Effect<A, E, NodeServices.NodeServices>) =>
Effect.runPromise(effect.pipe(Effect.provide(NodeServices.layer)));
const decodeLaunched = Schema.decodeUnknownEffect(
Schema.fromJsonString(Schema.Struct({ threadId: Schema.String })),
);
const decodeRuntimeState = Schema.decodeUnknownEffect(
Schema.fromJsonString(Schema.Struct({ origin: Schema.String, startedAt: Schema.String })),
);
type ServerApi = {
readonly client: HttpApiClient.ForApi<typeof EnvironmentHttpApi>;
readonly headers: { readonly authorization: string };
};
/**
* One API client per running server, signed in once. Each CLI call starts a process that takes
* about 0.7 s before it does anything, so per-test state goes through the server's HTTP API. A
* server started again on the same home, as the second server is for each test, gets a new one.
*/
const serverApis = new Map<string, Promise<ServerApi>>();
const uuid = SecureRandom.SecureRandom.pipe(
Effect.flatMap((random) => random.uuidV4()),
Effect.provide(NodeCrypto.layer),
);
/** State for the server on `home`, made through the app's own CLI and API: real events, not inserted rows. */
export function makeState(home: string, env: NodeJS.ProcessEnv = {}) {
const cli = Effect.fn("scenario.cli")(function* (
args: ReadonlyArray<string>,
allowExit: ReadonlyArray<number> = [0],
) {
const hostEnvironment = yield* HostProcess.Environment;
return yield* run(
yield* HostProcess.ExecutablePath,
["apps/server/src/bin.ts", ...args, "--base-dir", home],
{
cwd: APP_DIR,
env: { PATH: hostEnvironment.PATH, HOME: hostEnvironment.HOME, ...env },
allowExit,
},
);
});
// The server writes its origin beside its database once it listens. It also prints its pairing
// token, which can come first, so a missing file is read again.
const runtimeState = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
return yield* decodeRuntimeState(
yield* fs.readFileString(path.join(home, "userdata", "server-runtime.json")),
);
}).pipe(
Effect.retry({
schedule: Schedule.spaced("50 millis"),
times: 100,
while: (error) => error._tag === "PlatformError" && error.reason._tag === "NotFound",
}),
);
const connect = Effect.fn("scenario.connect")(function* (origin: string) {
const issued = yield* cli(["auth", "session", "issue", "--token-only", "--label", "scenario"]);
const client = yield* HttpApiClient.make(EnvironmentHttpApi, { baseUrl: origin });
return { client, headers: { authorization: `Bearer ${issued.stdout.trim()}` } };
}, Effect.provide(FetchHttpClient.layer));
const api = Effect.gen(function* () {
const { origin, startedAt } = yield* runtimeState;
const key = `${home}\n${origin}\n${startedAt}`;
return yield* Effect.tryPromise({
try: () => {
const known = serverApis.get(key);
if (known !== undefined) return known;
const connecting = runPromise(connect(origin));
serverApis.set(key, connecting);
// A failed sign-in is not kept: the next test tries again.
connecting.catch(() => serverApis.delete(key));
return connecting;
},
catch: (cause) => new ScenarioError({ message: `Could not reach the server API: ${cause}` }),
});
});
const mutate = Effect.fn("scenario.mutate")(function* (mutation: ProjectMutation) {
const { client, headers } = yield* api;
return yield* client.projects
.mutate({ headers, payload: mutation })
.pipe(
Effect.mapError((cause) => new ScenarioError({ message: `${mutation.type}: ${cause}` })),
);
});
const projects: Array<ProjectId> = [];
const project = Effect.fn("scenario.project")(function* (
title: string,
files: Readonly<Record<string, string | Uint8Array>>,
) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
// Its own folder per test: the server and this home outlive one test.
yield* fs.makeDirectory(path.join(home, "workspaces"), { recursive: true });
const parent = yield* fs.makeTempDirectory({
directory: path.join(home, "workspaces"),
prefix: "w-",
});
const workspace = path.join(parent, title);
for (const [file, content] of Object.entries({ "README.md": "", ...files })) {
yield* fs.makeDirectory(path.dirname(path.join(workspace, file)), { recursive: true });
yield* typeof content === "string"
? fs.writeFileString(path.join(workspace, file), content)
: fs.writeFile(path.join(workspace, file), content);
}
const git = (args: ReadonlyArray<string>) => run("git", args, { cwd: workspace });
const author = ["-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid"];
yield* git(["init", "--quiet", "--initial-branch=main"]);
yield* git(["add", "--all"]);
yield* git([...author, "commit", "--quiet", "--message", "Initial commit"]);
const created = yield* mutate({
type: "project.create",
commandId: CommandId.make(yield* uuid),
projectId: ProjectId.make(yield* uuid),
title,
workspaceRoot: workspace,
});
projects.push(created.id);
return workspace;
});
const thread = Effect.fn("scenario.thread")(function* (input: {
readonly project: string;
readonly title: string;
readonly messages: ReadonlyArray<string>;
readonly model?: string;
}) {
// Exit code 2 means the turn waits for an answer, which a scenario may want to start from.
const turn = (args: ReadonlyArray<string>) =>
cli(["thread", ...args, "--wait", "--json"], [0, 2]);
const [first = "Start", ...rest] = input.messages;
const replay = ["--provider", "devReplay", "--model", input.model ?? "multi-turn"];
const started = yield* turn(["start", input.project, first, "--title", input.title, ...replay]);
const { threadId } = yield* decodeLaunched(started.stdout);
for (const message of rest) yield* turn(["send", threadId, message]);
return { threadId } satisfies T3Thread;
});
return {
cli,
state: {
project: (title, files = {}) => runPromise(project(title, files)),
thread: (input) => runPromise(thread(input)),
} satisfies T3State,
// The server outlives the test, so the next one must not see this one's projects.
cleanup: Effect.forEach(
projects,
(projectId) =>
Effect.flatMap(uuid, (commandId) =>
mutate({
type: "project.delete",
commandId: CommandId.make(commandId),
projectId,
force: true,
}),
),
{ discard: true },
),
};
}
const readPairingPath = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
return yield* fs.readFileString(path.join(T3_HOME, "pairing-path"));
});
/** The id every thread link starts with, asked of the server the app talks to. */
export const environmentId = (baseUrl: string) =>
HttpClient.get(`${baseUrl}/.well-known/t3/environment`).pipe(
Effect.flatMap(HttpClientResponse.filterStatusOk),
Effect.flatMap(
HttpClientResponse.schemaBodyJson(Schema.Struct({ environmentId: Schema.String })),
),
Effect.map((environment) => environment.environmentId),
Effect.provide(FetchHttpClient.layer),
);
/** The typed RPC client of the server on `home` at `origin`, signed in through its own CLI. */
export const rpcClient = Effect.fn("scenario.rpcClient")(function* (home: string, origin: string) {
const issued = yield* makeState(home).cli(["auth", "session", "issue", "--token-only"]);
const { ticket } = yield* HttpClient.execute(
HttpClientRequest.post(`${origin}/api/auth/websocket-ticket`).pipe(
HttpClientRequest.bearerToken(issued.stdout.trim()),
),
).pipe(
Effect.flatMap(HttpClientResponse.filterStatusOk),
Effect.flatMap(HttpClientResponse.schemaBodyJson(Schema.Struct({ ticket: Schema.String }))),
Effect.provide(FetchHttpClient.layer),
);
// Built into the caller's scope: `Effect.provide` would close the socket once `make` returns.
const protocol = yield* Layer.build(
RpcClient.layerProtocolSocket().pipe(
Layer.provide(
Socket.layerWebSocket(
`${origin.replace(/^http/, "ws")}/ws?orchestrationProtocol=${ORCHESTRATION_PROTOCOL_VERSION}&wsTicket=${encodeURIComponent(ticket)}`,
),
),
Layer.provide(RpcSerialization.layerJson),
Layer.provide(NodeWS.layerWebSocketConstructor),
),
);
return yield* RpcClient.make(WsRpcGroup).pipe(Effect.provideContext(protocol));
});
export const test = base.extend<{ t3: T3 }>({
t3: async ({ app, browser }, use) => {
const { state, cleanup } = makeState(T3_HOME);
await use({
...state,
open: async (target = "/") => {
await app.open(await runPromise(readPairingPath));
// The pairing page signs in and then leaves itself. Going anywhere before it has done
// so cancels the sign-in, and the next page is the pairing page again.
await expect(browser).not.toHaveURL(/\/pair(?:[?#]|$)/, { timeout: 60_000 });
if (typeof target !== "string") {
const environment = await runPromise(environmentId(app.baseUrl ?? ""));
await app.open(`/${environment}/${target.threadId}`);
} else if (target !== "/") await app.open(target);
},
});
await runPromise(cleanup);
},
});
/** Test option for a web scenario: the iOS target skips it. */
export const WEB = { platforms: ["web"] } as const;