|
1 | 1 | import { COMMAND_SCHEMAS, type Command, type CommandAttrs, deriveCommandGroup } from '../command-run'; |
2 | 2 | import { ResourceAttributesSchema } from '../common-attributes'; |
3 | | -import { CommandResultSchema } from '../common-shapes'; |
| 3 | +import { CommandResultSchema, resilientParse } from '../common-shapes'; |
4 | 4 | import { describe, expect, expectTypeOf, it } from 'vitest'; |
5 | 5 | import { z } from 'zod'; |
6 | 6 |
|
@@ -170,3 +170,55 @@ describe('type safety', () => { |
170 | 170 | } |
171 | 171 | }); |
172 | 172 | }); |
| 173 | + |
| 174 | +describe('resilientParse', () => { |
| 175 | + it('passes valid attrs through unchanged', () => { |
| 176 | + const attrs = { |
| 177 | + language: 'python', |
| 178 | + framework: 'strands', |
| 179 | + model_provider: 'bedrock', |
| 180 | + memory: 'shortterm', |
| 181 | + protocol: 'mcp', |
| 182 | + build: 'codezip', |
| 183 | + agent_type: 'create', |
| 184 | + network_mode: 'public', |
| 185 | + has_agent: true, |
| 186 | + }; |
| 187 | + expect(resilientParse(COMMAND_SCHEMAS.create, attrs)).toEqual(attrs); |
| 188 | + }); |
| 189 | + |
| 190 | + it('defaults a single invalid enum field to unknown', () => { |
| 191 | + const attrs = { |
| 192 | + language: 'rust', // invalid |
| 193 | + framework: 'strands', |
| 194 | + model_provider: 'bedrock', |
| 195 | + memory: 'shortterm', |
| 196 | + protocol: 'mcp', |
| 197 | + build: 'codezip', |
| 198 | + agent_type: 'create', |
| 199 | + network_mode: 'public', |
| 200 | + has_agent: true, |
| 201 | + }; |
| 202 | + const result = resilientParse(COMMAND_SCHEMAS.create, attrs); |
| 203 | + expect(result.language).toBe('unknown'); |
| 204 | + expect(result.framework).toBe('strands'); |
| 205 | + }); |
| 206 | + |
| 207 | + it('defaults missing required fields to unknown', () => { |
| 208 | + const result = resilientParse(COMMAND_SCHEMAS.create, { language: 'python' }); |
| 209 | + expect(result.language).toBe('python'); |
| 210 | + expect(result.framework).toBe('unknown'); |
| 211 | + expect(result.model_provider).toBe('unknown'); |
| 212 | + }); |
| 213 | + |
| 214 | + it('defaults all fields to unknown when all are invalid', () => { |
| 215 | + const result = resilientParse(COMMAND_SCHEMAS.create, {}); |
| 216 | + for (const value of Object.values(result)) { |
| 217 | + expect(value).toBe('unknown'); |
| 218 | + } |
| 219 | + }); |
| 220 | + |
| 221 | + it('returns empty object for no-attrs schemas', () => { |
| 222 | + expect(resilientParse(COMMAND_SCHEMAS['telemetry.disable'], {})).toEqual({}); |
| 223 | + }); |
| 224 | +}); |
0 commit comments