Skip to content

src/raycast.test.mjs

104 lines · 3.9 KB · JavaScript
Raw
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import test from 'node:test'
import { fileURLToPath } from 'node:url'

const here = dirname(fileURLToPath(import.meta.url))
const root = join(here, '..')

const OFFICIAL_PACKAGE_ID = 'ea5db329-3cc0-4daa-b23a-3954cccfe560'
const PACKAGE_NAME = '@kody/raycast'
const PUBLIC_URL = 'https://kody.codes/@kody/raycast'

function tokenSetupUrl(packageId = OFFICIAL_PACKAGE_ID) {
	return `https://kody.codes/account/packages/${encodeURIComponent(packageId)}?newToken=1&name=Raycast&exportNames=*`
}

function invocationUrl(username, exportName = 'list-packages') {
	const owner = username.replace(/^@/, '').trim()
	const path = exportName.trim().replace(/^\.\//, '') || '__root__'
	return `https://kody.codes/@${encodeURIComponent(owner)}/api/package-invocations/raycast/${path}`
}

function routeExportName(exportName) {
	const trimmed = exportName.trim()
	if (!trimmed || trimmed === '.' || trimmed === './') return '__root__'
	return trimmed.replace(/^\.\//, '')
}

function getExportName(subpath) {
	if (subpath === '.') return ''
	if (subpath.startsWith('./')) return subpath.slice(2)
	return subpath
}

function isDryRun(input) {
	return input?.dryRun === true
}

test('official identity is fork-ready community package', () => {
	const manifest = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'))
	assert.equal(manifest.name, PACKAGE_NAME)
	assert.equal(manifest.private, false)
	assert.equal(manifest.license, 'MIT')
	assert.equal(manifest.kody.id, 'raycast')
	assert.equal(manifest.kody.category, 'productivity')
	assert.ok(Array.isArray(manifest.kody.webhooks))
	assert.ok(manifest.exports['./list-commands'])
	assert.ok(manifest.exports['./upsert-command'])
	assert.ok(manifest.exports['./run'])
	assert.ok(manifest.exports['./adapt'])
	assert.equal(manifest.kody.jobs, undefined)
	assert.doesNotMatch(JSON.stringify(manifest), /kentcdodds\.kody\.run/i)
	assert.doesNotMatch(JSON.stringify(manifest), /lutron/i)
	assert.doesNotMatch(readFileSync(join(root, 'README.md'), 'utf8'), /KODY_TOKEN=kody_/)
})

test('docs stay generic (no personal Mac paths or private notes)', () => {
	const readme = readFileSync(join(root, 'README.md'), 'utf8')
	const agents = readFileSync(join(root, 'AGENTS.md'), 'utf8')
	assert.match(readme, /## Intent/)
	assert.match(readme, /## Adapt this fork/)
	assert.match(agents, /upsert-command/)
	assert.doesNotMatch(readme + agents, /raycast-shortcuts/)
	assert.doesNotMatch(readme + agents, /kentcdodds\.kody\.run/)
	assert.doesNotMatch(readme + agents, /\/Users\/kent/)
	assert.doesNotMatch(readme + agents, /office.?lights/i)
})

test('token setup URL is prefilled and never includes a raw token', () => {
	const url = tokenSetupUrl()
	assert.equal(
		url,
		`https://kody.codes/account/packages/${OFFICIAL_PACKAGE_ID}?newToken=1&name=Raycast&exportNames=*`,
	)
	assert.doesNotMatch(url, /rawToken|bearer=/i)
	assert.match(url, /newToken=1/)
	assert.equal(PUBLIC_URL, 'https://kody.codes/@kody/raycast')
})

test('invocation URL is owner-scoped', () => {
	assert.equal(
		invocationUrl('alice', 'list-packages'),
		'https://kody.codes/@alice/api/package-invocations/raycast/list-packages',
	)
	assert.equal(routeExportName('.'), '__root__')
	assert.equal(routeExportName('./invoke'), 'invoke')
	assert.equal(getExportName('.'), '')
	assert.equal(getExportName('./get-package'), 'get-package')
})

test('invoke dryRun is opt-in', () => {
	assert.equal(isDryRun({}), false)
	assert.equal(isDryRun({ dryRun: false }), false)
	assert.equal(isDryRun({ dryRun: true }), true)
})

test('extension points at official package, not personal scope', () => {
	const ext = JSON.parse(readFileSync(join(root, 'extension/package.json'), 'utf8'))
	assert.equal(ext.name, 'kody')
	assert.equal(ext.title, 'Kody')
	assert.doesNotMatch(JSON.stringify(ext), /@kentcdodds\/raycast/)
	assert.doesNotMatch(readFileSync(join(root, 'extension/src/set-up-webhooks.tsx'), 'utf8'), /@kentcdodds/)
})