Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OpenCodeRuntime.environment.test.ts

82 lines2.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Path from "effect/Path";
7​
8import * as OpenCodeRuntime from "./OpenCodeRuntime.ts";
9import * as OpenCodeServerLedger from "./OpenCodeServerLedger.ts";
10​
11describe("resolveOpenCodeServerPassword", () => {
12 it("uses the settings password when local settings and environment differ", () => {
13 expect(
14 OpenCodeRuntime.resolveOpenCodeServerPassword(
15 {
16 external: false,
17 serverPassword: "settings-password",
18 environment: { OPENCODE_SERVER_PASSWORD: "environment-password" },
19 },
20 {},
21 ),
22 ).toBe("settings-password");
23 });
24​
25 it("does not send an inherited local password to an external server", () => {
26 expect(
27 OpenCodeRuntime.resolveOpenCodeServerPassword(
28 { external: true, environment: { OPENCODE_SERVER_PASSWORD: "local-secret" } },
29 { OPENCODE_SERVER_PASSWORD: "inherited-secret" },
30 ),
31 ).toBeUndefined();
32 });
33});
34​
35describe("startOpenCodeServerProcess", () => {
36 it.effect("gives a server started without a configured password a generated one", () =>
37 Effect.gen(function* () {
38 const fs = yield* FileSystem.FileSystem;
39 const path = yield* Path.Path;
40 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-password-" });
41 const binaryPath = path.join(tempDir, "opencode");
42 const receivedPath = path.join(tempDir, "received");
43 yield* fs.writeFileString(
44 binaryPath,
45 [
46 "#!/bin/sh",
47 `printf %s "$OPENCODE_SERVER_PASSWORD" > "${receivedPath}"`,
48 'echo "opencode server listening on http://127.0.0.1:4096"',
49 "",
50 ].join("\n"),
51 );
52 yield* fs.chmod(binaryPath, 0o755);
53​
54 const server = yield* OpenCodeRuntime.OpenCodeRuntime.pipe(
55 Effect.flatMap((runtime) =>
56 runtime.startOpenCodeServerProcess({
57 binaryPath,
58 directory: tempDir,
59 port: 4096,
60 environment: { PATH: process.env.PATH, OPENCODE_SERVER_PASSWORD: "" },
61 verify: () => Effect.succeed("1.0.0"),
62 }),
63 ),
64 Effect.provide(
65 OpenCodeRuntime.layer.pipe(
66 Layer.provide(
67 Layer.succeed(
68 OpenCodeServerLedger.OpenCodeServerLedger,
69 OpenCodeServerLedger.OpenCodeServerLedger.of({
70 track: () => Effect.succeed(Effect.void),
71 }),
72 ),
73 ),
74 ),
75 ),
76 );
77​
78 expect(server.serverPassword).toMatch(/^[\w-]{43}$/);
79 expect(yield* fs.readFileString(receivedPath)).toBe(server.serverPassword);
80 }).pipe(Effect.provide(NodeServices.layer)),
81 );
82});