repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
opencode/plugins/chef/test/store.test.ts
RawFile outline
- store line 8
- tempDir line 9
- beforeEach line 11
- afterEach line 16
- Store - Pipeline operations line 21
- creates a pipeline instance line 22
- gets pipeline by id line 37
- returns null for nonexistent pipeline line 51
- updates pipeline status line 56
- updates pipeline context line 64
- updates pipeline current_phase line 74
- updates multiple fields at once line 82
- lists all pipelines line 91
- lists pipelines filtered by status line 99
- lists pipelines filtered by completed status line 109
- Store - Phase operations line 119
- creates a phase line 120
- gets phase by id line 137
- returns null for nonexistent phase line 150
- updates phase status line 155
- updates phase error line 168
- updates phase attempts line 177
- gets phases by pipeline line 186
- Store - Column whitelist line 196
- whitelist allows valid columns line 197
- whitelist blocks unknown columns line 205
- Store - Schema migration line 218
- store works correctly after schema initialization line 219
- Store - close line 239
- close does not throw line 240
- Store - auto-creates parent directories line 246
- creates .opencode/chef if it does not exist line 247
- Store - Table name validation line 261
- updatePipeline and updatePhase use valid tables line 262
- updateRecord throws on unknown table name line 272
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { existsSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { mkdtempSync } from "node:fs";
import { Store } from "../store";
let store: Store;
let tempDir: string;
beforeEach(() => {
tempDir = mkdtempSync(join(tmpdir(), "chef-test-"));
store = new Store(tempDir);
});
afterEach(() => {
store.close();
rmSync(tempDir, { recursive: true, force: true });
});
describe("Store - Pipeline operations", () => {
test("creates a pipeline instance", () => {
const pipeline = store.createPipeline("test-pipeline", { key: "value" });
expect(pipeline).toMatchObject({
id: expect.any(String),
definition: "test-pipeline",
status: "running",
input: JSON.stringify({ key: "value" }),
context: "{}",
current_phase: 0,
created_at: expect.any(Number),
updated_at: expect.any(Number),
});
});
test("gets pipeline by id", () => {
const created = store.createPipeline("test-pipeline", { key: "value" });
const retrieved = store.getPipeline(created.id);
expect(retrieved).toMatchObject({
id: created.id,
definition: "test-pipeline",
status: "running",
input: JSON.stringify({ key: "value" }),
context: "{}",
current_phase: 0,
});
});
test("returns null for nonexistent pipeline", () => {
const result = store.getPipeline("nonexistent");
expect(result).toBeNull();
});
test("updates pipeline status", () => {
const pipeline = store.createPipeline("test-pipeline", {});
store.updatePipeline(pipeline.id, { status: "completed" });
const updated = store.getPipeline(pipeline.id);
expect(updated?.status).toBe("completed");
});
test("updates pipeline context", () => {
const pipeline = store.createPipeline("test-pipeline", {});
store.updatePipeline(pipeline.id, {
context: JSON.stringify({ foo: "bar" }),
});
const updated = store.getPipeline(pipeline.id);
expect(updated?.context).toBe(JSON.stringify({ foo: "bar" }));
});
test("updates pipeline current_phase", () => {
const pipeline = store.createPipeline("test-pipeline", {});
store.updatePipeline(pipeline.id, { current_phase: 3 });
const updated = store.getPipeline(pipeline.id);
expect(updated?.current_phase).toBe(3);
});
test("updates multiple fields at once", () => {
const pipeline = store.createPipeline("test-pipeline", {});
store.updatePipeline(pipeline.id, { status: "failed", current_phase: 2 });
const updated = store.getPipeline(pipeline.id);
expect(updated?.status).toBe("failed");
expect(updated?.current_phase).toBe(2);
});
test("lists all pipelines", () => {
store.createPipeline("pipeline-1", {});
store.createPipeline("pipeline-2", {});
const pipelines = store.listPipelines();
expect(pipelines).toHaveLength(2);
});
test("lists pipelines filtered by status", () => {
const p1 = store.createPipeline("pipeline-1", {});
store.createPipeline("pipeline-2", {});
store.createPipeline("pipeline-3", {});
store.updatePipeline(p1.id, { status: "completed" });
const running = store.listPipelines("running");
expect(running).toHaveLength(2);
});
test("lists pipelines filtered by completed status", () => {
const p1 = store.createPipeline("pipeline-1", {});
store.createPipeline("pipeline-2", {});
store.updatePipeline(p1.id, { status: "completed" });
const completed = store.listPipelines("completed");
expect(completed).toHaveLength(1);
});
});
describe("Store - Phase operations", () => {
test("creates a phase", () => {
const pipeline = store.createPipeline("test-pipeline", {});
const phase = store.createPhase(pipeline.id, "step-1");
expect(phase).toMatchObject({
id: expect.any(String),
pipeline_id: pipeline.id,
name: "step-1",
status: "pending",
output: "{}",
error: null,
attempts: 0,
started_at: null,
completed_at: null,
});
});
test("gets phase by id", () => {
const pipeline = store.createPipeline("test-pipeline", {});
const created = store.createPhase(pipeline.id, "step-1");
const retrieved = store.getPhase(created.id);
expect(retrieved).toMatchObject({
id: created.id,
pipeline_id: pipeline.id,
name: "step-1",
status: "pending",
});
});
test("returns null for nonexistent phase", () => {
const result = store.getPhase("nonexistent");
expect(result).toBeNull();
});
test("updates phase status", () => {
const pipeline = store.createPipeline("test-pipeline", {});
const phase = store.createPhase(pipeline.id, "step-1");
store.updatePhase(phase.id, {
status: "completed",
output: JSON.stringify({ result: true }),
});
const updated = store.getPhase(phase.id);
expect(updated?.status).toBe("completed");
expect(updated?.output).toBe(JSON.stringify({ result: true }));
});
test("updates phase error", () => {
const pipeline = store.createPipeline("test-pipeline", {});
const phase = store.createPhase(pipeline.id, "step-1");
store.updatePhase(phase.id, { error: "something went wrong" });
const updated = store.getPhase(phase.id);
expect(updated?.error).toBe("something went wrong");
});
test("updates phase attempts", () => {
const pipeline = store.createPipeline("test-pipeline", {});
const phase = store.createPhase(pipeline.id, "step-1");
store.updatePhase(phase.id, { attempts: 3 });
const updated = store.getPhase(phase.id);
expect(updated?.attempts).toBe(3);
});
test("gets phases by pipeline", () => {
const pipeline = store.createPipeline("test-pipeline", {});
store.createPhase(pipeline.id, "step-1");
store.createPhase(pipeline.id, "step-2");
const phases = store.getPhasesByPipeline(pipeline.id);
expect(phases).toHaveLength(2);
});
});
describe("Store - Column whitelist", () => {
test("whitelist allows valid columns", () => {
const pipeline = store.createPipeline("test-pipeline", {});
store.updatePipeline(pipeline.id, { status: "completed" });
const updated = store.getPipeline(pipeline.id);
expect(updated?.status).toBe("completed");
});
test("whitelist blocks unknown columns", () => {
const pipeline = store.createPipeline("test-pipeline", {});
// This should not crash and should not set the evil column
store.updatePipeline(pipeline.id, { evil_column: "hack" } as any);
const updated = store.getPipeline(pipeline.id);
// The pipeline should still exist and have original values
expect(updated).not.toBeNull();
expect(updated?.definition).toBe("test-pipeline");
expect((updated as any).evil_column).toBeUndefined();
});
});
describe("Store - Schema migration", () => {
test("store works correctly after schema initialization", () => {
// Schema initialization is implicitly tested by all other tests
// This test verifies basic CRUD still works
const pipeline = store.createPipeline("migrated-pipeline", { test: true });
expect(pipeline.id).toBeDefined();
const phase = store.createPhase(pipeline.id, "phase-1");
expect(phase.id).toBeDefined();
store.updatePipeline(pipeline.id, { status: "completed" });
store.updatePhase(phase.id, { status: "completed" });
const updatedPipeline = store.getPipeline(pipeline.id);
const updatedPhase = store.getPhase(phase.id);
expect(updatedPipeline?.status).toBe("completed");
expect(updatedPhase?.status).toBe("completed");
});
});
describe("Store - close", () => {
test("close does not throw", () => {
const testStore = new Store(tempDir);
expect(() => testStore.close()).not.toThrow();
});
});
describe("Store - auto-creates parent directories", () => {
test("creates .opencode/chef if it does not exist", () => {
const deepDir = join(tempDir, "a", "b", "c");
const deepStore = new Store(deepDir);
try {
const pipeline = deepStore.createPipeline("auto-dir-test", {});
expect(pipeline.id).toBeDefined();
expect(pipeline.status).toBe("running");
expect(existsSync(join(deepDir, ".opencode", "chef"))).toBe(true);
} finally {
deepStore.close();
}
});
});
describe("Store - Table name validation", () => {
test("updatePipeline and updatePhase use valid tables", () => {
const pipeline = store.createPipeline("test-pipeline", {});
expect(() => store.updatePipeline(pipeline.id, { status: "completed" }))
.not.toThrow();
const phase = store.createPhase(pipeline.id, "test-phase");
expect(() => store.updatePhase(phase.id, { status: "completed" }))
.not.toThrow();
});
test("updateRecord throws on unknown table name", () => {
expect(() =>
(store as any).updateRecord("evil_table", "some-id", { status: "hacked" })
)
.toThrow(/unknown table/);
});
});