Add runtime-core tests for process and todo services
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
e2cb297b5a
commit
1bf5a4e389
3 files changed
+109
-1
No files matched your search
@@ -14,7 +14,7 @@
|
||||
},
|
||||
"scripts": {
|
||||
"typecheck": "tsc --noEmit",
|
||||
"test": "vitest run --passWithNoTests"
|
||||
"test": "vitest run"
|
||||
},
|
||||
"dependencies": {
|
||||
"@acme/contracts": "workspace:*",
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { ProcessManager } from "./processManager";
|
||||
|
||||
describe("ProcessManager", () => {
|
||||
it("can be instantiated", () => {
|
||||
const pm = new ProcessManager();
|
||||
expect(pm).toBeInstanceOf(ProcessManager);
|
||||
});
|
||||
|
||||
it("spawns a process and receives output", async () => {
|
||||
const pm = new ProcessManager();
|
||||
const chunks: string[] = [];
|
||||
|
||||
const done = new Promise<void>((resolve) => {
|
||||
pm.on("output", (chunk) => {
|
||||
chunks.push(chunk.data);
|
||||
});
|
||||
pm.on("exit", () => {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
const sessionId = pm.spawn({ command: "echo", args: ["hello"] });
|
||||
expect(typeof sessionId).toBe("string");
|
||||
expect(sessionId.length).toBeGreaterThan(0);
|
||||
|
||||
await done;
|
||||
|
||||
expect(chunks.join("").trim()).toBe("hello");
|
||||
});
|
||||
|
||||
it("can kill a spawned process", async () => {
|
||||
const pm = new ProcessManager();
|
||||
|
||||
const exited = new Promise<number | null>((resolve) => {
|
||||
pm.on("exit", (exit) => {
|
||||
resolve(exit.code);
|
||||
});
|
||||
});
|
||||
|
||||
const sessionId = pm.spawn({ command: "sleep", args: ["10"] });
|
||||
pm.kill(sessionId);
|
||||
|
||||
const code = await exited;
|
||||
expect(code).not.toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { TodoStore } from "./todoStore";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(
|
||||
tempDirs.splice(0, tempDirs.length).map(async (dir) => {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
async function createStore() {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), "runtime-core-todos-"));
|
||||
tempDirs.push(dir);
|
||||
const filePath = path.join(dir, "todos.json");
|
||||
const store = new TodoStore(filePath);
|
||||
await store.init();
|
||||
return { store, filePath };
|
||||
}
|
||||
|
||||
describe("TodoStore", () => {
|
||||
it("initializes an empty todo file", async () => {
|
||||
const { store, filePath } = await createStore();
|
||||
|
||||
expect(await store.list()).toEqual([]);
|
||||
|
||||
const raw = await readFile(filePath, "utf8");
|
||||
expect(JSON.parse(raw)).toEqual([]);
|
||||
});
|
||||
|
||||
it("adds, toggles, removes, and persists todos", async () => {
|
||||
const { store, filePath } = await createStore();
|
||||
|
||||
const afterAdd = await store.add({ title: "ship runtime core" });
|
||||
expect(afterAdd).toHaveLength(1);
|
||||
const added = afterAdd[0];
|
||||
expect(added?.title).toBe("ship runtime core");
|
||||
expect(added?.completed).toBe(false);
|
||||
|
||||
if (!added) {
|
||||
throw new Error("Expected todo to be created.");
|
||||
}
|
||||
|
||||
const afterToggle = await store.toggle(added.id);
|
||||
expect(afterToggle[0]?.completed).toBe(true);
|
||||
|
||||
const reloaded = new TodoStore(filePath);
|
||||
await reloaded.init();
|
||||
expect((await reloaded.list())[0]?.completed).toBe(true);
|
||||
|
||||
const afterRemove = await reloaded.remove(added.id);
|
||||
expect(afterRemove).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user