Files
t3code/apps/server/src/provider/OpenCodeServerOwner.test.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

290 lines
11 KiB
TypeScript

import { it } from "@effect/vitest";
import * as Deferred from "effect/Deferred";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Ref from "effect/Ref";
import * as TestClock from "effect/testing/TestClock";
import { expect } from "vite-plus/test";
import * as OpenCodeRuntime from "./opencodeRuntime.ts";
import * as OpenCodeServerOwner from "./OpenCodeServerOwner.ts";
const unusedRuntimeMethod = () =>
Effect.fail(
new OpenCodeRuntime.OpenCodeRuntimeError({
operation: "unused",
detail: "unused test method",
}),
);
const makeRuntime = Effect.gen(function* () {
const starts = yield* Ref.make(0);
const closes = yield* Ref.make(0);
const failNextStart = yield* Ref.make(false);
const started = yield* Deferred.make<void>();
const closed = yield* Deferred.make<void>();
const runtime: OpenCodeRuntime.OpenCodeRuntimeShape = {
startOpenCodeServerProcess: () =>
Effect.gen(function* () {
if (yield* Ref.getAndSet(failNextStart, false)) {
return yield* new OpenCodeRuntime.OpenCodeRuntimeError({
operation: "startOpenCodeServerProcess",
detail: "start failed",
});
}
const index = yield* Ref.updateAndGet(starts, (count) => count + 1);
yield* Deferred.succeed(started, undefined).pipe(Effect.ignore);
yield* Effect.addFinalizer(() =>
Ref.update(closes, (count) => count + 1).pipe(
Effect.andThen(Deferred.succeed(closed, undefined)),
Effect.ignore,
),
);
return {
url: `http://127.0.0.1:${index}`,
version: "1.14.19",
isRunning: Effect.succeed(true),
exitCode: Effect.never,
};
}),
connectToOpenCodeServer: unusedRuntimeMethod,
runOpenCodeCommand: unusedRuntimeMethod,
createOpenCodeSdkClient: () => ({}) as never,
loadOpenCodeInventory: unusedRuntimeMethod,
loadOpenCodeSkills: unusedRuntimeMethod,
loadInventoryFromCli: unusedRuntimeMethod,
loadSkillsFromCli: unusedRuntimeMethod,
};
return { runtime, starts, closes, failNextStart, started, closed };
});
it.effect("shares concurrent borrowers and closes after the idle TTL", () =>
Effect.gen(function* () {
const testRuntime = yield* makeRuntime;
const release = yield* Deferred.make<void>();
yield* Effect.scoped(
Effect.gen(function* () {
const owner = yield* OpenCodeServerOwner.make({
binaryPath: "opencode",
directory: "/project",
});
const useServer = owner.withServer((server) =>
Deferred.await(release).pipe(Effect.as(server.url)),
);
const fibers = yield* Effect.all([useServer, useServer], {
concurrency: "unbounded",
}).pipe(Effect.forkChild);
yield* Deferred.await(testRuntime.started);
expect(yield* Ref.get(testRuntime.starts)).toBe(1);
yield* Deferred.succeed(release, undefined);
expect(yield* Fiber.join(fibers)).toEqual(["http://127.0.0.1:1", "http://127.0.0.1:1"]);
yield* TestClock.adjust(Duration.seconds(31));
yield* Deferred.await(testRuntime.closed);
expect(yield* Ref.get(testRuntime.closes)).toBe(1);
}),
).pipe(Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, testRuntime.runtime));
}).pipe(Effect.provide(TestClock.layer())),
);
it.effect("retries a failed start and closes on owner scope shutdown", () =>
Effect.gen(function* () {
const testRuntime = yield* makeRuntime;
yield* Ref.set(testRuntime.failNextStart, true);
yield* Effect.scoped(
Effect.gen(function* () {
const owner = yield* OpenCodeServerOwner.make({
binaryPath: "opencode",
directory: "/project",
});
expect(
(yield* Effect.exit(owner.withServer((server) => Effect.succeed(server.url))))._tag,
).toBe("Failure");
expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
"http://127.0.0.1:1",
);
}),
).pipe(Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, testRuntime.runtime));
expect(yield* Ref.get(testRuntime.starts)).toBe(1);
expect(yield* Ref.get(testRuntime.closes)).toBe(1);
}),
);
it.effect("invalidates an exited process so the next borrower starts a new one", () =>
Effect.gen(function* () {
const starts = yield* Ref.make(0);
const processExits: Array<Deferred.Deferred<number>> = [];
const processClosed = yield* Deferred.make<void>();
const runtime: OpenCodeRuntime.OpenCodeRuntimeShape = {
startOpenCodeServerProcess: () =>
Effect.gen(function* () {
const index = yield* Ref.updateAndGet(starts, (count) => count + 1);
const exitCode = yield* Deferred.make<number>();
processExits.push(exitCode);
yield* Effect.addFinalizer(() =>
Deferred.succeed(processClosed, undefined).pipe(Effect.ignore),
);
return {
url: `http://127.0.0.1:${index}`,
version: "1.14.19",
isRunning: Effect.succeed(true),
exitCode: Deferred.await(exitCode),
};
}),
connectToOpenCodeServer: unusedRuntimeMethod,
runOpenCodeCommand: unusedRuntimeMethod,
createOpenCodeSdkClient: () => ({}) as never,
loadOpenCodeInventory: unusedRuntimeMethod,
loadOpenCodeSkills: unusedRuntimeMethod,
loadInventoryFromCli: unusedRuntimeMethod,
loadSkillsFromCli: unusedRuntimeMethod,
};
yield* Effect.scoped(
Effect.gen(function* () {
const owner = yield* OpenCodeServerOwner.make({
binaryPath: "opencode",
directory: "/project",
});
expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
"http://127.0.0.1:1",
);
yield* Deferred.succeed(processExits[0]!, 1);
yield* Deferred.await(processClosed);
expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
"http://127.0.0.1:2",
);
}),
).pipe(Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, runtime));
expect(yield* Ref.get(starts)).toBe(2);
}),
);
it.effect("replaces a dead cached process before its exit watcher runs", () =>
Effect.gen(function* () {
const starts = yield* Ref.make(0);
const closes = yield* Ref.make(0);
const processRunning: Array<Ref.Ref<boolean>> = [];
const runtime: OpenCodeRuntime.OpenCodeRuntimeShape = {
startOpenCodeServerProcess: () =>
Effect.gen(function* () {
const index = yield* Ref.updateAndGet(starts, (count) => count + 1);
const isRunning = yield* Ref.make(true);
processRunning.push(isRunning);
yield* Effect.addFinalizer(() => Ref.update(closes, (count) => count + 1));
return {
url: `http://127.0.0.1:${index}`,
version: "1.14.19",
isRunning: Ref.get(isRunning),
exitCode: Effect.never,
};
}),
connectToOpenCodeServer: unusedRuntimeMethod,
runOpenCodeCommand: unusedRuntimeMethod,
createOpenCodeSdkClient: () => ({}) as never,
loadOpenCodeInventory: unusedRuntimeMethod,
loadOpenCodeSkills: unusedRuntimeMethod,
loadInventoryFromCli: unusedRuntimeMethod,
loadSkillsFromCli: unusedRuntimeMethod,
};
yield* Effect.scoped(
Effect.gen(function* () {
const owner = yield* OpenCodeServerOwner.make({
binaryPath: "opencode",
directory: "/project",
});
expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
"http://127.0.0.1:1",
);
yield* Ref.set(processRunning[0]!, false);
expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
"http://127.0.0.1:2",
);
expect(yield* Ref.get(starts)).toBe(2);
expect(yield* Ref.get(closes)).toBe(1);
}),
).pipe(Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, runtime));
}),
);
it.effect("cleans up an interrupted startup and allows a retry", () =>
Effect.gen(function* () {
const starts = yield* Ref.make(0);
const firstStartEntered = yield* Deferred.make<void>();
const firstStartClosed = yield* Deferred.make<void>();
const runtime: OpenCodeRuntime.OpenCodeRuntimeShape = {
startOpenCodeServerProcess: () =>
Effect.gen(function* () {
const index = yield* Ref.updateAndGet(starts, (count) => count + 1);
yield* Effect.addFinalizer(() =>
index === 1
? Deferred.succeed(firstStartClosed, undefined).pipe(Effect.ignore)
: Effect.void,
);
if (index === 1) {
yield* Deferred.succeed(firstStartEntered, undefined);
return yield* Effect.never;
}
return {
url: `http://127.0.0.1:${index}`,
version: "1.14.19",
isRunning: Effect.succeed(true),
exitCode: Effect.never,
};
}),
connectToOpenCodeServer: unusedRuntimeMethod,
runOpenCodeCommand: unusedRuntimeMethod,
createOpenCodeSdkClient: () => ({}) as never,
loadOpenCodeInventory: unusedRuntimeMethod,
loadOpenCodeSkills: unusedRuntimeMethod,
loadInventoryFromCli: unusedRuntimeMethod,
loadSkillsFromCli: unusedRuntimeMethod,
};
yield* Effect.scoped(
Effect.gen(function* () {
const owner = yield* OpenCodeServerOwner.make({
binaryPath: "opencode",
directory: "/project",
});
const firstBorrower = yield* owner
.withServer((server) => Effect.succeed(server.url))
.pipe(Effect.forkChild);
yield* Deferred.await(firstStartEntered);
yield* Fiber.interrupt(firstBorrower);
yield* Deferred.await(firstStartClosed);
expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
"http://127.0.0.1:2",
);
}),
).pipe(Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, runtime));
}),
);
it.effect("releases an interrupted borrower and closes after the idle TTL", () =>
Effect.gen(function* () {
const testRuntime = yield* makeRuntime;
const borrowerEntered = yield* Deferred.make<void>();
yield* Effect.scoped(
Effect.gen(function* () {
const owner = yield* OpenCodeServerOwner.make({
binaryPath: "opencode",
directory: "/project",
});
const borrower = yield* owner
.withServer(() =>
Deferred.succeed(borrowerEntered, undefined).pipe(Effect.andThen(Effect.never)),
)
.pipe(Effect.forkChild);
yield* Deferred.await(borrowerEntered);
yield* Fiber.interrupt(borrower);
yield* TestClock.adjust(Duration.seconds(31));
yield* Deferred.await(testRuntime.closed);
expect(yield* Ref.get(testRuntime.closes)).toBe(1);
}),
).pipe(Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, testRuntime.runtime));
}).pipe(Effect.provide(TestClock.layer())),
);