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 {
processProbeFailure,
makeProbeFailureLayer,
LSOF_TEST_PORT,
makeLsofScannerLayer,
} from "./testUtils/portScanner.ts";
import { it as effectIt } from "@effect/vitest";
import {
CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS,
type DiscoveredLocalServer,
} from "@akeru/contracts";
import * as Clock from "effect/Clock";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as Scope from "effect/Scope";
import * as TestClock from "effect/testing/TestClock";
import { expect } from "vite-plus/test";
import * as PortScanner from "./PortScanner.ts";
effectIt.effect("revalidates a successful HTML probe after its cache entry expires", () => {
let responds = true;
const requests: string[] = [];
const fetchFn = ((input: Parameters<typeof globalThis.fetch>[0]) => {
requests.push(String(input));
return responds
? Promise.resolve(new Response("hello", { headers: { "content-type": "text/html" } }))
: Promise.reject(new TypeError("not HTTP"));
}) as typeof globalThis.fetch;
const layer = makeLsofScannerLayer({ pid: () => 1234, fetch: fetchFn });
return Effect.gen(function* () {
const scanner = yield* PortScanner.PortDiscovery;
expect(yield* scanner.scan()).toHaveLength(1);
expect(yield* scanner.scan()).toHaveLength(1);
expect(requests).toEqual([`http://localhost:${LSOF_TEST_PORT}/`]);
responds = false;
yield* TestClock.adjust(Duration.seconds(15));
expect(yield* scanner.scan()).toHaveLength(0);
expect(requests).toEqual([
`http://localhost:${LSOF_TEST_PORT}/`,
`http://localhost:${LSOF_TEST_PORT}/`,
`https://localhost:${LSOF_TEST_PORT}/`,
]);
}).pipe(Effect.provide(layer));
});
effectIt.effect("keeps a full configured URL when the discovered server root fails", () => {
const requests: string[] = [];
const configuredUrl = `http://localhost:${LSOF_TEST_PORT}/docs`;
const fetchFn = ((input: Parameters<typeof globalThis.fetch>[0]) => {
const url = String(input);
requests.push(url);
return Promise.resolve(
url === configuredUrl
? new Response("docs", { headers: { "content-type": "text/html" } })
: new Response("not found", {
status: 404,
headers: { "content-type": "text/html" },
}),
);
}) as typeof globalThis.fetch;
const layer = makeLsofScannerLayer({ pid: () => 1234, fetch: fetchFn });
return Effect.gen(function* () {
const scanner = yield* PortScanner.PortDiscovery;
const servers = yield* scanner.scan([configuredUrl]);
expect(servers).toHaveLength(1);
expect(servers[0]?.url).toBe(configuredUrl);
expect(requests).toContain(configuredUrl);
}).pipe(Effect.provide(layer));
});
effectIt.effect("probes configured custom ports through a canonical loopback host", () => {
const customPort = 43_124;
const configuredUrl = `http://0.0.0.0:${customPort}/docs`;
const expectedUrl = `http://localhost:${customPort}/docs`;
const requests: string[] = [];
const fetchFn = ((input: Parameters<typeof globalThis.fetch>[0]) => {
requests.push(String(input));
return Promise.resolve(new Response("docs", { headers: { "content-type": "text/html" } }));
}) as typeof globalThis.fetch;
const layer = makeProbeFailureLayer(processProbeFailure, fetchFn);
return Effect.gen(function* () {
const scanner = yield* PortScanner.PortDiscovery;
const servers = yield* scanner.scan([configuredUrl]);
expect(servers).toHaveLength(1);
expect(servers[0]?.host).toBe("localhost");
expect(servers[0]?.port).toBe(customPort);
expect(servers[0]?.url).toBe(expectedUrl);
expect(requests).toEqual([expectedUrl]);
}).pipe(Effect.provide(layer));
});
effectIt.effect("projects configured paths independently for simultaneous subscribers", () => {
const docsUrl = `http://localhost:${LSOF_TEST_PORT}/docs`;
const adminUrl = `http://localhost:${LSOF_TEST_PORT}/admin`;
const fetchFn = ((input: Parameters<typeof globalThis.fetch>[0]) => {
const url = String(input);
return Promise.resolve(
url === docsUrl || url === adminUrl
? new Response("app", { headers: { "content-type": "text/html" } })
: new Response("not found", { status: 404 }),
);
}) as typeof globalThis.fetch;
const layer = makeLsofScannerLayer({ pid: () => 1234, fetch: fetchFn });
return Effect.gen(function* () {
const scanner = yield* PortScanner.PortDiscovery;
const docsSnapshots: ReadonlyArray<DiscoveredLocalServer>[] = [];
const adminSnapshots: ReadonlyArray<DiscoveredLocalServer>[] = [];
yield* scanner.subscribe({ configuredUrls: [docsUrl], initialSnapshot: [] }, (servers) =>
Effect.sync(() => docsSnapshots.push(servers)),
);
yield* scanner.subscribe({ configuredUrls: [adminUrl], initialSnapshot: [] }, (servers) =>
Effect.sync(() => adminSnapshots.push(servers)),
);
yield* scanner.retain;
expect(docsSnapshots.at(-1)?.[0]?.url).toBe(docsUrl);
expect(adminSnapshots.at(-1)?.[0]?.url).toBe(adminUrl);
}).pipe(Effect.scoped, Effect.provide(layer));
});
effectIt.effect(
"keeps each subscriber's candidates when their combined union exceeds the per-client cap",
() => {
const firstSubscriberUrls = Array.from(
{ length: CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS },
(_, index) => `http://localhost:${LSOF_TEST_PORT}/app-${index}`,
);
const secondSubscriberUrl = `http://localhost:${LSOF_TEST_PORT}/app-${CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS}`;
const fetchFn = ((input: Parameters<typeof globalThis.fetch>[0]) =>
Promise.resolve(
String(input) === secondSubscriberUrl
? new Response("app", { headers: { "content-type": "text/html" } })
: new Response("not found", { status: 404 }),
)) as typeof globalThis.fetch;
const layer = makeLsofScannerLayer({ pid: () => 1234, fetch: fetchFn });
return Effect.gen(function* () {
const scanner = yield* PortScanner.PortDiscovery;
const secondSnapshots: ReadonlyArray<DiscoveredLocalServer>[] = [];
yield* scanner.subscribe(
{ configuredUrls: firstSubscriberUrls, initialSnapshot: [] },
() => Effect.void,
);
yield* scanner.subscribe(
{ configuredUrls: [secondSubscriberUrl], initialSnapshot: [] },
(servers) => Effect.sync(() => secondSnapshots.push(servers)),
);
yield* scanner.retain;
expect(secondSnapshots.at(-1)?.[0]?.url).toBe(secondSubscriberUrl);
}).pipe(Effect.scoped, Effect.provide(layer));
},
);
effectIt.effect("stops probing a subscriber's configured paths after its scope closes", () => {
const docsUrl = `http://localhost:${LSOF_TEST_PORT}/docs`;
const adminUrl = `http://localhost:${LSOF_TEST_PORT}/admin`;
const requests: string[] = [];
const fetchFn = ((input: Parameters<typeof globalThis.fetch>[0]) => {
const url = String(input);
requests.push(url);
return Promise.resolve(
url === docsUrl || url === adminUrl
? new Response("app", { headers: { "content-type": "text/html" } })
: new Response("not found", { status: 404 }),
);
}) as typeof globalThis.fetch;
const layer = makeLsofScannerLayer({ pid: () => 1234, fetch: fetchFn });
return Effect.gen(function* () {
const scanner = yield* PortScanner.PortDiscovery;
const docsScope = yield* Scope.make();
yield* scanner
.subscribe({ configuredUrls: [docsUrl], initialSnapshot: [] }, () => Effect.void)
.pipe(Effect.provideService(Scope.Scope, docsScope));
yield* scanner.subscribe(
{ configuredUrls: [adminUrl], initialSnapshot: [] },
() => Effect.void,
);
yield* scanner.retain;
yield* Scope.close(docsScope, Exit.void);
requests.length = 0;
yield* TestClock.adjust(Duration.seconds(15));
expect(requests).toContain(adminUrl);
expect(requests).not.toContain(docsUrl);
}).pipe(Effect.scoped, Effect.provide(layer));
});
effectIt.effect("uses the current configured fragment when readiness comes from cache", () => {
const requests: string[] = [];
const fetchFn = ((input: Parameters<typeof globalThis.fetch>[0]) => {
requests.push(String(input));
return Promise.resolve(new Response("docs", { headers: { "content-type": "text/html" } }));
}) as typeof globalThis.fetch;
const layer = makeLsofScannerLayer({ pid: () => 1234, fetch: fetchFn });
const oldUrl = `http://localhost:${LSOF_TEST_PORT}/docs#old`;
const newUrl = `http://localhost:${LSOF_TEST_PORT}/docs#new`;
return Effect.gen(function* () {
const scanner = yield* PortScanner.PortDiscovery;
expect((yield* scanner.scan([oldUrl]))[0]?.url).toBe(oldUrl);
const requestCount = requests.length;
expect((yield* scanner.scan([newUrl]))[0]?.url).toBe(newUrl);
expect(requests).toHaveLength(requestCount);
}).pipe(Effect.provide(layer));
});
effectIt.effect("shares a configured root probe with discovered-root classification", () => {
const requests: string[] = [];
const rootUrl = `http://localhost:${LSOF_TEST_PORT}/`;
const fetchFn = ((input: Parameters<typeof globalThis.fetch>[0]) => {
requests.push(String(input));
return Promise.resolve(new Response("app", { headers: { "content-type": "text/html" } }));
}) as typeof globalThis.fetch;
const layer = makeLsofScannerLayer({ pid: () => 1234, fetch: fetchFn });
return Effect.gen(function* () {
const scanner = yield* PortScanner.PortDiscovery;
expect(yield* scanner.scan([rootUrl])).toHaveLength(1);
expect(requests).toEqual([rootUrl]);
yield* TestClock.adjust(Duration.seconds(15));
expect(yield* scanner.scan([rootUrl])).toHaveLength(1);
expect(requests).toEqual([rootUrl, rootUrl]);
}).pipe(Effect.provide(layer));
});
effectIt.effect("starts fresh cache entries after the probing batch completes", () =>
Effect.gen(function* () {
const baseClock = yield* Clock.Clock;
const times = [0, 20_000, 20_000, 20_000];
let timeIndex = 0;
const currentTimeMillis = () => times[Math.min(timeIndex++, times.length - 1)]!;
const clock: Clock.Clock = {
...baseClock,
currentTimeMillisUnsafe: currentTimeMillis,
currentTimeMillis: Effect.sync(currentTimeMillis),
};
const requests: string[] = [];
const fetchFn = ((input: Parameters<typeof globalThis.fetch>[0]) => {
requests.push(String(input));
return Promise.resolve(new Response("app", { headers: { "content-type": "text/html" } }));
}) as typeof globalThis.fetch;
const layer = makeLsofScannerLayer({ pid: () => 1234, fetch: fetchFn });
yield* Effect.gen(function* () {
const scanner = yield* PortScanner.PortDiscovery;
expect(yield* scanner.scan()).toHaveLength(1);
expect(yield* scanner.scan()).toHaveLength(1);
expect(requests).toHaveLength(1);
}).pipe(Effect.provide(layer), Effect.provideService(Clock.Clock, clock));
}),
);
effectIt.effect("caches a failed web probe until its bounded cache entry expires", () => {
let responds = false;
const requests: string[] = [];
const fetchFn = ((input: Parameters<typeof globalThis.fetch>[0]) => {
requests.push(String(input));
return responds
? Promise.resolve(new Response("hello", { headers: { "content-type": "text/html" } }))
: Promise.reject(new TypeError("not HTTP"));
}) as typeof globalThis.fetch;
const layer = makeLsofScannerLayer({ pid: () => 1234, fetch: fetchFn });
return Effect.gen(function* () {
const scanner = yield* PortScanner.PortDiscovery;
expect(yield* scanner.scan()).toHaveLength(0);
expect(yield* scanner.scan()).toHaveLength(0);
expect(requests).toHaveLength(2);
responds = true;
yield* TestClock.adjust(Duration.seconds(15));
expect(yield* scanner.scan()).toHaveLength(1);
expect(requests).toHaveLength(3);
}).pipe(Effect.provide(layer));
});
effectIt.effect("falls back to HTTPS and does not follow redirects while probing", () => {
const redirects: Array<string | undefined> = [];
const fetchFn = (async (
input: Parameters<typeof globalThis.fetch>[0],
init?: Parameters<typeof globalThis.fetch>[1],
) => {
redirects.push(init?.redirect);
if (String(input).startsWith("http:")) throw new TypeError("TLS listener");
return new Response(null, { status: 302, headers: { location: "https://example.com" } });
}) as typeof globalThis.fetch;
const layer = makeLsofScannerLayer({ pid: () => 1234, fetch: fetchFn });
return Effect.gen(function* () {
const scanner = yield* PortScanner.PortDiscovery;
const servers = yield* scanner.scan();
expect(servers).toHaveLength(1);
expect(servers[0]?.url).toBe(`https://localhost:${LSOF_TEST_PORT}`);
expect(redirects).toEqual(["manual", "manual"]);
}).pipe(Effect.provide(layer));
});
effectIt.effect(
"excludes HTTP errors, non-navigation responses, and successful non-documents",
() => {
let pid = 1;
let makeResponse = () =>
new Response("not found", { status: 404, headers: { "content-type": "text/html" } });
const fetchFn = ((_input: Parameters<typeof globalThis.fetch>[0]) =>
Promise.resolve(makeResponse())) as typeof globalThis.fetch;
const layer = makeLsofScannerLayer({ pid: () => pid, fetch: fetchFn });
return Effect.gen(function* () {
const scanner = yield* PortScanner.PortDiscovery;
expect(yield* scanner.scan()).toHaveLength(0);
pid += 1;
makeResponse = () =>
new Response("{}", { status: 200, headers: { "content-type": "application/json" } });
expect(yield* scanner.scan()).toHaveLength(0);
pid += 1;
makeResponse = () =>
new Response("ready", { status: 200, headers: { "content-type": "text/plain" } });
expect(yield* scanner.scan()).toHaveLength(0);
pid += 1;
makeResponse = () => new Response(null, { status: 304, headers: { location: "/cached" } });
expect(yield* scanner.scan()).toHaveLength(0);
pid += 1;
makeResponse = () =>
new Response(null, { status: 204, headers: { "content-type": "text/html" } });
expect(yield* scanner.scan()).toHaveLength(0);
pid += 1;
makeResponse = () => new Response(null, { status: 302 });
expect(yield* scanner.scan()).toHaveLength(0);
pid += 1;
makeResponse = () =>
new Response("<html />", {
status: 200,
headers: { "content-type": "application/xhtml+xml; charset=utf-8" },
});
expect(yield* scanner.scan()).toHaveLength(1);
}).pipe(Effect.provide(layer));
},
);
effectIt.effect("aborts HTTP and HTTPS probes when they time out", () => {
const aborted: string[] = [];
const fetchFn = ((
input: Parameters<typeof globalThis.fetch>[0],
init?: Parameters<typeof globalThis.fetch>[1],
) =>
new Promise<Response>((_resolve, reject) => {
const signal = init?.signal;
const onAbort = () => {
aborted.push(String(input));
reject(new DOMException("Aborted", "AbortError"));
};
if (signal?.aborted) {
onAbort();
} else {
signal?.addEventListener("abort", onAbort, { once: true });
}
})) as typeof globalThis.fetch;
const layer = makeLsofScannerLayer({ pid: () => 1234, fetch: fetchFn });
return Effect.gen(function* () {
const scanner = yield* PortScanner.PortDiscovery;
const scanFiber = yield* Effect.forkChild(scanner.scan());
yield* TestClock.adjust(Duration.seconds(2));
expect(yield* Fiber.join(scanFiber)).toHaveLength(0);
expect(aborted).toEqual([
`http://localhost:${LSOF_TEST_PORT}/`,
`https://localhost:${LSOF_TEST_PORT}/`,
]);
}).pipe(Effect.provide(layer));
});