Skip to content
← Public packages

@kody/doordash

Order food on DoorDash as a customer through the consumer MCP used by dd-cli.

src/smoke-test.ts

67 lines · 1.8 KB · TypeScript
import { kody } from 'kody:runtime'
import { mcpRequest, setupUrls, resolveAccessTokenSecretName, TOOLS } from './mcp.ts'

function secretNames(result: unknown): Set<string> {
	const names = new Set<string>()
	const rows =
		result && typeof result === 'object' && Array.isArray((result as any).secrets)
			? (result as any).secrets
			: Array.isArray(result)
				? result
				: []
	for (const row of rows) {
		if (row?.name) names.add(String(row.name))
	}
	return names
}

export default async function smokeTest(
	input: { liveListTools?: boolean; account?: string; accessTokenSecret?: string } = {},
) {
	const accessTokenSecret = resolveAccessTokenSecretName(input)
	let hasSecret = false
	try {
		const listed = await kody.secretList({ scope: 'user' })
		hasSecret = secretNames(listed).has(accessTokenSecret)
	} catch {
		/* ignore */
	}

	const setup = setupUrls(input)
	const next: string[] = []
	if (!hasSecret) {
		next.push('Join waitlist if needed: ' + setup.waitlist)
		next.push('Install dd-cli, run export-token, save secret: ' + setup.accessTokenUrl)
		next.push('Approve host ' + setup.hosts.join(', ') + ' on that secret.')
	} else {
		next.push('Try list-tools or search({ query: "pizza" }).')
		next.push('Cart/order mutations stay dry-run until confirm: true.')
	}

	const result: Record<string, unknown> = {
		ok: hasSecret,
		hasSecret,
		accessTokenSecret,
		setup,
		tools: TOOLS,
		relatedDrivePackage: '@kody/doordash-driver',
		next,
	}

	if (input.liveListTools) {
		result.listToolsDryRun = await mcpRequest({
			...input,
			method: 'tools/list',
			dryRun: true,
		})
		if (hasSecret) {
			try {
				result.listTools = await mcpRequest({ ...input, method: 'tools/list' })
			} catch (error) {
				result.listToolsError = error instanceof Error ? error.message : String(error)
			}
		}
	}

	return result
}