|
| 1 | +// Test whitespace-padded key recovery for issue #95407 |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { canonicalizeCronToolObject } from "./cron-tool-canonicalize.js"; |
| 4 | + |
| 5 | +describe("canonicalizeCronToolObject whitespace-padded keys (#95407)", () => { |
| 6 | + it("trims trailing-space keys like 'schedule ' -> 'schedule'", () => { |
| 7 | + const input: Record<string, unknown> = { |
| 8 | + name: "Test", |
| 9 | + "schedule ": { kind: "cron", expr: "0 * * * *", tz: "UTC" }, |
| 10 | + "sessionTarget ": "isolated", |
| 11 | + "payload ": { kind: "agentTurn", message: "hello" }, |
| 12 | + "enabled ": true, |
| 13 | + }; |
| 14 | + |
| 15 | + const result = canonicalizeCronToolObject(input); |
| 16 | + |
| 17 | + // Verify trimmed keys exist |
| 18 | + expect(result).toHaveProperty("schedule"); |
| 19 | + expect(result).toHaveProperty("sessionTarget"); |
| 20 | + expect(result).toHaveProperty("payload"); |
| 21 | + expect(result).toHaveProperty("enabled"); |
| 22 | + expect(result).toHaveProperty("name"); |
| 23 | + |
| 24 | + // Verify untrimmed keys do NOT exist |
| 25 | + expect(Object.keys(result)).not.toContain("schedule "); |
| 26 | + expect(Object.keys(result)).not.toContain("sessionTarget "); |
| 27 | + expect(Object.keys(result)).not.toContain("payload "); |
| 28 | + expect(Object.keys(result)).not.toContain("enabled "); |
| 29 | + |
| 30 | + // Verify no key has trailing spaces |
| 31 | + for (const key of Object.keys(result)) { |
| 32 | + expect(key).toBe(key.trim()); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + it("trims leading-space keys", () => { |
| 37 | + const input: Record<string, unknown> = { |
| 38 | + " schedule": { kind: "at", at: "2026-12-25T00:00:00Z" }, |
| 39 | + " sessionTarget": "main", |
| 40 | + }; |
| 41 | + |
| 42 | + const result = canonicalizeCronToolObject(input); |
| 43 | + |
| 44 | + expect(result).toHaveProperty("schedule"); |
| 45 | + expect(result).toHaveProperty("sessionTarget"); |
| 46 | + expect(Object.keys(result)).not.toContain(" schedule"); |
| 47 | + expect(Object.keys(result)).not.toContain(" sessionTarget"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("passes through already-clean keys unchanged", () => { |
| 51 | + const input: Record<string, unknown> = { |
| 52 | + name: "Holiday Check-in", |
| 53 | + description: "Casual check-in", |
| 54 | + schedule: { kind: "cron", expr: "30 10,20 * * *", tz: "Europe/Madrid" }, |
| 55 | + sessionTarget: "isolated", |
| 56 | + payload: { kind: "agentTurn", message: "hello" }, |
| 57 | + enabled: true, |
| 58 | + }; |
| 59 | + |
| 60 | + const result = canonicalizeCronToolObject(input); |
| 61 | + |
| 62 | + expect(result.schedule).toEqual(input.schedule); |
| 63 | + expect(result.sessionTarget).toBe("isolated"); |
| 64 | + expect(result.payload).toEqual(input.payload); |
| 65 | + expect(result.enabled).toBe(true); |
| 66 | + expect(result.name).toBe("Holiday Check-in"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("handles mixed clean and whitespace-padded keys together", () => { |
| 70 | + const input: Record<string, unknown> = { |
| 71 | + name: "Test", |
| 72 | + "schedule ": { kind: "cron", expr: "0 * * * *" }, |
| 73 | + sessionTarget: "isolated", |
| 74 | + "payload ": { kind: "agentTurn", message: "hi" }, |
| 75 | + enabled: true, |
| 76 | + }; |
| 77 | + |
| 78 | + const result = canonicalizeCronToolObject(input); |
| 79 | + |
| 80 | + expect(result).toHaveProperty("name"); |
| 81 | + expect(result).toHaveProperty("schedule"); |
| 82 | + expect(result).toHaveProperty("sessionTarget"); |
| 83 | + expect(result).toHaveProperty("payload"); |
| 84 | + expect(result).toHaveProperty("enabled"); |
| 85 | + |
| 86 | + for (const key of Object.keys(result)) { |
| 87 | + expect(key).toBe(key.trim()); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + it("wrapping via job property also trims keys", () => { |
| 92 | + const jobValue: Record<string, unknown> = { |
| 93 | + name: "Test", |
| 94 | + "schedule ": { kind: "at", at: "2026-12-25T00:00:00Z" }, |
| 95 | + "sessionTarget ": "isolated", |
| 96 | + "payload ": { kind: "agentTurn", message: "test" }, |
| 97 | + }; |
| 98 | + |
| 99 | + const wrapped = { job: jobValue }; |
| 100 | + const result = canonicalizeCronToolObject(wrapped); |
| 101 | + |
| 102 | + expect(result).toHaveProperty("schedule"); |
| 103 | + expect(result).toHaveProperty("sessionTarget"); |
| 104 | + expect(result).toHaveProperty("payload"); |
| 105 | + for (const key of Object.keys(result)) { |
| 106 | + expect(key).toBe(key.trim()); |
| 107 | + } |
| 108 | + }); |
| 109 | +}); |
0 commit comments