import { accounts } from './accounts.ts'
import { inferOperationFromPath, normalizeWorkosPath, workosUrl } from './client.ts'
import { listOrganizations } from './list-organizations.ts'
import { apiKeySetupUrl } from './setup.ts'
import type { WorkosAuthInput } from './types.ts'
import { optionalString, requireRecord } from './types.ts'
export type SmokeTestInput = WorkosAuthInput & {
/** When true, skip the live organization list even if credentials exist. */
setupOnly?: boolean
/** Alias of `setupOnly`. */
dryRun?: boolean
}
/**
* Verify WorkOS auth without returning user emails. Without credentials this
* still returns the API-key setup URL so the package is forkable first.
* @example
* import smokeTest from 'kody:@kody/workos/smoke-test'
* const result = await smokeTest()
*/
export async function smokeTest(params: SmokeTestInput = {}) {
const selfCheck = {
ok: true as const,
pathNormalizesRoot: normalizeWorkosPath('organizations') === '/organizations',
listUrl: workosUrl('/organizations', { limit: 1 }).startsWith(
'https://api.workos.com/organizations',
),
portalWriteFromPath: inferOperationFromPath('POST', '/portal/generate_link') === 'portal.write',
orgWriteFromPath: inferOperationFromPath('POST', '/organizations') === 'organizations.write',
setupUrlHasApiHost: apiKeySetupUrl().includes('api.workos.com'),
}
const info = await accounts(params)
const skipLive = params.setupOnly === true || params.dryRun === true
const canLive = info.apiKeySaved && !skipLive
if (!canLive) {
return {
ok: true,
live: false,
dryRun: skipLive || undefined,
selfCheck,
secretName: info.secretName,
apiKeySaved: info.apiKeySaved,
listedCount: 0,
setup: {
setupUrl: info.setupUrl,
dashboardUrl: info.dashboardUrl,
requiredHost: info.requiredHost,
},
}
}
const listed = await listOrganizations({ ...params, limit: 1 })
return {
ok: true,
live: true,
selfCheck,
secretName: info.secretName,
apiKeySaved: info.apiKeySaved,
listedCount: listed.items.length,
hasMore: Boolean(listed.pageInfo.after),
setup: {
setupUrl: info.setupUrl,
dashboardUrl: info.dashboardUrl,
requiredHost: info.requiredHost,
},
}
}
/**
* Verify WorkOS auth without returning user emails.
* @example
* import smokeTest from 'kody:@kody/workos/smoke-test'
* const result = await smokeTest({ dryRun: true })
*/
export default async function smokeTestEntrypoint(
params: SmokeTestInput & Record<string, unknown> = {},
) {
const input = requireRecord(params, 'smoke-test')
return smokeTest({
account: optionalString(input.account, 'account'),
secretName: optionalString(input.secretName, 'secretName'),
setupOnly: input.setupOnly === true,
dryRun: input.dryRun === true,
})
}