6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
183 lines
5.2 KiB
TypeScript
183 lines
5.2 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { describe, expect, it } from 'vitest'
|
|
import type { ColumnDefinition, RowData, TableSchema } from '@/lib/table/types'
|
|
import {
|
|
coerceRowToSchema,
|
|
resolveSelectOptionId,
|
|
validateColumnDefinition,
|
|
validateRowAgainstSchema,
|
|
} from '@/lib/table/validation'
|
|
|
|
const selectColumn: ColumnDefinition = {
|
|
id: 'col_status',
|
|
name: 'status',
|
|
type: 'select',
|
|
options: [
|
|
{ id: 'opt_open', name: 'Open' },
|
|
{ id: 'opt_closed', name: 'Closed' },
|
|
],
|
|
}
|
|
|
|
const multiselectColumn: ColumnDefinition = {
|
|
id: 'col_tags',
|
|
name: 'tags',
|
|
type: 'select',
|
|
multiple: true,
|
|
options: [
|
|
{ id: 'opt_a', name: 'Alpha' },
|
|
{ id: 'opt_b', name: 'Beta' },
|
|
],
|
|
}
|
|
|
|
function schemaWith(...columns: ColumnDefinition[]): TableSchema {
|
|
return { columns }
|
|
}
|
|
|
|
describe('validateRowAgainstSchema — select', () => {
|
|
it('accepts a value matching an option id', () => {
|
|
expect(
|
|
validateRowAgainstSchema({ col_status: 'opt_open' }, schemaWith(selectColumn)).valid
|
|
).toBe(true)
|
|
})
|
|
|
|
it('rejects a value that is not a declared option id', () => {
|
|
expect(
|
|
validateRowAgainstSchema({ col_status: 'opt_unknown' }, schemaWith(selectColumn)).valid
|
|
).toBe(false)
|
|
})
|
|
|
|
it('rejects a non-string value', () => {
|
|
const result = validateRowAgainstSchema(
|
|
{ col_status: 123 } as unknown as RowData,
|
|
schemaWith(selectColumn)
|
|
)
|
|
expect(result.valid).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('validateRowAgainstSchema — multiselect', () => {
|
|
it('accepts an array of valid option ids', () => {
|
|
expect(
|
|
validateRowAgainstSchema({ col_tags: ['opt_a', 'opt_b'] }, schemaWith(multiselectColumn))
|
|
.valid
|
|
).toBe(true)
|
|
})
|
|
|
|
it('rejects an array containing an unknown id', () => {
|
|
expect(
|
|
validateRowAgainstSchema({ col_tags: ['opt_a', 'nope'] }, schemaWith(multiselectColumn)).valid
|
|
).toBe(false)
|
|
})
|
|
|
|
it('rejects a non-array value', () => {
|
|
expect(
|
|
validateRowAgainstSchema({ col_tags: 'opt_a' }, schemaWith(multiselectColumn)).valid
|
|
).toBe(false)
|
|
})
|
|
|
|
it('rejects an empty array when required', () => {
|
|
const result = validateRowAgainstSchema(
|
|
{ col_tags: [] },
|
|
schemaWith({ ...multiselectColumn, required: true })
|
|
)
|
|
expect(result.valid).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('coerceRowToSchema — select', () => {
|
|
it('maps an option name to its id', () => {
|
|
const data: RowData = { col_status: 'Open' }
|
|
const result = coerceRowToSchema(data, schemaWith(selectColumn))
|
|
expect(result.valid).toBe(true)
|
|
expect(data.col_status).toBe('opt_open')
|
|
})
|
|
|
|
it('maps an option name case-insensitively', () => {
|
|
const data: RowData = { col_status: 'closed' }
|
|
coerceRowToSchema(data, schemaWith(selectColumn))
|
|
expect(data.col_status).toBe('opt_closed')
|
|
})
|
|
|
|
it('nulls an unmatched value on an optional column', () => {
|
|
const data: RowData = { col_status: 'banana' }
|
|
const result = coerceRowToSchema(data, schemaWith(selectColumn))
|
|
expect(result.valid).toBe(true)
|
|
expect(data.col_status).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('coerceRowToSchema — multiselect', () => {
|
|
it('resolves names and drops unmatched entries', () => {
|
|
const data: RowData = { col_tags: ['Alpha', 'opt_b', 'ghost'] }
|
|
const result = coerceRowToSchema(data, schemaWith(multiselectColumn))
|
|
expect(result.valid).toBe(true)
|
|
expect(data.col_tags).toEqual(['opt_a', 'opt_b'])
|
|
})
|
|
|
|
it('wraps a single string into a one-element array', () => {
|
|
const data: RowData = { col_tags: 'opt_a' as unknown as string[] }
|
|
coerceRowToSchema(data, schemaWith(multiselectColumn))
|
|
expect(data.col_tags).toEqual(['opt_a'])
|
|
})
|
|
})
|
|
|
|
describe('resolveSelectOptionId', () => {
|
|
const options = selectColumn.options ?? []
|
|
|
|
it('resolves a stable id', () => {
|
|
expect(resolveSelectOptionId('opt_open', options)).toBe('opt_open')
|
|
})
|
|
|
|
it('resolves a display name (case-insensitively)', () => {
|
|
expect(resolveSelectOptionId('closed', options)).toBe('opt_closed')
|
|
})
|
|
|
|
it('returns null for an unknown value (drives the type-conversion compatibility gate)', () => {
|
|
expect(resolveSelectOptionId('nope', options)).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('validateColumnDefinition — select options', () => {
|
|
it('accepts a well-formed select column', () => {
|
|
expect(validateColumnDefinition(selectColumn).valid).toBe(true)
|
|
})
|
|
|
|
it('requires at least one option', () => {
|
|
expect(validateColumnDefinition({ ...selectColumn, options: [] }).valid).toBe(false)
|
|
})
|
|
|
|
it('rejects duplicate option ids', () => {
|
|
const result = validateColumnDefinition({
|
|
...selectColumn,
|
|
options: [
|
|
{ id: 'dup', name: 'One' },
|
|
{ id: 'dup', name: 'Two' },
|
|
],
|
|
})
|
|
expect(result.valid).toBe(false)
|
|
})
|
|
|
|
it('rejects duplicate option names', () => {
|
|
const result = validateColumnDefinition({
|
|
...selectColumn,
|
|
options: [
|
|
{ id: 'a', name: 'Same' },
|
|
{ id: 'b', name: 'same' },
|
|
],
|
|
})
|
|
expect(result.valid).toBe(false)
|
|
})
|
|
|
|
it('rejects options on a non-select column', () => {
|
|
const result = validateColumnDefinition({
|
|
id: 'c',
|
|
name: 'plain',
|
|
type: 'string',
|
|
options: [{ id: 'a', name: 'One' }],
|
|
})
|
|
expect(result.valid).toBe(false)
|
|
})
|
|
})
|