|
| 1 | +import { describe, test, expect, beforeEach, afterEach } from "bun:test" |
| 2 | +import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs" |
| 3 | +import { join } from "node:path" |
| 4 | +import { findNearestMessageWithFields } from "./injector" |
| 5 | + |
| 6 | +const TEST_DIR = "/tmp/test-hook-message-injector" |
| 7 | + |
| 8 | +describe("findNearestMessageWithFields", () => { |
| 9 | + beforeEach(() => { |
| 10 | + if (existsSync(TEST_DIR)) { |
| 11 | + rmSync(TEST_DIR, { recursive: true }) |
| 12 | + } |
| 13 | + mkdirSync(TEST_DIR, { recursive: true }) |
| 14 | + }) |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + if (existsSync(TEST_DIR)) { |
| 18 | + rmSync(TEST_DIR, { recursive: true }) |
| 19 | + } |
| 20 | + }) |
| 21 | + |
| 22 | + test("returns message with model info when available", () => { |
| 23 | + // #given |
| 24 | + const messageWithModel = { |
| 25 | + id: "msg_001", |
| 26 | + agent: "Sisyphus", |
| 27 | + model: { providerID: "openai", modelID: "gpt-5.2" }, |
| 28 | + tools: { write: true }, |
| 29 | + } |
| 30 | + writeFileSync(join(TEST_DIR, "msg_001.json"), JSON.stringify(messageWithModel)) |
| 31 | + |
| 32 | + // #when |
| 33 | + const result = findNearestMessageWithFields(TEST_DIR) |
| 34 | + |
| 35 | + // #then |
| 36 | + expect(result).not.toBeNull() |
| 37 | + expect(result?.agent).toBe("Sisyphus") |
| 38 | + expect(result?.model?.providerID).toBe("openai") |
| 39 | + expect(result?.model?.modelID).toBe("gpt-5.2") |
| 40 | + }) |
| 41 | + |
| 42 | + test("returns most recent message with model info", () => { |
| 43 | + // #given |
| 44 | + const olderMessage = { |
| 45 | + id: "msg_001", |
| 46 | + agent: "Sisyphus", |
| 47 | + model: { providerID: "anthropic", modelID: "claude-opus-4-5" }, |
| 48 | + } |
| 49 | + const newerMessage = { |
| 50 | + id: "msg_002", |
| 51 | + agent: "oracle", |
| 52 | + model: { providerID: "openai", modelID: "gpt-5.2" }, |
| 53 | + } |
| 54 | + writeFileSync(join(TEST_DIR, "msg_001.json"), JSON.stringify(olderMessage)) |
| 55 | + writeFileSync(join(TEST_DIR, "msg_002.json"), JSON.stringify(newerMessage)) |
| 56 | + |
| 57 | + // #when |
| 58 | + const result = findNearestMessageWithFields(TEST_DIR) |
| 59 | + |
| 60 | + // #then |
| 61 | + expect(result?.agent).toBe("oracle") |
| 62 | + expect(result?.model?.providerID).toBe("openai") |
| 63 | + expect(result?.model?.modelID).toBe("gpt-5.2") |
| 64 | + }) |
| 65 | + |
| 66 | + test("skips messages without complete model info", () => { |
| 67 | + // #given |
| 68 | + const incompleteMessage = { |
| 69 | + id: "msg_002", |
| 70 | + agent: "explore", |
| 71 | + model: { providerID: "openai" }, |
| 72 | + } |
| 73 | + const completeMessage = { |
| 74 | + id: "msg_001", |
| 75 | + agent: "Sisyphus", |
| 76 | + model: { providerID: "anthropic", modelID: "claude-opus-4-5" }, |
| 77 | + } |
| 78 | + writeFileSync(join(TEST_DIR, "msg_001.json"), JSON.stringify(completeMessage)) |
| 79 | + writeFileSync(join(TEST_DIR, "msg_002.json"), JSON.stringify(incompleteMessage)) |
| 80 | + |
| 81 | + // #when |
| 82 | + const result = findNearestMessageWithFields(TEST_DIR) |
| 83 | + |
| 84 | + // #then |
| 85 | + expect(result?.agent).toBe("Sisyphus") |
| 86 | + expect(result?.model?.providerID).toBe("anthropic") |
| 87 | + expect(result?.model?.modelID).toBe("claude-opus-4-5") |
| 88 | + }) |
| 89 | + |
| 90 | + test("falls back to message with agent only when no model info exists", () => { |
| 91 | + // #given |
| 92 | + const agentOnlyMessage = { |
| 93 | + id: "msg_001", |
| 94 | + agent: "librarian", |
| 95 | + } |
| 96 | + writeFileSync(join(TEST_DIR, "msg_001.json"), JSON.stringify(agentOnlyMessage)) |
| 97 | + |
| 98 | + // #when |
| 99 | + const result = findNearestMessageWithFields(TEST_DIR) |
| 100 | + |
| 101 | + // #then |
| 102 | + expect(result?.agent).toBe("librarian") |
| 103 | + expect(result?.model).toBeUndefined() |
| 104 | + }) |
| 105 | + |
| 106 | + test("returns null for empty directory", () => { |
| 107 | + // #given - empty directory (already created in beforeEach) |
| 108 | + |
| 109 | + // #when |
| 110 | + const result = findNearestMessageWithFields(TEST_DIR) |
| 111 | + |
| 112 | + // #then |
| 113 | + expect(result).toBeNull() |
| 114 | + }) |
| 115 | + |
| 116 | + test("returns null for non-existent directory", () => { |
| 117 | + // #given |
| 118 | + const nonExistentDir = "/tmp/non-existent-test-dir-12345" |
| 119 | + |
| 120 | + // #when |
| 121 | + const result = findNearestMessageWithFields(nonExistentDir) |
| 122 | + |
| 123 | + // #then |
| 124 | + expect(result).toBeNull() |
| 125 | + }) |
| 126 | + |
| 127 | + test("preserves tools field from stored message", () => { |
| 128 | + // #given |
| 129 | + const messageWithTools = { |
| 130 | + id: "msg_001", |
| 131 | + agent: "frontend-ui-ux-engineer", |
| 132 | + model: { providerID: "google", modelID: "gemini-3-pro-preview" }, |
| 133 | + tools: { write: true, edit: true, bash: false }, |
| 134 | + } |
| 135 | + writeFileSync(join(TEST_DIR, "msg_001.json"), JSON.stringify(messageWithTools)) |
| 136 | + |
| 137 | + // #when |
| 138 | + const result = findNearestMessageWithFields(TEST_DIR) |
| 139 | + |
| 140 | + // #then |
| 141 | + expect(result?.tools).toEqual({ write: true, edit: true, bash: false }) |
| 142 | + }) |
| 143 | + |
| 144 | + test("handles malformed JSON files gracefully", () => { |
| 145 | + // #given |
| 146 | + const validMessage = { |
| 147 | + id: "msg_001", |
| 148 | + agent: "Sisyphus", |
| 149 | + model: { providerID: "openai", modelID: "gpt-5.2" }, |
| 150 | + } |
| 151 | + writeFileSync(join(TEST_DIR, "msg_001.json"), JSON.stringify(validMessage)) |
| 152 | + writeFileSync(join(TEST_DIR, "msg_002.json"), "{ invalid json }") |
| 153 | + |
| 154 | + // #when |
| 155 | + const result = findNearestMessageWithFields(TEST_DIR) |
| 156 | + |
| 157 | + // #then |
| 158 | + expect(result?.agent).toBe("Sisyphus") |
| 159 | + }) |
| 160 | +}) |
0 commit comments