Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { assert, describe, it } from "@effect/vitest";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as NetService from "@akeru/shared/Net";
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 Layer from "effect/Layer";
import * as Result from "effect/Result";
import * as Stream from "effect/Stream";
import { TestClock } from "effect/testing";
import { HttpClient } from "effect/unstable/http";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
import { SshPasswordPrompt } from "./auth.ts";
import { SshCommandError } from "./errors.ts";
import { SshEnvironmentManager } from "./tunnel.ts";
import {
makeSuccessfulProcess,
makeRunningProcess,
testHttpClient,
testNetService,
commandArgs,
} from "./tunnel.test-support.ts";
describe("ssh tunnel scripts", () => {
it.effect.each(["successful stop", "failed stop"] as const)(
"closes the tunnel scope and starts fresh after a %s",
(mode) => {
const spawnedCommands: Array<ReadonlyArray<string>> = [];
let tunnelKillCount = 0;
let stopCommandCount = 0;
const spawner = ChildProcessSpawner.make((command) =>
Effect.sync(() => {
const args = commandArgs(command);
spawnedCommands.push(args);
if (args.includes("-N")) {
return makeRunningProcess(() => {
tunnelKillCount += 1;
});
}
if (args.includes("sh") && args.includes("--")) {
return makeSuccessfulProcess('{"remotePort":3773}\n');
}
if (args.includes("sh")) {
stopCommandCount += 1;
if (mode === "failed stop" && stopCommandCount === 1) {
return {
...makeSuccessfulProcess(""),
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)),
stderr: Stream.make(
new TextEncoder().encode("Remote Akeru server did not stop within 2 seconds.\n"),
),
};
}
return makeSuccessfulProcess('{"stopped":true}\n');
}
return makeSuccessfulProcess("\n");
}),
);
const layer = Layer.mergeAll(
NodeServices.layer,
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
Layer.succeed(HttpClient.HttpClient, testHttpClient),
Layer.succeed(NetService.NetService, testNetService),
SshPasswordPrompt.disabledLayer,
SshEnvironmentManager.layer(),
);
const target = {
alias: "devbox",
hostname: "devbox.example.com",
username: "julius",
port: 2222,
} as const;
return Effect.gen(function* () {
const manager = yield* SshEnvironmentManager;
const first = yield* manager.ensureEnvironment(target);
assert.equal(first.httpBaseUrl, "http://127.0.0.1:41773/");
const firstTunnelArgs = spawnedCommands.find((args) => args.includes("-N"));
assert.isDefined(firstTunnelArgs);
assert.include(firstTunnelArgs, "ControlMaster=no");
assert.include(firstTunnelArgs, "ControlPath=none");
assert.include(firstTunnelArgs, "ControlPersist=no");
const disconnected = yield* Effect.result(manager.disconnectEnvironment(target));
if (mode === "failed stop") {
assert.isTrue(Result.isFailure(disconnected));
if (Result.isFailure(disconnected)) {
assert.instanceOf(disconnected.failure, SshCommandError);
assert.equal(
disconnected.failure.message,
"Remote Akeru server did not stop within 2 seconds.",
);
}
} else {
assert.isTrue(Result.isSuccess(disconnected));
}
assert.equal(tunnelKillCount, 1);
assert.equal(stopCommandCount, 1);
if (mode === "failed stop") {
yield* manager.disconnectEnvironment(target);
assert.equal(tunnelKillCount, 1);
assert.equal(stopCommandCount, 2);
}
yield* manager.ensureEnvironment(target);
assert.equal(spawnedCommands.filter((args) => args.includes("-N")).length, 2);
assert.equal(tunnelKillCount, 1);
}).pipe(
Effect.provide(layer),
Effect.scoped,
Effect.andThen(
Effect.sync(() => {
assert.equal(tunnelKillCount, 2);
assert.equal(stopCommandCount, mode === "failed stop" ? 3 : 2);
}),
),
);
},
);
it.effect.each(["local tunnel", "remote server"] as const)(
"waits for %s shutdown before reconnecting the same target",
(stalledStep) =>
Effect.gen(function* () {
const shutdownStarted = yield* Deferred.make<void>();
const finishShutdown = yield* Deferred.make<void>();
const pauseShutdown = Deferred.succeed(shutdownStarted, undefined).pipe(
Effect.andThen(Deferred.await(finishShutdown)),
);
let launches = 0;
let tunnels = 0;
let stops = 0;
let remoteRunning = false;
const target = { alias: "devbox", hostname: "devbox", username: null, port: null };
const spawner = ChildProcessSpawner.make((command) =>
Effect.sync(() => {
const args = commandArgs(command);
const isTarget = args.includes(target.alias);
if (args.includes("-G")) {
return makeSuccessfulProcess("");
}
if (args.includes("-N")) {
const tunnel = makeRunningProcess(() => undefined);
if (isTarget && ++tunnels === 1 && stalledStep === "local tunnel") {
return {
...tunnel,
kill: (options?: ChildProcess.KillOptions) =>
pauseShutdown.pipe(Effect.andThen(tunnel.kill(options))),
};
}
return tunnel;
}
if (args.includes("--")) {
if (isTarget) {
launches += 1;
remoteRunning = true;
}
return makeSuccessfulProcess('{"remotePort":3773}\n');
}
const stop = makeSuccessfulProcess('{"stopped":true}\n');
if (!isTarget) return stop;
const pause = ++stops === 1 && stalledStep === "remote server";
return {
...stop,
exitCode: (pause ? pauseShutdown : Effect.void).pipe(
Effect.andThen(
Effect.sync(() => {
remoteRunning = false;
return ChildProcessSpawner.ExitCode(0);
}),
),
),
};
}),
);
const layer = Layer.mergeAll(
NodeServices.layer,
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
Layer.succeed(HttpClient.HttpClient, testHttpClient),
Layer.succeed(NetService.NetService, testNetService),
SshPasswordPrompt.disabledLayer,
SshEnvironmentManager.layer(),
);
yield* Effect.gen(function* () {
const manager = yield* SshEnvironmentManager;
yield* manager.ensureEnvironment(target);
const disconnect = yield* Effect.forkChild(manager.disconnectEnvironment(target));
yield* Deferred.await(shutdownStarted);
const firstReconnect = yield* Effect.forkChild(manager.ensureEnvironment(target));
const secondReconnect = yield* Effect.forkChild(manager.ensureEnvironment(target));
yield* manager.ensureEnvironment({
alias: "other",
hostname: "other",
username: null,
port: null,
});
yield* TestClock.adjust(Duration.zero);
const launchesBeforeShutdown = launches;
yield* Deferred.succeed(finishShutdown, undefined);
yield* Fiber.join(disconnect);
const first = yield* Fiber.join(firstReconnect);
const second = yield* Fiber.join(secondReconnect);
assert.equal(launchesBeforeShutdown, 1);
assert.equal(launches, 2);
assert.equal(tunnels, 2);
assert.isTrue(remoteRunning);
assert.equal(first.httpBaseUrl, second.httpBaseUrl);
}).pipe(
Effect.ensuring(Deferred.succeed(finishShutdown, undefined)),
Effect.provide(layer),
Effect.scoped,
);
}),
);
});