Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/airtable

test/models.test.ts

50 lines · 1.6 KB · TypeScript
import assert from 'node:assert/strict'
import test from 'node:test'
import { mapComment, mapRecord, mapTable } from '../src/models.ts'
import { requireConfirmOrDryRun } from '../src/types.ts'

test('mapComment drops author email', () => {
	const comment = mapComment({
		id: 'comX',
		text: 'Hello',
		createdTime: '2026-01-01T00:00:00.000Z',
		author: { id: 'usrX', name: 'Ada', email: 'hidden@example.com' },
	})
	assert.equal(comment.id, 'comX')
	assert.deepEqual(comment.author, { id: 'usrX', name: 'Ada' })
	assert.equal(JSON.stringify(comment).includes('hidden@example.com'), false)
})

test('mapRecord keeps fields and ignores extra keys', () => {
	const record = mapRecord({
		id: 'recX',
		createdTime: '2026-01-01T00:00:00.000Z',
		commentCount: 2,
		fields: { Name: 'Task' },
	})
	assert.deepEqual(record, {
		id: 'recX',
		createdTime: '2026-01-01T00:00:00.000Z',
		commentCount: 2,
		fields: { Name: 'Task' },
	})
})

test('mapTable projects field and view counts', () => {
	const table = mapTable({
		id: 'tblX',
		name: 'Tasks',
		primaryFieldId: 'fldX',
		fields: [{ id: 'fldX', name: 'Name', type: 'singleLineText' }],
		views: [{ id: 'viwX', name: 'Grid', type: 'grid' }],
	})
	assert.equal(table.fieldCount, 1)
	assert.equal(table.viewCount, 1)
	assert.equal(table.fields[0]?.type, 'singleLineText')
})

test('requireConfirmOrDryRun guards writes', () => {
	assert.equal(requireConfirmOrDryRun({ dryRun: true }, 'Creating'), 'dryRun')
	assert.equal(requireConfirmOrDryRun({ confirm: true }, 'Creating'), 'confirm')
	assert.throws(() => requireConfirmOrDryRun({}, 'Creating an Airtable record'))
})