context-build.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { describe, expect, test } from "bun:test";
  2. import { stripHtmlComments, isMemoryFilePath, getLargeMemoryFiles } from "../../src/utils/claudemd";
  3. import { buildEffectiveSystemPrompt } from "../../src/utils/systemPrompt";
  4. import { createTempDir, cleanupTempDir, writeTempFile } from "../mocks/file-system";
  5. // ─── CLAUDE.md Integration with System Prompt ─────────────────────────
  6. describe("Context build: CLAUDE.md + system prompt integration", () => {
  7. test("buildEffectiveSystemPrompt passes through default prompt", () => {
  8. const result = buildEffectiveSystemPrompt({
  9. defaultSystemPrompt: "You are Claude.",
  10. });
  11. // Result is an array of strings (may be split differently)
  12. const joined = Array.from(result).join("");
  13. expect(joined).toBe("You are Claude.");
  14. });
  15. test("buildEffectiveSystemPrompt handles empty prompts", () => {
  16. const result = buildEffectiveSystemPrompt({
  17. defaultSystemPrompt: "",
  18. });
  19. const joined = Array.from(result).join("");
  20. expect(joined).toBe("");
  21. });
  22. test("buildEffectiveSystemPrompt with overrideSystemPrompt replaces everything", () => {
  23. const result = buildEffectiveSystemPrompt({
  24. defaultSystemPrompt: "Default",
  25. overrideSystemPrompt: "Override",
  26. });
  27. const joined = Array.from(result).join("");
  28. expect(joined).toBe("Override");
  29. });
  30. test("buildEffectiveSystemPrompt with customSystemPrompt replaces default", () => {
  31. const result = buildEffectiveSystemPrompt({
  32. defaultSystemPrompt: "Default",
  33. customSystemPrompt: "Custom",
  34. });
  35. const joined = Array.from(result).join("");
  36. expect(joined).toBe("Custom");
  37. });
  38. test("buildEffectiveSystemPrompt with appendSystemPrompt includes both", () => {
  39. const result = buildEffectiveSystemPrompt({
  40. defaultSystemPrompt: "Main prompt",
  41. appendSystemPrompt: "Appended",
  42. });
  43. const joined = Array.from(result).join("");
  44. expect(joined).toContain("Main prompt");
  45. expect(joined).toContain("Appended");
  46. // Appended should come after main
  47. expect(joined.indexOf("Main prompt")).toBeLessThan(
  48. joined.indexOf("Appended")
  49. );
  50. });
  51. });
  52. // ─── CLAUDE.md Discovery with Real File System ───────────────────────
  53. describe("Context build: CLAUDE.md file system integration", () => {
  54. let tempDir: string;
  55. test("strips HTML comments from CLAUDE.md content", () => {
  56. const input = "<!-- this is a comment -->Actual content";
  57. const { content, stripped } = stripHtmlComments(input);
  58. expect(content).toBe("Actual content");
  59. expect(stripped).toBe(true);
  60. });
  61. test("preserves code blocks when stripping HTML comments", () => {
  62. const input = "```\n<!-- not a real comment -->\n```\nReal text";
  63. const { content } = stripHtmlComments(input);
  64. expect(content).toContain("<!-- not a real comment -->");
  65. expect(content).toContain("Real text");
  66. });
  67. test("isMemoryFilePath correctly identifies CLAUDE.md paths", () => {
  68. expect(isMemoryFilePath("/project/CLAUDE.md")).toBe(true);
  69. expect(isMemoryFilePath("/project/CLAUDE.local.md")).toBe(true);
  70. expect(isMemoryFilePath("/project/.claude/rules/file.md")).toBe(true);
  71. expect(isMemoryFilePath("/project/README.md")).toBe(false);
  72. expect(isMemoryFilePath("/project/src/index.ts")).toBe(false);
  73. });
  74. });
  75. // ─── Large Memory File Filtering ──────────────────────────────────────
  76. describe("Context build: large memory file filtering", () => {
  77. test("getLargeMemoryFiles returns empty for empty input", () => {
  78. expect(getLargeMemoryFiles([])).toEqual([]);
  79. });
  80. test("getLargeMemoryFiles returns empty when all files are small", () => {
  81. const files = [
  82. { path: "/a/CLAUDE.md", content: "small" },
  83. { path: "/b/CLAUDE.md", content: "also small" },
  84. ];
  85. expect(getLargeMemoryFiles(files)).toEqual([]);
  86. });
  87. });