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

DevinDriver.test.ts

128 lines4.6 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import * as NodeFSP from "node:fs/promises";
6import { expect, it } from "@effect/vitest";
7import { ProviderInstanceId } from "@t3tools/contracts";
8import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
9import * as Effect from "effect/Effect";
10import * as Exit from "effect/Exit";
11import * as Layer from "effect/Layer";
12import * as Schema from "effect/Schema";
13import { ChildProcessSpawner } from "effect/unstable/process";
14​
15import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
16import { ServerConfig } from "../../config.ts";
17import { ServerSettingsService } from "../../serverSettings.ts";
18import { NoOpProviderEventLoggers, ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
19import { DevinDriver } from "./DevinDriver.ts";
20​
21const encodeUnknownJson = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown));
22​
23const testLayer = ServerConfig.layerTest(process.cwd(), {
24 prefix: "t3-devin-driver-skills-",
25}).pipe(
26 Layer.provideMerge(NodeServices.layer),
27 Layer.provideMerge(ServerSettingsService.layerTest()),
28 Layer.provideMerge(Layer.succeed(ProviderEventLoggers, NoOpProviderEventLoggers)),
29 Layer.provideMerge(
30 Layer.mock(BackgroundPolicy.BackgroundPolicy)({
31 shouldRunScopeWork: () => Effect.succeed(false),
32 }),
33 ),
34);
35​
36// The `#!/bin/sh` stub below cannot be resolved as an executable on Windows.
37const windowsHost = HostProcessPlatform.defaultValue() === "win32";
38​
39const makeSkillsBinary = Effect.fn("makeDevinSkillsBinary")(function* (options: {
40 readonly skillsJson: string;
41 readonly skillsExitCode?: number;
42}) {
43 const dir = yield* Effect.promise(() =>
44 NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "devin-driver-skills-")),
45 );
46 const binaryPath = NodePath.join(dir, "fake-devin.sh");
47 const skillsJsonPath = NodePath.join(dir, "skills.json");
48 yield* Effect.promise(() => NodeFSP.writeFile(skillsJsonPath, options.skillsJson, "utf8"));
49 const script = `#!/bin/sh
50if [ "$1" = "--version" ]; then
51 echo "devin 1.0.0"
52 exit 0
53fi
54if [ "$1" = "skills" ]; then
55 cat '${skillsJsonPath}'
56 exit ${options.skillsExitCode ?? 0}
57fi
58echo "unexpected command: $*" >&2
59exit 1
60`;
61 yield* Effect.promise(() => NodeFSP.writeFile(binaryPath, script, "utf8"));
62 yield* Effect.promise(() => NodeFSP.chmod(binaryPath, 0o755));
63 return binaryPath;
64});
65​
66const createInstance = (binaryPath: string, enabled: boolean) =>
67 DevinDriver.create({
68 instanceId: ProviderInstanceId.make("devin-skills-test"),
69 displayName: "Devin skills test",
70 enabled,
71 environment: [],
72 config: { ...DevinDriver.defaultConfig(), binaryPath, enabled },
73 });
74​
75it.layer(testLayer)("DevinDriver snapshotForCwd", (it) => {
76 it.effect.skipIf(windowsHost)(
77 "returns the normal snapshot without discovery when Devin is disabled",
78 () =>
79 Effect.gen(function* () {
80 const noSpawn = Layer.succeed(
81 ChildProcessSpawner.ChildProcessSpawner,
82 ChildProcessSpawner.make(() =>
83 Effect.die("Disabled Devin must not spawn a skills process"),
84 ),
85 );
86 const instance = yield* createInstance("devin-unused", false).pipe(Effect.provide(noSpawn));
87 const snapshot = yield* instance.snapshotForCwd!("/workspace");
88​
89 expect(snapshot.skills).toEqual([]);
90 expect(snapshot.enabled).toBe(false);
91 }),
92 );
93​
94 it.effect.skipIf(windowsHost)("includes discovered skills in the workspace snapshot", () =>
95 Effect.gen(function* () {
96 const binaryPath = yield* makeSkillsBinary({
97 skillsJson: encodeUnknownJson([
98 {
99 name: "deploy",
100 base_dir: "/tmp/skills/deploy",
101 description: "Deploy the app.",
102 triggers: ["user", "model"],
103 },
104 ]),
105 });
106 const instance = yield* createInstance(binaryPath, true);
107 const snapshot = yield* instance.snapshotForCwd!("/workspace");
108​
109 expect(snapshot.skills).toHaveLength(1);
110 expect(snapshot.skills[0]).toMatchObject({ name: "deploy", enabled: true });
111 }),
112 );
113​
114 it.effect.skipIf(windowsHost)(
115 "fails with a typed error so the registry keeps the last valid snapshot",
116 () =>
117 Effect.gen(function* () {
118 const binaryPath = yield* makeSkillsBinary({
119 skillsJson: "[]",
120 skillsExitCode: 3,
121 });
122 const instance = yield* createInstance(binaryPath, true);
123 const exit = yield* Effect.exit(instance.snapshotForCwd!("/workspace"));
124​
125 expect(Exit.isFailure(exit)).toBe(true);
126 }),
127 );
128});