|
| 1 | +import { |
| 2 | + $MI, |
| 3 | + MI, |
| 4 | +} from "@nhsdigital/nhs-notify-event-schemas-supplier-api/src/domain/mi"; |
| 5 | + |
| 6 | +describe("MI schema validation", () => { |
| 7 | + const validMIEvent: MI = { |
| 8 | + id: "mi-001", |
| 9 | + lineItem: "LETTER_PRINT_A4", |
| 10 | + timestamp: "2025-11-16T10:30:00.000Z", |
| 11 | + quantity: 150, |
| 12 | + specificationId: "spec-123", |
| 13 | + groupId: "group-456", |
| 14 | + stockRemaining: 1000, |
| 15 | + supplierId: "supplier-789", |
| 16 | + createdAt: "2025-11-16T10:30:00.000Z", |
| 17 | + updatedAt: "2025-11-16T10:30:00.000Z", |
| 18 | + }; |
| 19 | + |
| 20 | + describe("basic validation", () => { |
| 21 | + it("should validate a valid MI event with all fields", () => { |
| 22 | + const result = $MI.safeParse(validMIEvent); |
| 23 | + expect(result.success).toBe(true); |
| 24 | + expect(result.error).toBeUndefined(); |
| 25 | + expect(result.data).toEqual(validMIEvent); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should validate an MI event without optional fields", () => { |
| 29 | + const minimalMI = { |
| 30 | + id: "mi-002", |
| 31 | + lineItem: "LETTER_PRINT_A5", |
| 32 | + timestamp: "2025-11-16T11:00:00.000Z", |
| 33 | + quantity: 75, |
| 34 | + supplierId: "supplier-101", |
| 35 | + createdAt: "2025-11-16T11:00:00.000Z", |
| 36 | + updatedAt: "2025-11-16T11:00:00.000Z", |
| 37 | + }; |
| 38 | + |
| 39 | + const result = $MI.safeParse(minimalMI); |
| 40 | + expect(result.success).toBe(true); |
| 41 | + const data = result.data as MI; |
| 42 | + expect(data.specificationId).toBeUndefined(); |
| 43 | + expect(data.groupId).toBeUndefined(); |
| 44 | + expect(data.stockRemaining).toBeUndefined(); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("field validation", () => { |
| 49 | + it("should reject MI event missing required field 'id'", () => { |
| 50 | + const invalidMI = { ...validMIEvent }; |
| 51 | + delete (invalidMI as any).id; |
| 52 | + |
| 53 | + const result = $MI.safeParse(invalidMI); |
| 54 | + expect(result.success).toBe(false); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should reject MI event missing required field 'lineItem'", () => { |
| 58 | + const invalidMI = { ...validMIEvent }; |
| 59 | + delete (invalidMI as any).lineItem; |
| 60 | + |
| 61 | + const result = $MI.safeParse(invalidMI); |
| 62 | + expect(result.success).toBe(false); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should reject MI event missing required field 'timestamp'", () => { |
| 66 | + const invalidMI = { ...validMIEvent }; |
| 67 | + delete (invalidMI as any).timestamp; |
| 68 | + |
| 69 | + const result = $MI.safeParse(invalidMI); |
| 70 | + expect(result.success).toBe(false); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should reject MI event missing required field 'quantity'", () => { |
| 74 | + const invalidMI = { ...validMIEvent }; |
| 75 | + delete (invalidMI as any).quantity; |
| 76 | + |
| 77 | + const result = $MI.safeParse(invalidMI); |
| 78 | + expect(result.success).toBe(false); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should reject MI event missing required field 'supplierId'", () => { |
| 82 | + const invalidMI = { ...validMIEvent }; |
| 83 | + delete (invalidMI as any).supplierId; |
| 84 | + |
| 85 | + const result = $MI.safeParse(invalidMI); |
| 86 | + expect(result.success).toBe(false); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should reject MI event with invalid quantity type", () => { |
| 90 | + const invalidMI = { |
| 91 | + ...validMIEvent, |
| 92 | + quantity: "not-a-number", |
| 93 | + }; |
| 94 | + |
| 95 | + const result = $MI.safeParse(invalidMI); |
| 96 | + expect(result.success).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should reject MI event with invalid stockRemaining type", () => { |
| 100 | + const invalidMI = { |
| 101 | + ...validMIEvent, |
| 102 | + stockRemaining: "not-a-number", |
| 103 | + }; |
| 104 | + |
| 105 | + const result = $MI.safeParse(invalidMI); |
| 106 | + expect(result.success).toBe(false); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe("testData examples", () => { |
| 111 | + it("should parse a letter print MI event", () => { |
| 112 | + const letterPrintMI = { |
| 113 | + id: "mi-letter-001", |
| 114 | + lineItem: "LETTER_PRINT_A4_COLOR", |
| 115 | + timestamp: "2025-11-16T14:00:00.000Z", |
| 116 | + quantity: 250, |
| 117 | + specificationId: "letter-spec-001", |
| 118 | + groupId: "batch-001", |
| 119 | + supplierId: "supplier-abc", |
| 120 | + createdAt: "2025-11-16T14:00:00.000Z", |
| 121 | + updatedAt: "2025-11-16T14:00:00.000Z", |
| 122 | + }; |
| 123 | + |
| 124 | + const result = $MI.safeParse(letterPrintMI); |
| 125 | + expect(result.success).toBe(true); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should parse an envelope usage MI event", () => { |
| 129 | + const envelopeMI = { |
| 130 | + id: "mi-envelope-001", |
| 131 | + lineItem: "ENVELOPE_DL", |
| 132 | + timestamp: "2025-11-16T15:00:00.000Z", |
| 133 | + quantity: 300, |
| 134 | + stockRemaining: 2500, |
| 135 | + supplierId: "supplier-xyz", |
| 136 | + createdAt: "2025-11-16T15:00:00.000Z", |
| 137 | + updatedAt: "2025-11-16T15:00:00.000Z", |
| 138 | + }; |
| 139 | + |
| 140 | + const result = $MI.safeParse(envelopeMI); |
| 141 | + expect(result.success).toBe(true); |
| 142 | + const data = result.data as MI; |
| 143 | + expect(data.stockRemaining).toBe(2500); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should parse a postage MI event", () => { |
| 147 | + const postageMI = { |
| 148 | + id: "mi-postage-001", |
| 149 | + lineItem: "POSTAGE_FIRST_CLASS", |
| 150 | + timestamp: "2025-11-16T16:00:00.000Z", |
| 151 | + quantity: 500, |
| 152 | + groupId: "daily-batch-16-11-2025", |
| 153 | + supplierId: "supplier-123", |
| 154 | + createdAt: "2025-11-16T16:00:00.000Z", |
| 155 | + updatedAt: "2025-11-16T16:00:00.000Z", |
| 156 | + }; |
| 157 | + |
| 158 | + const result = $MI.safeParse(postageMI); |
| 159 | + expect(result.success).toBe(true); |
| 160 | + const data = result.data as MI; |
| 161 | + expect(data.lineItem).toBe("POSTAGE_FIRST_CLASS"); |
| 162 | + expect(data.groupId).toBe("daily-batch-16-11-2025"); |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments