Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/tui/workspace/operations/operation-input-refusal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, expect, it } from 'vitest'
import { AtcError } from '../../../errors/index.js'
import { failureReport } from '../../../telemetry/index.js'
import { inputObject, parseField } from './operation-model.js'

describe('operation form input refusals', () => {
it('minimum field value is a usage refusal', () => {
const refusals = [
() => parseField({ type: 'number', minimum: 0 }, '-1'),
() => parseField({ type: 'number', maximum: 10 }, '11'),
() => parseField({ type: 'integer' }, '1.5'),
() => parseField({ type: 'array' }, '{}'),
() => parseField({ type: 'object' }, 'not json'),
() => inputObject('[]'),
() => inputObject('not json'),
]
for (const refusal of refusals) {
let thrown: unknown
try {
refusal()
} catch (error) {
thrown = error
}
expect(thrown).toBeInstanceOf(AtcError)
expect((thrown as AtcError).code).toBe('ATC_USAGE')
expect(failureReport(thrown, { component: 'tui', operation: 'workspace.action' })).toBeNull()
}
})
})
24 changes: 17 additions & 7 deletions src/tui/workspace/operations/operation-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { WorkspaceField, WorkspaceOperation } from '../../../contract/index.js'
import { AtcError } from '../../../errors/index.js'
import type { WorkspaceState } from '../state.js'
export interface OperationField {
path: string
Expand All @@ -22,10 +23,17 @@ export function operationFields(operation: WorkspaceOperation): OperationField[]
add(operation.inputSchema.properties, operation.inputSchema.required)
return fields
}
function parseJson(raw: string): unknown {
try {
return JSON.parse(raw)
} catch {
throw new AtcError('ATC_USAGE', 'Enter valid JSON.')
}
}
export function inputObject(raw: string): Record<string, unknown> {
const value: unknown = JSON.parse(raw)
const value: unknown = parseJson(raw)
if (!value || typeof value !== 'object' || Array.isArray(value))
throw new Error('Input must be a JSON object.')
throw new AtcError('ATC_USAGE', 'Input must be a JSON object.')
return value as Record<string, unknown>
}
export function fieldValue(raw: string, path: string): unknown {
Expand All @@ -50,17 +58,19 @@ export function setField(raw: string, path: string, value: unknown): string {
export function parseField(field: WorkspaceField, raw: string): unknown {
if (!raw.trim()) return undefined
if (field.type === 'object' || field.type === 'array') {
const value: unknown = JSON.parse(raw)
const value: unknown = parseJson(raw)
if (!value || typeof value !== 'object' || (field.type === 'array') !== Array.isArray(value))
throw new Error(`Expected ${field.type}.`)
throw new AtcError('ATC_USAGE', `Expected ${field.type}.`)
return value
}
if (field.type === 'number' || field.type === 'integer') {
const value = Number(raw)
if (!Number.isFinite(value) || (field.type === 'integer' && !Number.isInteger(value)))
throw new Error(`Enter a valid ${field.type}.`)
if (field.minimum != null && value < field.minimum) throw new Error(`Minimum: ${field.minimum}`)
if (field.maximum != null && value > field.maximum) throw new Error(`Maximum: ${field.maximum}`)
throw new AtcError('ATC_USAGE', `Enter a valid ${field.type}.`)
if (field.minimum != null && value < field.minimum)
throw new AtcError('ATC_USAGE', `Minimum: ${field.minimum}`)
if (field.maximum != null && value > field.maximum)
throw new AtcError('ATC_USAGE', `Maximum: ${field.maximum}`)
return value
}
return raw
Expand Down
Loading