test: session — loaded() and filterCompacted() coverage#395
test: session — loaded() and filterCompacted() coverage#395anandgupta42 wants to merge 1 commit intomainfrom
Conversation
…ted() coverage These two functions had zero test coverage despite being critical for session correctness: loaded() prevents duplicate instruction injection, filterCompacted() controls compaction boundary detection for context window management. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> https://claude.ai/code/session_01TGBJo6pqwxz6qh72s79wCg
There was a problem hiding this comment.
Claude Code Review
This repository is configured for manual code reviews. Comment @claude review to trigger a review.
Tip: disable this comment in your organization's Code Review settings.
📝 WalkthroughWalkthroughTwo test suites were added to enhance coverage for core functionality. The first tests Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
packages/opencode/test/session/message-v2.test.ts (1)
1004-1007: Strengthen ordering assertions to full sequence checks.Line [1004] and Line [1020] currently validate length plus endpoints only; a middle-order regression could still pass.
Suggested assertion tightening
- expect(result.length).toBe(5) - expect(result[0].info.id).toBe("u1") - expect(result[4].info.id).toBe("u3") + expect(result.map((m) => m.info.id)).toStrictEqual(["u1", "a1", "u2", "a2", "u3"])- expect(result.length).toBe(5) - expect(result[0].info.id).toBe("u1") - expect(result[4].info.id).toBe("u3") + expect(result.map((m) => m.info.id)).toStrictEqual(["u1", "a1", "u2", "a2", "u3"])Also applies to: 1020-1023
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/opencode/test/session/message-v2.test.ts` around lines 1004 - 1007, The test only asserts result.length and the first/last ids, which allows middle-order regressions; update the assertions to verify the full ordering by comparing the sequence of ids extracted from result (using result.map(r => r.info.id)) against the expected array of ids so both tests (the block around result.length/asserts and the similar block at lines 1020-1023) assert the entire ordered sequence rather than just endpoints.packages/opencode/test/session/instruction.test.ts (1)
14-108: Optional: consolidate message/part test builders shared across session tests.The helper constructors here overlap with fixture patterns in
packages/opencode/test/session/message-v2.test.ts; extracting a small shared builder module would reduce drift whenMessageV2shapes evolve.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/opencode/test/session/instruction.test.ts` around lines 14 - 108, Extract the overlapping test builders (makeUserMsg, readToolPart, nonReadToolPart) into a small shared test helper module and import it from both session tests; move the implementations out of packages/opencode/test/session/instruction.test.ts into e.g. a new helper that exports makeUserMsg, readToolPart, nonReadToolPart and accepts/sessionID or uses a shared sid fixture so types (MessageV2.WithParts, MessageV2.ToolPart) and utilities (MessageID.make, PartID.make) remain correct; update instruction.test.ts and message-v2.test.ts to import these helpers instead of duplicating logic and run tests to ensure no type regressions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/opencode/test/session/instruction.test.ts`:
- Around line 14-108: Extract the overlapping test builders (makeUserMsg,
readToolPart, nonReadToolPart) into a small shared test helper module and import
it from both session tests; move the implementations out of
packages/opencode/test/session/instruction.test.ts into e.g. a new helper that
exports makeUserMsg, readToolPart, nonReadToolPart and accepts/sessionID or uses
a shared sid fixture so types (MessageV2.WithParts, MessageV2.ToolPart) and
utilities (MessageID.make, PartID.make) remain correct; update
instruction.test.ts and message-v2.test.ts to import these helpers instead of
duplicating logic and run tests to ensure no type regressions.
In `@packages/opencode/test/session/message-v2.test.ts`:
- Around line 1004-1007: The test only asserts result.length and the first/last
ids, which allows middle-order regressions; update the assertions to verify the
full ordering by comparing the sequence of ids extracted from result (using
result.map(r => r.info.id)) against the expected array of ids so both tests (the
block around result.length/asserts and the similar block at lines 1020-1023)
assert the entire ordered sequence rather than just endpoints.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 0b6586ad-8fdd-4cb0-9fc6-e2c6fc7e5090
📒 Files selected for processing (2)
packages/opencode/test/session/instruction.test.tspackages/opencode/test/session/message-v2.test.ts
Merges 9 test PRs (#390, #394, #395, #396, #397, #398, #399, #401, #387) into a single PR, discarding 4 duplicates (#391, #392, #393, #400). **New test coverage:** - `id.test.ts` — 14 tests: Identifier generation, monotonic ordering, timestamp extraction, Zod schema - `shell.test.ts` — 9 tests: shell blacklist enforcement for fish/nu, cache reset, fallback - `path-traversal.test.ts` — 7 tests (new): `Protected.isSensitiveWrite` for .pem/.key, .gnupg, .env variants - `ignore.test.ts` — 7 tests (new): `FileIgnore.match` directory patterns, globs, whitelist overrides - `paths-parsetext.test.ts` — 16 tests: `ConfigPaths.parseText` env/file substitution, JSONC parsing - `finops-formatting.test.ts` — 14 tests: `formatBytes`/`truncateQuery` edge cases - `finops-role-access.test.ts` — 10 tests: RBAC `formatGrants`/`formatHierarchy`/`formatUserRoles` - `tool-lookup.test.ts` — 4 tests: Zod schema introspection for tool parameters - `summary-git-path.test.ts` — 10 tests: `unquoteGitPath` decoding for non-ASCII filenames - `tracing.test.ts` — 2 tests (added): Recap loop detection boundaries - `patch.test.ts` — 9 tests (added): `maybeParseApplyPatchVerified` entry point coverage - `instruction.test.ts` — 13 tests (new): `InstructionPrompt.loaded()` dedup guards - `message-v2.test.ts` — 23 tests (new): `MessageV2.filterCompacted()` boundary detection **Bug fixes (discovered during testing):** - `finops-formatting.ts`: fix `formatBytes` crash on negative/NaN/fractional inputs - `finops-formatting.ts`: fix `truncateQuery` returning empty for whitespace-only, exceeding `maxLen` when < 4 - `training-import.test.ts`: fix pre-existing typecheck errors (missing `context`/`rule` mock keys) - `instruction.test.ts`, `message-v2.test.ts`: fix `MessageV2.Part` cast and branded ID type errors Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
What does this PR do?
1.
InstructionPrompt.loaded()—src/session/instruction.ts(7 new tests)This pure function extracts loaded file paths from session message history, used by
resolve()to prevent duplicate AGENTS.md/CLAUDE.md injection. Zero tests existed. If broken, users get duplicate instructions in their context window — wasting tokens and potentially confusing the model with repeated directives. New coverage includes:metadata.loadedstate.time.compactedguard)loadedmetadata (e.g., bash tool)loadedarray filtered out (defends againstz.any()metadata)2.
MessageV2.filterCompacted()—src/session/message-v2.ts(5 new tests)This async function processes a message stream to find the compaction boundary — the point where old messages were summarized and can be trimmed. Zero tests existed. If broken, compacted sessions either include stale pre-compaction content (context overflow) or lose valid messages. New coverage includes:
summary/finishfieldsmsg.info.error) do NOT mark parent as completed — isolates the!errorguard withsummary: true, finish: "stop"presentType of change
Issue for this PR
N/A — proactive test coverage for session module
How did you verify your code works?
Checklist
https://claude.ai/code/session_01TGBJo6pqwxz6qh72s79wCg
Summary by CodeRabbit