|
| 1 | +import type OpenAI from "openai" |
| 2 | +import { apply_diff } from "../apply_diff" |
| 3 | + |
| 4 | +// Helper type to access function tools |
| 5 | +type FunctionTool = OpenAI.Chat.ChatCompletionTool & { type: "function" } |
| 6 | + |
| 7 | +// Helper to get function definition from tool |
| 8 | +const getFunctionDef = (tool: OpenAI.Chat.ChatCompletionTool) => (tool as FunctionTool).function |
| 9 | + |
| 10 | +// OpenAI types the parameter schema as FunctionParameters (Record<string, unknown>). |
| 11 | +// Narrow it with runtime checks instead of assertions so the spec tracks schema changes. |
| 12 | +function asRecord(value: unknown): Record<string, unknown> { |
| 13 | + if (typeof value !== "object" || value === null) { |
| 14 | + throw new Error("expected an object value in the apply_diff parameter schema") |
| 15 | + } |
| 16 | + return value as Record<string, unknown> |
| 17 | +} |
| 18 | + |
| 19 | +const getDiffDescription = (): string => { |
| 20 | + const parameters = asRecord(getFunctionDef(apply_diff).parameters) |
| 21 | + const properties = asRecord(parameters["properties"]) |
| 22 | + const diff = asRecord(properties["diff"]) |
| 23 | + const description = diff["description"] |
| 24 | + if (typeof description !== "string") { |
| 25 | + throw new Error("diff parameter must declare a string description") |
| 26 | + } |
| 27 | + return description |
| 28 | +} |
| 29 | + |
| 30 | +const getRequiredParameters = (): unknown[] => { |
| 31 | + const parameters = asRecord(getFunctionDef(apply_diff).parameters) |
| 32 | + const required = parameters["required"] |
| 33 | + if (!Array.isArray(required)) { |
| 34 | + throw new Error("apply_diff parameters must declare a required array") |
| 35 | + } |
| 36 | + return required |
| 37 | +} |
| 38 | + |
| 39 | +describe("apply_diff tool description", () => { |
| 40 | + const description = getFunctionDef(apply_diff).description |
| 41 | + |
| 42 | + describe("read_file line-number format", () => { |
| 43 | + it("should document line numbers using the actual read_file output format (number, spaces, pipe)", () => { |
| 44 | + // The real format comes from addLineNumbers() in extract-text.ts: "N | content" |
| 45 | + expect(description).toContain("'1 | '") |
| 46 | + expect(description).toContain("'2 | '") |
| 47 | + }) |
| 48 | + |
| 49 | + it("should not describe read_file line numbers with a colon suffix", () => { |
| 50 | + // Regression guard: read_file never outputs "N: " prefixes (extract-text.ts addLineNumbers), |
| 51 | + // and the server-side auto-strip in multi-search-replace.ts only keys on the "N | " pattern. |
| 52 | + expect(description).not.toContain("'1: '") |
| 53 | + expect(description).not.toContain("'2: '") |
| 54 | + }) |
| 55 | + |
| 56 | + it("should instruct the model to strip line numbers from the SEARCH block", () => { |
| 57 | + expect(description).toContain("MUST STRIP all line numbers from the 'SEARCH' block") |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + describe("diff parameter description", () => { |
| 62 | + const diffDescription = getDiffDescription() |
| 63 | + |
| 64 | + it("should use the actual read_file output format in the line-number prohibition bullet", () => { |
| 65 | + expect(diffDescription).toContain("'137 | '") |
| 66 | + expect(diffDescription).not.toContain("'137: '") |
| 67 | + }) |
| 68 | + |
| 69 | + it("should keep the template placeholder free of line numbers", () => { |
| 70 | + expect(diffDescription).toContain("[exact content to find WITHOUT line numbers]") |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + describe("schema", () => { |
| 75 | + it("should require path and diff parameters", () => { |
| 76 | + expect(getRequiredParameters()).toEqual(["path", "diff"]) |
| 77 | + }) |
| 78 | + }) |
| 79 | +}) |
0 commit comments