Skip to content

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

Package listing

@kody/figma

scripts/self-check.mjs

95 lines · 3.6 KB · JavaScript
import assert from 'node:assert/strict'
import {
	DEFAULT_OAUTH_SCOPES,
	FIGMA_AUTHORIZE_URL,
	FIGMA_REQUIRED_HOST,
	FIGMA_TOKEN_URL,
	inferOperationFromPath,
	normalizeNodeId,
	oauthConnectUrl,
	parseFileKey,
	patSetupUrl,
	scopeForOperation,
} from '../src/setup.ts'
import { mapComment, mapComponentList, mapFile, mapImageMap } from '../src/models.ts'
import { resolveIntegrationName, resolveSecretName } from '../src/auth-names.ts'

assert.equal(parseFileKey('AbC123xyz'), 'AbC123xyz')
assert.equal(
	parseFileKey('https://www.figma.com/design/AbC123xyz/Demo?node-id=1-2'),
	'AbC123xyz',
)
assert.equal(parseFileKey('https://figma.com/file/Key99/Name'), 'Key99')
assert.equal(normalizeNodeId('12-34'), '12:34')
assert.equal(normalizeNodeId('12:34'), '12:34')
assert.equal(scopeForOperation('comments.write'), 'file_comments:write')
assert.equal(inferOperationFromPath('POST', '/v1/files/abc/comments'), 'comments.write')
assert.equal(inferOperationFromPath('GET', '/v1/files/abc/nodes'), 'files.read')
assert.equal(inferOperationFromPath('GET', '/v1/files/abc/meta'), 'files.meta')
assert.equal(inferOperationFromPath('GET', '/v1/files/abc/components'), 'library.read')
assert.equal(inferOperationFromPath('GET', '/v1/images/abc'), 'files.read')
assert.equal(inferOperationFromPath('GET', '/v1/me'), 'users.read')

assert.equal(resolveIntegrationName({}), 'figma')
assert.equal(resolveIntegrationName({ account: 'work' }), 'figma-work')
assert.equal(resolveIntegrationName({ account: 'figma-work' }), 'figma-work')
assert.equal(resolveIntegrationName({ integrationName: 'figma-brand' }), 'figma-brand')
assert.equal(resolveSecretName({}), 'figmaPat')
assert.equal(resolveSecretName({ account: 'work' }), 'figmaPat-work')
assert.equal(resolveSecretName({ secretName: 'figmaPlanToken' }), 'figmaPlanToken')

const connectUrl = oauthConnectUrl('figma')
assert.match(connectUrl, /^https:\/\/kody\.codes\/connect\/oauth\?/)
assert.ok(connectUrl.includes(encodeURIComponent(FIGMA_AUTHORIZE_URL)))
assert.ok(connectUrl.includes(encodeURIComponent(FIGMA_TOKEN_URL)))
assert.ok(connectUrl.includes('file_content%3Aread'))
assert.ok(connectUrl.includes(FIGMA_REQUIRED_HOST))
assert.ok(DEFAULT_OAUTH_SCOPES.includes('file_comments:write'))

const patUrl = patSetupUrl('figmaPat')
assert.match(patUrl, /^https:\/\/kody\.codes\/account\/secrets\/new\?/)
assert.ok(patUrl.includes('name=figmaPat'))
assert.ok(patUrl.includes('api.figma.com'))

const file = mapFile('abc', {
	name: 'Checkout',
	lastModified: '2026-01-01T00:00:00Z',
	version: '1',
	document: {
		id: '0:0',
		name: 'Document',
		type: 'DOCUMENT',
		children: [{ id: '0:1', name: 'Page 1', type: 'CANVAS' }],
	},
	components: { a: {}, b: {} },
	styles: { c: {} },
})
assert.equal(file.fileKey, 'abc')
assert.equal(file.name, 'Checkout')
assert.equal(file.componentCount, 2)
assert.equal(file.styleCount, 1)
assert.equal(file.document?.children[0]?.name, 'Page 1')

const comment = mapComment('abc', {
	id: 'c1',
	message: 'Hello',
	user: { id: 'u1', handle: 'ada', email: 'hidden@example.com' },
	reactions: [{ emoji: ':eyes:' }, { emoji: ':eyes:' }],
})
assert.equal(comment.user?.handle, 'ada')
assert.equal('email' in (comment.user ?? {}), false)
assert.deepEqual(comment.reactions, [{ emoji: ':eyes:', count: 2 }])

const components = mapComponentList({
	meta: {
		components: {
			'12:34': { key: 'comp1', name: 'Button', file_key: 'abc', node_id: '12:34' },
		},
	},
})
assert.equal(components[0]?.name, 'Button')

const images = mapImageMap('abc', { images: { '12:34': 'https://example.com/a.png' }, err: null })
assert.equal(images.images[0]?.url, 'https://example.com/a.png')

console.log('self-check ok')