Files
oh-my-opencode/src/features/context-injector/collector.test.ts
T
YeonGyu-Kim f146aeff0f refactor: major codebase cleanup - BDD comments, file splitting, bug fixes (#1350)
* style(tests): normalize BDD comments from '// #given' to '// given'

- Replace 4,668 Python-style BDD comments across 107 test files
- Patterns changed: // #given -> // given, // #when -> // when, // #then -> // then
- Also handles no-space variants: //#given -> // given

* fix(rules-injector): prefer output.metadata.filePath over output.title

- Extract file path resolution to dedicated output-path.ts module
- Prefer metadata.filePath which contains actual file path
- Fall back to output.title only when metadata unavailable
- Fixes issue where rules weren't injected when tool output title was a label

* feat(slashcommand): add optional user_message parameter

- Add user_message optional parameter for command arguments
- Model can now call: command='publish' user_message='patch'
- Improves error messages with clearer format guidance
- Helps LLMs understand correct parameter usage

* feat(hooks): restore compaction-context-injector hook

- Restore hook deleted in cbbc7bd0 for session compaction context
- Injects 7 mandatory sections: User Requests, Final Goal, Work Completed,
  Remaining Tasks, Active Working Context, MUST NOT Do, Agent Verification State
- Re-register in hooks/index.ts and main plugin entry

* refactor(background-agent): split manager.ts into focused modules

- Extract constants.ts for TTL values and internal types (52 lines)
- Extract state.ts for TaskStateManager class (204 lines)
- Extract spawner.ts for task creation logic (244 lines)
- Extract result-handler.ts for completion handling (265 lines)
- Reduce manager.ts from 1377 to 755 lines (45% reduction)
- Maintain backward compatible exports

* refactor(agents): split prometheus-prompt.ts into subdirectory

- Move 1196-line prometheus-prompt.ts to prometheus/ subdirectory
- Organize prompt sections into separate files for maintainability
- Update agents/index.ts exports

* refactor(delegate-task): split tools.ts into focused modules

- Extract categories.ts for category definitions and routing
- Extract executor.ts for task execution logic
- Extract helpers.ts for utility functions
- Extract prompt-builder.ts for prompt construction
- Reduce tools.ts complexity with cleaner separation of concerns

* refactor(builtin-skills): split skills.ts into individual skill files

- Move each skill to dedicated file in skills/ subdirectory
- Create barrel export for backward compatibility
- Improve maintainability with focused skill modules

* chore: update import paths and lockfile

- Update prometheus import path after refactor
- Update bun.lock

* fix(tests): complete BDD comment normalization

- Fix remaining #when/#then patterns missed by initial sed
- Affected: state.test.ts, events.test.ts

---------

Co-authored-by: justsisyphus <justsisyphus@users.noreply.github.com>
2026-02-01 16:47:50 +09:00

331 lines
8.3 KiB
TypeScript

import { describe, it, expect, beforeEach } from "bun:test"
import { ContextCollector } from "./collector"
import type { ContextPriority, ContextSourceType } from "./types"
describe("ContextCollector", () => {
let collector: ContextCollector
beforeEach(() => {
collector = new ContextCollector()
})
describe("register", () => {
it("registers context for a session", () => {
// given
const sessionID = "ses_test1"
const options = {
id: "ulw-context",
source: "keyword-detector" as ContextSourceType,
content: "Ultrawork mode activated",
}
// when
collector.register(sessionID, options)
// then
const pending = collector.getPending(sessionID)
expect(pending.hasContent).toBe(true)
expect(pending.entries).toHaveLength(1)
expect(pending.entries[0].content).toBe("Ultrawork mode activated")
})
it("assigns default priority of 'normal' when not specified", () => {
// given
const sessionID = "ses_test2"
// when
collector.register(sessionID, {
id: "test",
source: "keyword-detector",
content: "test content",
})
// then
const pending = collector.getPending(sessionID)
expect(pending.entries[0].priority).toBe("normal")
})
it("uses specified priority", () => {
// given
const sessionID = "ses_test3"
// when
collector.register(sessionID, {
id: "critical-context",
source: "keyword-detector",
content: "critical content",
priority: "critical",
})
// then
const pending = collector.getPending(sessionID)
expect(pending.entries[0].priority).toBe("critical")
})
it("deduplicates by source + id combination", () => {
// given
const sessionID = "ses_test4"
const options = {
id: "ulw-context",
source: "keyword-detector" as ContextSourceType,
content: "First content",
}
// when
collector.register(sessionID, options)
collector.register(sessionID, { ...options, content: "Updated content" })
// then
const pending = collector.getPending(sessionID)
expect(pending.entries).toHaveLength(1)
expect(pending.entries[0].content).toBe("Updated content")
})
it("allows same id from different sources", () => {
// given
const sessionID = "ses_test5"
// when
collector.register(sessionID, {
id: "context-1",
source: "keyword-detector",
content: "From keyword-detector",
})
collector.register(sessionID, {
id: "context-1",
source: "rules-injector",
content: "From rules-injector",
})
// then
const pending = collector.getPending(sessionID)
expect(pending.entries).toHaveLength(2)
})
})
describe("getPending", () => {
it("returns empty result for session with no context", () => {
// given
const sessionID = "ses_empty"
// when
const pending = collector.getPending(sessionID)
// then
expect(pending.hasContent).toBe(false)
expect(pending.entries).toHaveLength(0)
expect(pending.merged).toBe("")
})
it("merges multiple contexts with separator", () => {
// given
const sessionID = "ses_merge"
collector.register(sessionID, {
id: "ctx-1",
source: "keyword-detector",
content: "First context",
})
collector.register(sessionID, {
id: "ctx-2",
source: "rules-injector",
content: "Second context",
})
// when
const pending = collector.getPending(sessionID)
// then
expect(pending.hasContent).toBe(true)
expect(pending.merged).toContain("First context")
expect(pending.merged).toContain("Second context")
})
it("orders contexts by priority (critical > high > normal > low)", () => {
// given
const sessionID = "ses_priority"
collector.register(sessionID, {
id: "low",
source: "custom",
content: "LOW",
priority: "low",
})
collector.register(sessionID, {
id: "critical",
source: "custom",
content: "CRITICAL",
priority: "critical",
})
collector.register(sessionID, {
id: "normal",
source: "custom",
content: "NORMAL",
priority: "normal",
})
collector.register(sessionID, {
id: "high",
source: "custom",
content: "HIGH",
priority: "high",
})
// when
const pending = collector.getPending(sessionID)
// then
const order = pending.entries.map((e) => e.priority)
expect(order).toEqual(["critical", "high", "normal", "low"])
})
it("maintains registration order within same priority", () => {
// given
const sessionID = "ses_order"
collector.register(sessionID, {
id: "first",
source: "custom",
content: "First",
priority: "normal",
})
collector.register(sessionID, {
id: "second",
source: "custom",
content: "Second",
priority: "normal",
})
collector.register(sessionID, {
id: "third",
source: "custom",
content: "Third",
priority: "normal",
})
// when
const pending = collector.getPending(sessionID)
// then
const ids = pending.entries.map((e) => e.id)
expect(ids).toEqual(["first", "second", "third"])
})
})
describe("consume", () => {
it("clears pending context for session", () => {
// given
const sessionID = "ses_consume"
collector.register(sessionID, {
id: "ctx",
source: "keyword-detector",
content: "test",
})
// when
collector.consume(sessionID)
// then
const pending = collector.getPending(sessionID)
expect(pending.hasContent).toBe(false)
})
it("returns the consumed context", () => {
// given
const sessionID = "ses_consume_return"
collector.register(sessionID, {
id: "ctx",
source: "keyword-detector",
content: "test content",
})
// when
const consumed = collector.consume(sessionID)
// then
expect(consumed.hasContent).toBe(true)
expect(consumed.entries[0].content).toBe("test content")
})
it("does not affect other sessions", () => {
// given
const session1 = "ses_1"
const session2 = "ses_2"
collector.register(session1, {
id: "ctx",
source: "keyword-detector",
content: "session 1",
})
collector.register(session2, {
id: "ctx",
source: "keyword-detector",
content: "session 2",
})
// when
collector.consume(session1)
// then
expect(collector.getPending(session1).hasContent).toBe(false)
expect(collector.getPending(session2).hasContent).toBe(true)
})
})
describe("clear", () => {
it("removes all context for a session", () => {
// given
const sessionID = "ses_clear"
collector.register(sessionID, {
id: "ctx-1",
source: "keyword-detector",
content: "test 1",
})
collector.register(sessionID, {
id: "ctx-2",
source: "rules-injector",
content: "test 2",
})
// when
collector.clear(sessionID)
// then
expect(collector.getPending(sessionID).hasContent).toBe(false)
})
})
describe("hasPending", () => {
it("returns true when session has pending context", () => {
// given
const sessionID = "ses_has"
collector.register(sessionID, {
id: "ctx",
source: "keyword-detector",
content: "test",
})
// when / #then
expect(collector.hasPending(sessionID)).toBe(true)
})
it("returns false when session has no pending context", () => {
// given
const sessionID = "ses_empty"
// when / #then
expect(collector.hasPending(sessionID)).toBe(false)
})
it("returns false after consume", () => {
// given
const sessionID = "ses_after_consume"
collector.register(sessionID, {
id: "ctx",
source: "keyword-detector",
content: "test",
})
// when
collector.consume(sessionID)
// then
expect(collector.hasPending(sessionID)).toBe(false)
})
})
})