|
| 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 | + id: { type: "string" }, |
| 8 | + query: { type: "string" }, |
| 9 | + note: { type: "string", minLength: 0 }, |
| 10 | + customView: { type: "string", minLength: 1 }, |
| 11 | + view: { anyOf: [{ type: "string", minLength: 1 }, { type: "null" }] }, |
| 12 | + label: { oneOf: [{ type: "string", minLength: 1 }, { type: "string" }] }, |
| 13 | + state: { type: "string", enum: ["open", "closed"] }, |
| 14 | + stage: { type: "string", enum: ["", "planned"] }, |
| 15 | + scope: { type: "string", const: "all" }, |
| 16 | + slug: { type: "string", pattern: "^[a-z]+$" }, |
| 17 | + suffix: { type: "string", pattern: "[a-z]*" }, |
| 18 | + malformed: { type: "string", pattern: "(" }, |
| 19 | + fields: { type: "array", items: { type: "string" } }, |
| 20 | + labels: { type: "array", items: { type: "string" }, minItems: 1 }, |
| 21 | + team: { type: "string" }, |
| 22 | + assignee: { type: ["string", "null"] }, |
| 23 | + parentId: { anyOf: [{ type: "string" }, { type: "null" }] }, |
| 24 | + cycle: { type: "string", nullable: true }, |
| 25 | + priority: { type: "number" }, |
| 26 | + includeArchived: { type: "boolean" }, |
| 27 | + anything: {}, |
| 28 | + }, |
| 29 | + required: ["id"], |
| 30 | +}; |
| 31 | + |
| 32 | +describe("stripPlaceholderArguments", () => { |
| 33 | + test("forwards empty and whitespace strings the property schema permits", () => { |
| 34 | + const args = { query: "", note: "", team: " ", suffix: "", stage: "" }; |
| 35 | + expect(stripPlaceholderArguments(args, schema)).toEqual(args); |
| 36 | + }); |
| 37 | + |
| 38 | + test("drops an empty or whitespace string where minLength is at least 1", () => { |
| 39 | + expect( |
| 40 | + stripPlaceholderArguments({ customView: " ", query: "x" }, schema), |
| 41 | + ).toEqual({ query: "x" }); |
| 42 | + expect(stripPlaceholderArguments({ customView: "" }, schema)).toEqual({}); |
| 43 | + }); |
| 44 | + |
| 45 | + test("drops an empty string that an enum, const, or pattern rejects", () => { |
| 46 | + expect( |
| 47 | + stripPlaceholderArguments({ state: "", scope: " ", slug: "" }, schema), |
| 48 | + ).toEqual({}); |
| 49 | + }); |
| 50 | + |
| 51 | + test("forwards an empty string when the pattern does not compile", () => { |
| 52 | + expect(stripPlaceholderArguments({ malformed: "" }, schema)).toEqual({ |
| 53 | + malformed: "", |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + test("drops an empty array only where minItems is at least 1", () => { |
| 58 | + expect( |
| 59 | + stripPlaceholderArguments({ fields: [], labels: [] }, schema), |
| 60 | + ).toEqual({ fields: [] }); |
| 61 | + }); |
| 62 | + |
| 63 | + test("drops an empty value the property type does not admit", () => { |
| 64 | + expect( |
| 65 | + stripPlaceholderArguments( |
| 66 | + { priority: "", includeArchived: " ", query: [] }, |
| 67 | + schema, |
| 68 | + ), |
| 69 | + ).toEqual({}); |
| 70 | + }); |
| 71 | + |
| 72 | + test("drops null only where the property schema disallows it", () => { |
| 73 | + expect( |
| 74 | + stripPlaceholderArguments({ team: null, priority: null }, schema), |
| 75 | + ).toEqual({}); |
| 76 | + const nullable = { |
| 77 | + assignee: null, |
| 78 | + parentId: null, |
| 79 | + cycle: null, |
| 80 | + anything: null, |
| 81 | + }; |
| 82 | + expect(stripPlaceholderArguments(nullable, schema)).toEqual(nullable); |
| 83 | + }); |
| 84 | + |
| 85 | + test("drops a composite value only when every branch rejects it", () => { |
| 86 | + expect(stripPlaceholderArguments({ view: " " }, schema)).toEqual({}); |
| 87 | + expect(stripPlaceholderArguments({ view: null }, schema)).toEqual({ |
| 88 | + view: null, |
| 89 | + }); |
| 90 | + expect(stripPlaceholderArguments({ label: "" }, schema)).toEqual({ |
| 91 | + label: "", |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + test("never changes required arguments", () => { |
| 96 | + expect(stripPlaceholderArguments({ id: "" }, schema)).toEqual({ id: "" }); |
| 97 | + expect(stripPlaceholderArguments({ id: null }, schema)).toEqual({ |
| 98 | + id: null, |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + test("keeps zero and false", () => { |
| 103 | + const args = { priority: 0, includeArchived: false }; |
| 104 | + expect(stripPlaceholderArguments(args, schema)).toEqual(args); |
| 105 | + }); |
| 106 | + |
| 107 | + test("keeps meaningful values and leaves the caller's object intact", () => { |
| 108 | + const args = { query: "release", fields: ["title"], customView: "" }; |
| 109 | + expect(stripPlaceholderArguments(args, schema)).toEqual({ |
| 110 | + query: "release", |
| 111 | + fields: ["title"], |
| 112 | + }); |
| 113 | + expect(args).toEqual({ |
| 114 | + query: "release", |
| 115 | + fields: ["title"], |
| 116 | + customView: "", |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + test("forwards a key the schema does not declare", () => { |
| 121 | + expect( |
| 122 | + stripPlaceholderArguments({ id: "CL-1", extra: "" }, schema), |
| 123 | + ).toEqual({ id: "CL-1", extra: "" }); |
| 124 | + }); |
| 125 | + |
| 126 | + test("returns missing or non-object arguments as-is", () => { |
| 127 | + expect(stripPlaceholderArguments(null, schema)).toBeNull(); |
| 128 | + expect(stripPlaceholderArguments(undefined, schema)).toBeUndefined(); |
| 129 | + expect(stripPlaceholderArguments("query", schema)).toBe("query"); |
| 130 | + const list = [""]; |
| 131 | + expect(stripPlaceholderArguments(list, schema)).toBe(list); |
| 132 | + }); |
| 133 | + |
| 134 | + test("forwards arguments unchanged when the schema shape is unusable", () => { |
| 135 | + const args = { query: "" }; |
| 136 | + expect( |
| 137 | + stripPlaceholderArguments(args, { type: "object", required: "query" }), |
| 138 | + ).toEqual(args); |
| 139 | + }); |
| 140 | +}); |
0 commit comments