feat: finish the ai plugin (B18)

This commit is contained in:
LukeGus committed 2026-09-24 13:43:26 -05:00
1 parent 6b4d1432ab
commit 21937a6b02
193 files changed
+23429 -4244

No files matched your search

@@ -1,39 +1,60 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { describe, expect, it, vi } from "vitest";
import {
listCommandHistory,
setPluginServices,
readService,
requireService,
serviceAvailable,
SERVICE,
} from "../../src/backend/services.js";
afterEach(() => setPluginServices(null));
function services(provided: Record<string, object>) {
return {
providers: (service: string) => (service in provided ? [""] : []),
get: vi.fn((service: string) => provided[service]),
} as never;
}
describe("listCommandHistory", () => {
describe("optional services", () => {
it("reads the user's history through terminal.history", async () => {
const list = vi.fn(async () => [
{ command: "ls", executedAt: "2026-01-01T00:00:00.000Z" },
]);
const get = vi.fn(() => ({ list }));
setPluginServices({ get, provide: vi.fn() } as never);
const history = await readService(
services({ [SERVICE.history]: { list } }),
SERVICE.history,
(terminal) => terminal.list(4, 25),
);
const history = await listCommandHistory("user-1", 4, 25);
expect(get).toHaveBeenCalledWith("terminal.history", { userId: "user-1" });
expect(list).toHaveBeenCalledWith(4, 25);
expect(history).toEqual([
{ command: "ls", executedAt: "2026-01-01T00:00:00.000Z" },
]);
});
it("answers null while the terminal is off or refuses the user", async () => {
expect(await listCommandHistory("user-1", 4, 25)).toBeNull();
it("answers null while the provider is off or refuses the user", async () => {
expect(
await readService(services({}), SERVICE.history, (terminal) =>
terminal.list(4, 25),
),
).toBeNull();
setPluginServices({
get: () => ({
list: async () => {
throw new Error("unavailable");
},
}),
provide: vi.fn(),
} as never);
expect(await listCommandHistory("user-1", 4, 25)).toBeNull();
const refusing = {
list: async () => {
throw new Error("Missing permission");
},
};
expect(
await readService(
services({ [SERVICE.history]: refusing }),
SERVICE.history,
(terminal) => terminal.list(4, 25),
),
).toBeNull();
});
it("says which plugin is missing when a write needs it", () => {
expect(serviceAvailable(services({}), SERVICE.fleets)).toBe(false);
expect(() => requireService(services({}), SERVICE.fleets)).toThrow(
"The fleets plugin is not available",
);
});
});