|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { stripPlaceholderArguments } from "./placeholder-args.js"; |
| 3 | + |
| 4 | +const schema = { |
| 5 | + type: "object", |
| 6 | + properties: { |
| 7 | + query: { type: "string" }, |
| 8 | + customView: { type: "string", minLength: 1 }, |
| 9 | + fields: { type: "array", items: { type: "string" } }, |
| 10 | + team: { type: "string" }, |
| 11 | + assignee: { type: ["string", "null"] }, |
| 12 | + parentId: { anyOf: [{ type: "string" }, { type: "null" }] }, |
| 13 | + cycle: { type: "string", nullable: true }, |
| 14 | + stage: { type: "string", enum: ["", "planned"] }, |
| 15 | + priority: { type: "number" }, |
| 16 | + includeArchived: { type: "boolean" }, |
| 17 | + id: { type: "string" }, |
| 18 | + }, |
| 19 | + required: ["id"], |
| 20 | +}; |
| 21 | + |
| 22 | +describe("stripPlaceholderArguments", () => { |
| 23 | + test("drops optional empty strings, whitespace, empty arrays, and disallowed null", () => { |
| 24 | + expect( |
| 25 | + stripPlaceholderArguments( |
| 26 | + { id: "CL-1", query: "", customView: " ", fields: [], team: null }, |
| 27 | + schema, |
| 28 | + ), |
| 29 | + ).toEqual({ id: "CL-1" }); |
| 30 | + }); |
| 31 | + |
| 32 | + test("never changes required arguments", () => { |
| 33 | + expect(stripPlaceholderArguments({ id: "" }, schema)).toEqual({ id: "" }); |
| 34 | + expect(stripPlaceholderArguments({ id: null }, schema)).toEqual({ |
| 35 | + id: null, |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + test("keeps null where the property schema allows it", () => { |
| 40 | + const args = { assignee: null, parentId: null, cycle: null }; |
| 41 | + expect(stripPlaceholderArguments(args, schema)).toEqual(args); |
| 42 | + }); |
| 43 | + |
| 44 | + test("keeps zero, false, and enum values", () => { |
| 45 | + const args = { priority: 0, includeArchived: false, stage: "" }; |
| 46 | + expect(stripPlaceholderArguments(args, schema)).toEqual(args); |
| 47 | + }); |
| 48 | + |
| 49 | + test("keeps meaningful values and leaves the caller's object intact", () => { |
| 50 | + const args = { query: "release", fields: ["title"], team: "" }; |
| 51 | + expect(stripPlaceholderArguments(args, schema)).toEqual({ |
| 52 | + query: "release", |
| 53 | + fields: ["title"], |
| 54 | + }); |
| 55 | + expect(args).toEqual({ query: "release", fields: ["title"], team: "" }); |
| 56 | + }); |
| 57 | + |
| 58 | + test("treats a key the schema does not declare as optional", () => { |
| 59 | + expect( |
| 60 | + stripPlaceholderArguments({ id: "CL-1", extra: "" }, schema), |
| 61 | + ).toEqual({ id: "CL-1" }); |
| 62 | + }); |
| 63 | + |
| 64 | + test("forwards arguments unchanged when the schema shape is unusable", () => { |
| 65 | + const args = { query: "" }; |
| 66 | + expect( |
| 67 | + stripPlaceholderArguments(args, { type: "object", required: "query" }), |
| 68 | + ).toEqual(args); |
| 69 | + }); |
| 70 | +}); |
0 commit comments