import { accounts } from './accounts.ts'
import { getViewer } from './viewer.ts'
import {
inferOperationFromPath,
normalizeNodeId,
oauthConnectUrl,
parseFileKey,
scopeForOperation,
} from './setup.ts'
import { normalizeFigmaPath } from './client.ts'
import type { FigmaAuthInput } from './types.ts'
import { optionalString, requireRecord } from './types.ts'
import { requireAuthMode } from './auth.ts'
export type SmokeTestInput = FigmaAuthInput & {
/** When true, skip the live viewer query even if credentials exist. */
setupOnly?: boolean
}
/**
* Verify Figma auth without returning account PII. Without credentials this
* still returns setup URLs so the package is forkable before anyone connects.
* @example
* import smokeTest from 'kody:@kody/figma/smoke-test'
* const result = await smokeTest()
*/
export async function smokeTest(params: SmokeTestInput = {}) {
const connectUrl = oauthConnectUrl('figma')
const selfCheck = {
ok: true as const,
pathNormalizesApiPrefix: normalizeFigmaPath('/files/abc') === '/v1/files/abc',
mePathUnchanged: normalizeFigmaPath('/v1/me') === '/v1/me',
commentWriteScope: scopeForOperation('comments.write') === 'file_comments:write',
commentWriteFromPath:
inferOperationFromPath('POST', '/v1/files/abc/comments') === 'comments.write',
fileKeyFromDesignUrl:
parseFileKey('https://www.figma.com/design/AbC123xyz/Demo?node-id=1-2') ===
'AbC123xyz',
nodeIdNormalizesHyphen: normalizeNodeId('12-34') === '12:34',
connectUrlUsesFigmaHost: connectUrl.includes('www.figma.com'),
connectUrlHasFileScope: connectUrl.includes('file_content%3Aread'),
}
const info = await accounts(params)
const canLive = Boolean(info.preferredAuth) && params.setupOnly !== true
if (!canLive) {
return {
ok: true,
live: false,
selfCheck,
authMode: info.preferredAuth,
integrationName: info.integrationName,
secretName: info.secretName,
oauthConnected: info.oauthConnected,
patSaved: info.patSaved,
hasViewerId: false,
setup: {
connectUrl: info.connectUrl,
patUrl: info.patUrl,
callbackUrl: info.callbackUrl,
requiredHost: info.requiredHost,
},
}
}
const viewer = await getViewer(params)
return {
ok: true,
live: true,
selfCheck,
authMode: info.preferredAuth,
integrationName: info.integrationName,
secretName: info.secretName,
oauthConnected: info.oauthConnected,
patSaved: info.patSaved,
hasViewerId: viewer.id.length > 0,
setup: {
connectUrl: info.connectUrl,
patUrl: info.patUrl,
callbackUrl: info.callbackUrl,
requiredHost: info.requiredHost,
},
}
}
/**
* Verify Figma auth without returning account PII.
* @example
* import smokeTest from 'kody:@kody/figma/smoke-test'
* const result = await smokeTest({ setupOnly: true })
*/
export default async function smokeTestEntrypoint(
params: SmokeTestInput & Record<string, unknown> = {},
) {
const input = requireRecord(params, 'smoke-test')
return smokeTest({
integrationName: optionalString(input.integrationName, 'integrationName'),
integration: optionalString(input.integration, 'integration'),
account: optionalString(input.account, 'account'),
secretName: optionalString(input.secretName, 'secretName'),
auth: requireAuthMode(input.auth),
setupOnly: input.setupOnly === true,
})
}