Skip to content

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

Package listing

@kody/airtable

test/setup.test.ts

68 lines · 2.3 KB · TypeScript
import assert from 'node:assert/strict'
import test from 'node:test'
import {
	inferOperationFromPath,
	isMutatingMethod,
	normalizeAirtablePath,
	oauthConnectUrl,
	patSetupUrl,
	recordsPath,
	scopeForOperation,
} from '../src/setup.ts'

test('recordsPath encodes table names', () => {
	assert.equal(recordsPath('appExample', 'Table 1'), '/v0/appExample/Table%201')
	assert.equal(
		recordsPath('appExample', 'tblX', 'recY', 'comments'),
		'/v0/appExample/tblX/recY/comments',
	)
})

test('scopeForOperation maps write comments', () => {
	assert.equal(scopeForOperation('comments.write'), 'data.recordComments:write')
	assert.equal(scopeForOperation('whoami'), null)
	assert.equal(scopeForOperation('bases.read'), 'schema.bases:read')
})

test('inferOperationFromPath classifies REST surfaces', () => {
	assert.equal(inferOperationFromPath('GET', '/v0/meta/whoami'), 'whoami')
	assert.equal(inferOperationFromPath('GET', '/v0/meta/bases'), 'bases.read')
	assert.equal(
		inferOperationFromPath('GET', '/v0/meta/bases/appX/tables'),
		'tables.read',
	)
	assert.equal(inferOperationFromPath('POST', '/v0/appX/tblY'), 'records.write')
	assert.equal(
		inferOperationFromPath('GET', '/v0/appX/tblY/recZ/comments'),
		'comments.read',
	)
})

test('mutating methods', () => {
	assert.equal(isMutatingMethod('GET'), false)
	assert.equal(isMutatingMethod('POST'), true)
	assert.equal(isMutatingMethod('patch'), true)
})

test('setup URLs are prefilled', () => {
	const connect = oauthConnectUrl('airtable')
	assert.match(connect, /^https:\/\/kody\.codes\/connect\/oauth\?/)
	assert.match(connect, /provider=airtable/)
	assert.match(connect, /authorizeUrl=https%3A%2F%2Fairtable.com/)
	assert.match(connect, /allowedHosts=api.airtable.com/)
	assert.match(connect, /tokenExchangeStyle=basic-form/)
	assert.match(connect, /data.records%3Aread/)
	const pat = patSetupUrl('airtablePat')
	assert.match(pat, /^https:\/\/kody\.codes\/account\/secrets\/new\?/)
	assert.match(pat, /name=airtablePat/)
	assert.match(pat, /allowedHosts=api.airtable.com/)
})

test('normalizeAirtablePath stays on api.airtable.com', () => {
	assert.equal(normalizeAirtablePath('/meta/whoami'), '/v0/meta/whoami')
	assert.equal(
		normalizeAirtablePath('https://api.airtable.com/v0/meta/bases'),
		'/v0/meta/bases',
	)
	assert.throws(() => normalizeAirtablePath('https://example.com/v0/meta/bases'))
})