added repo

This commit is contained in:
2026-03-03 14:25:43 +00:00
commit 4328ada595
15 changed files with 2118 additions and 0 deletions

124
tests/api.test.ts Normal file
View File

@@ -0,0 +1,124 @@
import { afterEach, describe, expect, it } from "bun:test";
import { mkdtempSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { reseedDatabase } from "../src/database";
import { createServer } from "../src/index";
let tempDir: string | null = null;
function createTestServer() {
tempDir = mkdtempSync(join(tmpdir(), "mock-task-api-http-"));
const db = reseedDatabase(join(tempDir, "test.sqlite"));
return createServer(db);
}
async function readJson(response: Response) {
return (await response.json()) as Record<string, unknown>;
}
afterEach(() => {
if (tempDir) {
rmSync(tempDir, { recursive: true, force: true });
tempDir = null;
}
});
describe("mock task API", () => {
it("serves a health endpoint", async () => {
const server = createTestServer();
const response = await server.fetch(new Request("http://mock.local/health"));
const body = await readJson(response);
expect(response.status).toBe(200);
expect(body.status).toBe("ok");
});
it("lists organizations", async () => {
const server = createTestServer();
const response = await server.fetch(new Request("http://mock.local/orgs"));
const body = await readJson(response);
expect(response.status).toBe(200);
expect(Array.isArray(body.organizations)).toBe(true);
expect((body.organizations as unknown[]).length).toBe(3);
});
it("filters tasks by org and status", async () => {
const server = createTestServer();
const response = await server.fetch(
new Request("http://mock.local/tasks?orgId=org_acme&status=blocked"),
);
const body = await readJson(response);
const tasks = body.tasks as Array<Record<string, unknown>>;
expect(response.status).toBe(200);
expect(tasks.length).toBe(1);
expect(tasks[0]?.id).toBe("task_1003");
});
it("creates a task through the HTTP contract", async () => {
const server = createTestServer();
const response = await server.fetch(
new Request("http://mock.local/tasks", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
orgId: "org_acme",
projectId: "proj_acme_ops",
title: "Run API smoke test",
priority: "low",
}),
}),
);
const body = await readJson(response);
expect(response.status).toBe(201);
expect((body.task as Record<string, unknown>).title).toBe("Run API smoke test");
});
it("updates a task through the HTTP contract", async () => {
const server = createTestServer();
const response = await server.fetch(
new Request("http://mock.local/tasks/task_1001", {
method: "PATCH",
headers: { "content-type": "application/json" },
body: JSON.stringify({
status: "done",
assigneeUserId: null,
}),
}),
);
const body = await readJson(response);
expect(response.status).toBe(200);
expect((body.task as Record<string, unknown>).status).toBe("done");
expect((body.task as Record<string, unknown>).assigneeUserId).toBeNull();
});
it("returns a valid openapi document at the import path", async () => {
const server = createTestServer();
const response = await server.fetch(new Request("http://mock.local/openapi.json"));
const body = await readJson(response);
const paths = body.paths as Record<string, unknown>;
expect(response.status).toBe(200);
expect(body.openapi).toBe("3.1.0");
expect(paths["/tasks"]).toBeDefined();
expect(paths["/orgs/{orgId}/tasks"]).toBeDefined();
});
it("supports the swagger alias path used by import tools", async () => {
const server = createTestServer();
const response = await server.fetch(new Request("http://mock.local/swagger.json"));
expect(response.status).toBe(200);
});
});

73
tests/repository.test.ts Normal file
View File

@@ -0,0 +1,73 @@
import { afterEach, describe, expect, it } from "bun:test";
import { mkdtempSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { reseedDatabase } from "../src/database";
import { createTask, getOrganizationSummary, getTaskDetail, listOrganizations, listTasks, updateTask } from "../src/repository";
let tempDir: string | null = null;
function createDb() {
tempDir = mkdtempSync(join(tmpdir(), "mock-task-api-"));
return reseedDatabase(join(tempDir, "test.sqlite"));
}
afterEach(() => {
if (tempDir) {
rmSync(tempDir, { recursive: true, force: true });
tempDir = null;
}
});
describe("mock task repository", () => {
it("lists seeded organizations and tasks", () => {
const db = createDb();
const organizations = listOrganizations(db);
const tasks = listTasks(db, { orgId: "org_acme" });
expect(organizations.length).toBe(3);
expect(tasks.length).toBeGreaterThan(0);
expect(organizations[0]).toHaveProperty("openTaskCount");
});
it("returns task detail with labels and comments", () => {
const db = createDb();
const task = getTaskDetail(db, "task_1001");
expect(task?.labels.length).toBeGreaterThan(0);
expect(task?.comments.length).toBeGreaterThan(0);
});
it("creates and updates a task", () => {
const db = createDb();
const created = createTask(db, {
orgId: "org_acme",
projectId: "proj_acme_ops",
assigneeUserId: "user_acme_2",
title: "Mock task from test",
priority: "low",
});
expect(created?.title).toBe("Mock task from test");
const updated = updateTask(db, created!.id as string, {
status: "done",
assigneeUserId: null,
});
expect(updated?.status).toBe("done");
expect(updated?.assigneeUserId).toBeNull();
});
it("returns org summary status counts", () => {
const db = createDb();
const summary = getOrganizationSummary(db, "org_acme");
expect(summary).not.toBeNull();
expect(summary?.statusBreakdown.length).toBeGreaterThan(0);
});
});