Integrate auth, config, watcher and terminal wiring in buildApp: auth guards every API route by default, notebook watcher events feed the /api/events hub, the terminal runs in the configured workspace, and SHELLBOOK_DATA_DIR is the single data directory variable. The systemd unit uses KillMode=process so tmux sessions survive service restarts.
62 lines
3.1 KiB
TypeScript
62 lines
3.1 KiB
TypeScript
import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import type { FastifyInstance } from "fastify";
|
|
import type { ServerEvent } from "@shellbook/core";
|
|
import { buildApp } from "../src/app";
|
|
import { EventsHub } from "../src/runs";
|
|
import { setPassword } from "../src/auth/credentials";
|
|
|
|
const cleanup: Array<() => Promise<unknown>> = [];
|
|
afterEach(async () => { for (const fn of cleanup.splice(0).reverse()) await fn(); });
|
|
|
|
async function workspace() {
|
|
const root = await mkdtemp(join(tmpdir(), "shellbook-int-"));
|
|
cleanup.push(() => rm(root, { recursive: true, force: true }));
|
|
const workspaceDir = join(root, "workspace");
|
|
const dataDir = join(root, "data");
|
|
await mkdir(join(workspaceDir, "notebooks"), { recursive: true });
|
|
await writeFile(join(workspaceDir, "hosts.yaml"), "[]\n");
|
|
await writeFile(join(workspaceDir, "notebooks", "a.md"), "# A\n");
|
|
return { workspaceDir, dataDir, sshKeyPath: join(dataDir, "id_ed25519") };
|
|
}
|
|
async function app(options: Parameters<typeof buildApp>[0]): Promise<FastifyInstance> {
|
|
const instance = await buildApp(options);
|
|
cleanup.push(() => instance.close());
|
|
return instance;
|
|
}
|
|
|
|
describe("app wiring", () => {
|
|
it("publishes notebook watcher events into the /api/events hub", async () => {
|
|
const config = await workspace();
|
|
const hub = new EventsHub();
|
|
const events: ServerEvent[] = [];
|
|
hub.subscribe(event => events.push(event));
|
|
await app({ config, events: hub, auth: false, watchNotebooks: true });
|
|
await writeFile(join(config.workspaceDir, "notebooks", "a.md"), "# A changed\n");
|
|
await vi.waitFor(() => expect(events).toContainEqual({ type: "file.changed", path: "a.md" }), { timeout: 5000 });
|
|
});
|
|
|
|
it("guards every API route from the run, workspace and terminal plugins by default", async () => {
|
|
const config = await workspace();
|
|
await setPassword(config.dataDir, "pw");
|
|
const instance = await app({ config, terminal: { ptyFactory: () => { throw new Error("no pty in this test"); } } });
|
|
const routes: Array<[string, string]> = [
|
|
["GET", "/api/notebooks"], ["GET", "/api/notebooks/a.md"], ["PUT", "/api/notebooks/a.md"],
|
|
["GET", "/api/hosts"], ["POST", "/api/runs"], ["GET", "/api/events"],
|
|
["GET", "/api/terminal?session=main"], ["GET", "/api/auth/me"],
|
|
["GET", `/api/runs/${"0".repeat(8)}-0000-0000-0000-${"0".repeat(12)}/artifacts/x`],
|
|
];
|
|
for (const [method, url] of routes) {
|
|
const res = await instance.inject({ method: method as "GET", url, payload: method === "GET" ? undefined : {} });
|
|
expect(res.statusCode, `${method} ${url}`).toBe(401);
|
|
}
|
|
expect((await instance.inject("/api/health")).statusCode).toBe(200);
|
|
const login = await instance.inject({ method: "POST", url: "/api/auth/login", payload: { password: "pw" } });
|
|
expect(login.statusCode).toBe(204);
|
|
const cookie = String(login.headers["set-cookie"]).split(";")[0]!;
|
|
expect((await instance.inject({ url: "/api/notebooks", headers: { cookie } })).json().notebooks).toEqual([{ path: "a.md", title: "A" }]);
|
|
});
|
|
});
|