diff --git a/changes/fix-attio-pilot-gaps.md b/changes/fix-attio-pilot-gaps.md new file mode 100644 index 0000000..dde3464 --- /dev/null +++ b/changes/fix-attio-pilot-gaps.md @@ -0,0 +1,18 @@ +### Fixed + +- **`new --from-config` with `auth.verify` makes a CLI without the template's sample.** `src/context.ts`, + `src/program.ts`, the test harness and `api`, `mcp` and `run-scope` tests reach the API and its fake, and the + sample's files are left out; before, they imported `src/sample/`, and `remove-sample` refused the CLI until about 30 + lines were rewritten by hand. The hosts an OAuth sign-in in the description reaches, such as `app.attio.com`, join + `hosts`, so `lint:commands` no longer stops on them. +- **A function in a config's `source` stops `pnpm generate`** and names where it is, instead of being dropped + silently when the source is written out as JSON. +- **The seed's sync test reads the CRM's rows from `crmManifestFake`**, which pages them to their end. Over + `fakeServiceApi`, every page of an offset list held a row, so a sync read until its budget ran out and the test + timed out (382 pages in the Attio pilot). + +### Added + +- **`auth.verify.active`**: the field of the check's answer that must be `true`, for a service that answers a revoked + key with 200 and `active: false`, as Attio does. `session start` then says to sign in again, not `provider_error`. +- **A config with `source.users` mounts `users list|show`**, read live from the CRM. diff --git a/cspell.json b/cspell.json index e811f81..7ccc9e0 100644 --- a/cspell.json +++ b/cspell.json @@ -2,5 +2,5 @@ "version": "0.2", "import": ["./config/wirecat/cspell.json"], "ignorePaths": ["docs/commands.md", "docs/dev/**"], - "words": ["clidev", "gifsicle", "Petstore", "typechecks", "ydisk"] + "words": ["Attio", "clidev", "gifsicle", "Petstore", "typechecks", "ydisk"] } diff --git a/kit/README.md b/kit/README.md index c858a6d..2d405d8 100644 --- a/kit/README.md +++ b/kit/README.md @@ -92,15 +92,22 @@ read before the CLI has `node_modules`: its import resolves to the kit's own cli - A `source` (a `crm` or a `tracker` category) needs `auth: { preset: "token", verify }`; `new` writes the files below only for what the config holds at `new`, and `pnpm generate` then reads their values again. - With `auth.verify`, `session start` is cli-core's `tokenSessionCommand`: it checks a token with that operation and - keeps the account's id from the answer. + keeps the account's id from the answer. `verify.active` names a field that must be `true`, for a service that + answers a revoked key with 200 and `active: false`, as Attio does; anything else is "sign in again". +- With `auth.verify` and a spec, the CLI starts without the template's sample: its wiring and tests reach the API and + its fake, so there is nothing for `remove-sample` to do. The hosts an OAuth sign-in in the description reaches join + `hosts`. - With a `source`, `src/commands/crm.ts` mounts cli-core's CRM commands (`sync`, `store status`, `deals`, `search`…) - over `crmSourceFromManifest`, the store keyed by the account `session start` checked. A `tracker` source gets + over `crmSourceFromManifest`, the store keyed by the account `session start` checked; with `source.users`, also + `users list|show`, read live. A function in `source` stops `pnpm generate`: the source is written out as JSON, so + code goes into the hooks module. A `tracker` source gets `src/commands/tracker.ts` in its place: cli-core's tracker commands (`sync`, `issues`, `projects`…) over `trackerSourceFromManifest`, usually with `spec: { kind: "graphql", from: "" }`. `new` asks the endpoint for its schema by introspection; a GraphQL schema names no sign-in, so the header the key travels in is `auth` in `scripts/generate.ts`, the sample's `Authorization` until you check the service's documentation. - Tests come with them: `src/commands/session.test.ts` signs in against a fake of the API, and - `src/crm-contracts.test.ts` runs `crmSourceContract` and `crmSyncContract`. `lint/contract-exceptions.json` excepts + `src/crm-contracts.test.ts` runs `crmSourceContract` and `crmSyncContract`, and a sync through the CLI over + `crmManifestFake`, which pages the rows to their end. `lint/contract-exceptions.json` excepts `crmWriteContract`, as the source only reads. A list the generator finds no paging for fails the contract's cut-list case on purpose: describe the paging in the API description or an override. A tracker's `src/tracker-contracts.test.ts` checks the source against the generated API and syncs through the CLI over a fake diff --git a/kit/seeds/config/scripts/from-config.ts b/kit/seeds/config/scripts/from-config.ts index 193f1d3..6bd6b96 100644 --- a/kit/seeds/config/scripts/from-config.ts +++ b/kit/seeds/config/scripts/from-config.ts @@ -11,6 +11,19 @@ const out = join(root, "src/generated/wirecat-config.ts") if (process.argv.includes("--if-missing") && existsSync(out)) process.exit(0) const cli = parseCliConfig(config) +// The source is written out as JSON, which drops a function without a word: a rule that is code belongs in the hooks. +const functionsIn = (value: unknown, path: string): string[] => + typeof value === "function" + ? [path] + : value !== null && typeof value === "object" + ? Object.entries(value).flatMap(([key, inner]) => functionsIn(inner, `${path}.${key}`)) + : [] +const functions = functionsIn(cli.source, "source") +if (functions.length) + throw new Error( + `wirecat.config: ${functions.join(", ")}: a function cannot be written into the CLI; ` + + "move it into the hooks module the config names in hooks (./src/hooks.ts)", + ) const verify = cli.auth.preset === "none" ? undefined : cli.auth.verify const literal = (value: unknown) => (value === undefined ? "undefined" : JSON.stringify(value, null, 2)) // The hooks module is imported from src/generated/, inside the bundle: it must live under src/. @@ -36,7 +49,7 @@ const text = formatted( [ "// Generated from wirecat.config.ts by scripts/from-config.ts; run pnpm generate", "", - `export const VERIFY: { operation: string; id: string; name?: string } | undefined = ${literal(verify)}`, + `export const VERIFY: { operation: string; id: string; name?: string; active?: string } | undefined = ${literal(verify)}`, "", `export const SOURCE: Readonly> | undefined = ${literal(cli.source)}`, "", diff --git a/kit/seeds/config/src/commands/crm.ts b/kit/seeds/config/src/commands/crm.ts index 2cabcde..dac69cc 100644 --- a/kit/seeds/config/src/commands/crm.ts +++ b/kit/seeds/config/src/commands/crm.ts @@ -94,6 +94,8 @@ export const crmCommands = (): Command[] => { app: IDENTITY, open, leads: Array.isArray(SOURCE?.kinds) && SOURCE.kinds.includes("lead"), + // People and companies have no live list in a manifest source yet; its users do. + ...(SOURCE?.users ? { live: ["user"] as const } : {}), }) return [...holder.commands] } diff --git a/kit/seeds/config/src/commands/session.ts b/kit/seeds/config/src/commands/session.ts index 59e25a2..75c5efb 100644 --- a/kit/seeds/config/src/commands/session.ts +++ b/kit/seeds/config/src/commands/session.ts @@ -1,20 +1,13 @@ import { CliError } from "@wirecat/cli-base" import { hostFetch } from "@wirecat/cli-base/http" import { serviceApiCall, tokenSessionCommand } from "@wirecat/cli-core/cli" +import { valueAt } from "@wirecat/cli-core/manifest" import type { Command } from "commander" import { API, configuration } from "../config.js" import { contextOf } from "../context.js" import { VERIFY } from "../generated/wirecat-config.js" import { DESKTOP, IDENTITY } from "../identity.js" -const valueAt = (answer: unknown, path: string): unknown => - path - .split(".") - .reduce( - (at, key) => (at !== null && typeof at === "object" ? (at as Record)[key] : undefined), - answer, - ) - /** * `session start | status | end | list` (WC-ACCT-12): the token is checked by the call `auth.verify` names in * wirecat.config.ts, and the account is the id and name its answer holds. @@ -34,6 +27,9 @@ export const sessionCommand = (): Command => token: () => token, }) const answer = await call(VERIFY.operation, { path: {}, query: {}, headers: {}, fields: {} }) + // A service such as Attio answers a revoked key with 200 and `active: false`, not with 401. + if (VERIFY.active && valueAt(answer, VERIFY.active) !== true) + throw new CliError("authentication_error", `${API.service} does not accept this token; sign in again`) const id = valueAt(answer, VERIFY.id) if (typeof id !== "string" && typeof id !== "number") throw new CliError("provider_error", `${API.service} answered ${VERIFY.operation} without ${VERIFY.id}`, { diff --git a/kit/seeds/config/src/crm-contracts.test.ts b/kit/seeds/config/src/crm-contracts.test.ts index 04947d8..c2efd49 100644 --- a/kit/seeds/config/src/crm-contracts.test.ts +++ b/kit/seeds/config/src/crm-contracts.test.ts @@ -5,16 +5,18 @@ import { memoryKeyring } from "@wirecat/cli-base" import { parseCrmManifest } from "@wirecat/cli-core/crm" import { type CrmManifestFake, + crmManifestFake, crmManifestFixture, crmSourceContract, crmSyncContract, + FAKE_MANIFEST_TOKEN, } from "@wirecat/cli-core/crm/testing" import { type MessageStore, openStore } from "@wirecat/cli-core/store" import { afterEach, describe, expect, it } from "vitest" import { API } from "./config.js" import { definitions } from "./generated/definitions.js" -import { HOOKS, SOURCE } from "./generated/wirecat-config.js" -import { FAKE_API_TOKEN, fakeServiceApi } from "./testing/fake-api.js" +import { HOOKS, SOURCE, VERIFY } from "./generated/wirecat-config.js" +import { fakeServiceApi } from "./testing/fake-api.js" import { runCli } from "./testing/harness.js" const manifest = parseCrmManifest(SOURCE, API.manifest) @@ -45,18 +47,45 @@ describe("crmSyncContract over a store", () => { it(name, run) }) +// fakeServiceApi answers every page of a list with a sample row, and an offset list ends only on an empty page: a sync +// over it reads until its budget runs out. The CRM's rows come from crmManifestFake, which pages them to their end; the +// call auth.verify names stays with fakeServiceApi, which crmManifestFake does not answer. +const signInAndCrm = (crm: CrmManifestFake) => { + const verify = API.manifest.operations.find((one) => one.id === VERIFY?.operation) + const path = verify?.binding.kind === "http" ? verify.binding.path : undefined + const pattern = path ? new RegExp(`${path.replace(/\{[^}]+\}/g, "[^/]+")}$`) : undefined + const signIn = fakeServiceApi(FAKE_MANIFEST_TOKEN) + return { + fetch: (input: string | URL | Request, init?: RequestInit) => { + const url = new URL(input instanceof Request ? input.url : input) + return pattern?.test(url.pathname) ? signIn.fetch(input, init) : crm.fetch(input, init) + }, + } +} + it("sync through this CLI keeps the rows under the account session start checked", async () => { const keyring = memoryKeyring() - const fake = fakeServiceApi() + const crm = crmManifestFake(manifest, { api: API.manifest, definitions }) + fakes.push(crm) + const service = signInAndCrm(crm) const started = await runCli(["personal", "session", "start", "--json"], { keyring, - service: fake, - stdin: FAKE_API_TOKEN, + service, + stdin: FAKE_MANIFEST_TOKEN, }) expect(started.code, started.err).toBe(0) - const synced = await runCli(["personal", "sync", "--json"], { keyring, service: fake }) + const synced = await runCli(["personal", "sync", "--json"], { keyring, service }) expect(synced.code, synced.err).toBe(0) - const status = await runCli(["personal", "store", "status", "--json"], { keyring, service: fake }) + expect(synced.json().item).toMatchObject({ complete: true }) + const status = await runCli(["personal", "store", "status", "--json"], { keyring, service }) expect(status.code, status.err).toBe(0) - expect(fake.refusals).toEqual([]) + const kinds = (status.json() as { items: { kind: string; stored: number; state: string }[] }).items + for (const kind of manifest.kinds) + expect( + kinds.find((one) => one.kind === kind), + kind, + ).toMatchObject({ + state: "complete", + stored: crm.rows[kind]?.length, + }) }) diff --git a/kit/seeds/config/src/testing/fake-api.ts b/kit/seeds/config/src/testing/fake-api.ts index c2fb4d7..159b10b 100644 --- a/kit/seeds/config/src/testing/fake-api.ts +++ b/kit/seeds/config/src/testing/fake-api.ts @@ -1,7 +1,8 @@ -import { fakeApi, graphqlFake } from "@wirecat/cli-base/codegen" +import { fakeApi, graphqlFake, sampleOf } from "@wirecat/cli-base/codegen" import { manifestBaseUrl } from "@wirecat/cli-core/cli" import { API } from "../config.js" import { definitions } from "../generated/definitions.js" +import { VERIFY } from "../generated/wirecat-config.js" /** The token fakeServiceApi takes; never a real one (WC-TEST-2). */ export const FAKE_API_TOKEN = "fake0api0token" @@ -16,6 +17,28 @@ const graphqlAuth = (token: string) => { : { header: "Authorization", value: `Bearer ${token}` } } +/** The answer of the call auth.verify names, with the token marked active where auth.verify.active says. */ +const verifyAnswer = (): Record { body: unknown }> => { + const operation = API.manifest.operations.find((one) => one.id === VERIFY?.operation) + const schema = operation?.response?.schema + const active = VERIFY?.active + if (!VERIFY || !active || !schema) return {} + return { + [VERIFY.operation]: () => { + const body = sampleOf({ type: "ref", ref: schema }, definitions) + const keys = active.split(".") + const last = keys.pop() as string + let here = body as Record + for (const key of keys) { + here[key] ??= {} + here = here[key] as Record + } + here[last] = true + return { body } + }, + } +} + /** The service's API as its description says, answering only `token`: a sample of each answer schema. */ export const fakeServiceApi = (token = FAKE_API_TOKEN) => graphql @@ -32,18 +55,5 @@ export const fakeServiceApi = (token = FAKE_API_TOKEN) => security: API.manifest.security, securitySchemes: API.manifest.securitySchemes, token, + answers: verifyAnswer(), }) - -type Fetch = (input: string | URL | Request, init?: RequestInit) => Promise - -/** The API's host goes to fakeServiceApi, any other to `rest` (the sample's fake while the sample stays). */ -export const withServiceApi = (rest: { fetch: Fetch }, token: string): { fetch: Fetch } => { - const api = fakeServiceApi(token) - const host = new URL(manifestBaseUrl(API.manifest)).host - return { - fetch: (input, init) => - new URL(input instanceof Request ? input.url : input).host === host - ? api.fetch(input, init) - : rest.fetch(input, init), - } -} diff --git a/kit/src/config-wiring.ts b/kit/src/config-wiring.ts new file mode 100644 index 0000000..0bd204b --- /dev/null +++ b/kit/src/config-wiring.ts @@ -0,0 +1,127 @@ +import { withoutMount } from "./sample.js" +import { replaceOnce } from "./spec.js" + +// A config whose `auth.verify` checks the token against the API has a fake of that API, so the CLI it makes starts on +// the API, not on the template's sample: `new` leaves the sample's files out and these rewrite the files that used it. +// `remove-sample` refused such a CLI before, since its wiring imported the sample. + +const each = (file: string, text: string, pairs: readonly [string | RegExp, string][]): string => + pairs.reduce((rest, [from, to]) => replaceOnce(file, rest, from, to), text) + +const CONTEXT = "src/context.ts" +const withoutSampleContext = (text: string): string => { + const kept = each(CONTEXT, text, [ + [" type EventSink,\n", ""], + ['import { Pacer, pacePathFor } from "@wirecat/cli-core/sends"\n', ""], + [/^import \{ type CommentsService, commentsService \} from "\.\/sample\/comments\.js"\n/m, ""], + [/^import \{ transport \} from "\.\/sample\/transport\.js"\n/m, ""], + ]) + // The sample's service and its run wrapper go: the api group and the CRM commands run their own calls. + return each(CONTEXT, kept, [ + [/\n\/\*\* The service for one run[\s\S]*?\n}\n/, "\n"], + [/\n\/\*\*\n \* Every remote call goes through here[\s\S]*?\n}\n/, ""], + ]) +} + +const PROGRAM = "src/program.ts" +const withoutSampleProgram = (text: string): string => + each(PROGRAM, withoutMount(text), [ + ['untrusted: ["comment text", "author names"],', 'untrusted: ["names and text the service\'s users wrote"],'], + ['permissionExample: "comments.create=deny",', 'permissionExample: "api.list=deny",'], + ]) + +const HARNESS = "src/testing/harness.ts" +const withoutSampleHarness = (text: string): string => + each(HARNESS, text, [ + [ + 'import { FAKE_TOKEN, fakeComments } from "./fake-comments.js"\n', + 'import { FAKE_API_TOKEN as FAKE_TOKEN, fakeServiceApi } from "./fake-api.js"\n', + ], + ["service = fakeComments(),", "service = fakeServiceApi(),"], + ]) + +const MCP = "src/mcp.test.ts" +const withoutSampleMcp = (text: string): string => + each(MCP, text, [ + [ + 'import { fakeComments } from "./testing/fake-comments.js"\n', + 'import { fakeServiceApi } from "./testing/fake-api.js"\n', + ], + ['"comments.nope=deny"', '"api.nope=deny"'], + ['toContain("comments.nope")', 'toContain("api.nope")'], + [/"comments\.create=deny"/g, '"api.list=deny"'], + ['{ permissions: { "comments.list": "deny" } }', '{ permissions: { "api.list": "deny" } }'], + ["const service = fakeComments()", "const service = fakeServiceApi()"], + ['{ query: "comments list" }', '{ query: "api list" }'], + [/command: "comments list"/g, 'command: "api list"'], + ]) + +const RUN_SCOPE = "src/run-scope.test.ts" +export const withoutSampleRunScope = (text: string): string => + each(RUN_SCOPE, text, [ + [ + 'import { FAKE_TOKEN, fakeComments } from "./testing/fake-comments.js"\n', + 'import { FAKE_API_TOKEN as FAKE_TOKEN, fakeServiceApi } from "./testing/fake-api.js"\n', + ], + [/^ +"comments [a-z]+": \[.*\],\n/gm, ""], + ["fetch: fakeComments().fetch", "fetch: fakeServiceApi().fetch"], + ]) + +/** + * run-scope over a config's wiring: `session start` checks the token with the API, so the answering fake is the API's, + * the sample gone; `sync` is cli-core's, run in its own run and tested by the source's contracts test. + */ +export const withConfigRunScope = ( + text: string, + { verifies, source, users = false }: { verifies: boolean; source?: "crm" | "tracker"; users?: boolean }, +): string => { + const answering = verifies ? withoutSampleRunScope(text) : text + const sync = { + crm: ' sync: "cli-core\'s addCrmCommands runs it in its run; src/crm-contracts.test.ts runs crmSyncContract",', + tracker: ' sync: "cli-core\'s addTrackerCommands runs it in its run; src/tracker-contracts.test.ts syncs",', + } + return source + ? replaceOnce( + RUN_SCOPE, + answering, + ' "mcp serve": "listens until stopped",\n', + [ + ' "mcp serve": "listens until stopped",', + sync[source], + ...(users + ? [ + ' "users list": "cli-core\'s addCrmCommands runs it in its run, over source.users",', + ' "users show": "cli-core\'s addCrmCommands runs it in its run, over source.users",', + ] + : []), + "", + ].join("\n"), + ) + : answering +} + +const API_TEST = "src/api.test.ts" +const withoutSampleApiTest = (text: string): string => + each(API_TEST, text, [ + [ + 'import { FAKE_TOKEN } from "./testing/fake-comments.js"\n', + 'import { FAKE_API_TOKEN as FAKE_TOKEN } from "./testing/fake-api.js"\n', + ], + ]) + +const IDENTITY = "src/identity.ts" +const withoutSampleHost = (text: string): string => + each(IDENTITY, text, [ + [/^ *\/\/ SAMPLE: every host the CLI contacts.*\n/m, ""], + [/"comments\.example\.invalid",? ?/, ""], + ]) + +/** The rewrites of the files that used the sample, by path. */ +export const OFF_SAMPLE: Readonly string>> = { + [CONTEXT]: withoutSampleContext, + [PROGRAM]: withoutSampleProgram, + [HARNESS]: withoutSampleHarness, + [MCP]: withoutSampleMcp, + [API_TEST]: withoutSampleApiTest, + [IDENTITY]: withoutSampleHost, +} diff --git a/kit/src/new.ts b/kit/src/new.ts index 0edbf03..7411250 100644 --- a/kit/src/new.ts +++ b/kit/src/new.ts @@ -7,6 +7,7 @@ import { dirname, join } from "node:path" import { createInterface } from "node:readline/promises" import type { Readable, Writable } from "node:stream" import { promisify } from "node:util" +import { OFF_SAMPLE, withConfigRunScope } from "./config-wiring.js" import type { LoadedConfig } from "./from-config.js" import { ask, @@ -20,6 +21,7 @@ import { } from "./interview.js" import { type CliManifest, isRecord, MANIFEST, type Names, sha256 } from "./manifest.js" import { appNameOf, isWireCat, nameClashes, prefixOf, withFreePrefix } from "./rename.js" +import { inSample } from "./sample.js" import { renderTemplate, type TemplateSource } from "./source.js" import { replaceOnce, @@ -763,43 +765,6 @@ export const withSourceCommands = (text: string, kind: "crm" | "tracker" = "crm" `$1apiCommand(),\n$1...${kind}Commands(),\n`, ) -const RUN_SCOPE = "src/run-scope.test.ts" - -/** - * run-scope over a config's wiring: `session start` checks the token with the API, so the answering fake answers the - * API's host too; `sync` is cli-core's, run in its own run and tested by the source's contracts test. - */ -export const withConfigRunScope = ( - text: string, - { verifies, source }: { verifies: boolean; source?: "crm" | "tracker" }, -): string => { - const answering = verifies - ? replaceOnce( - RUN_SCOPE, - replaceOnce( - RUN_SCOPE, - text, - 'import { signedIn } from "./testing/harness.js"\n', - 'import { withServiceApi } from "./testing/fake-api.js"\nimport { signedIn } from "./testing/harness.js"\n', - ), - "fetch: fakeComments().fetch, stdin: stdin()", - "fetch: withServiceApi(fakeComments(), FAKE_TOKEN).fetch, stdin: stdin()", - ) - : text - const sync = { - crm: ' sync: "cli-core\'s addCrmCommands runs it in its run; src/crm-contracts.test.ts runs crmSyncContract",', - tracker: ' sync: "cli-core\'s addTrackerCommands runs it in its run; src/tracker-contracts.test.ts syncs",', - } - return source - ? replaceOnce( - RUN_SCOPE, - answering, - ' "mcp serve": "listens until stopped",\n', - [' "mcp serve": "listens until stopped",', sync[source], ""].join("\n"), - ) - : answering -} - // pnpm generate writes what the CLI reads of the config into src/generated/, and build needs it before the first one. const withConfigScripts = (text: string): string => { const pkg = JSON.parse(text) @@ -862,6 +827,7 @@ export const runNew = async ( ? withConfigRunScope(withApiRunScope(text), { verifies: loaded.verifies, ...(loaded.config.source ? { source: sourceKind(loaded.config) } : {}), + users: Boolean(loaded.config.source && "users" in loaded.config.source && loaded.config.source.users), }) : withApiRunScope(text), } @@ -911,7 +877,11 @@ export const runNew = async ( const hashes: Record = {} const prefixed: string[] = [] const agentSkill = `skills/${appNameOf(names)}/SKILL.md` + // The sample stays only where nothing replaces it: a config's token check brings a fake of the API itself. + const offSample = Boolean(spec && loaded?.verifies) + const own = (path: string, text: string) => (offSample && OFF_SAMPLE[path] ? OFF_SAMPLE[path](text) : text) for (const [path, file] of rendered) { + if (offSample && inSample(path)) continue if (file.kind === "file") hashes[path] = sha256(file.content) const rewrite = rewrites[path] ?? @@ -921,7 +891,8 @@ export const runNew = async ( return service ? withServicePage(agentSkill, linked, service) : linked } : undefined) - const content = rewrite ? rewrite(file.content.toString("utf8")) : file.content + const rewritten = rewrite ? rewrite(file.content.toString("utf8")) : file.content + const content = OFF_SAMPLE[path] ? own(path, rewritten.toString()) : rewritten if (content.includes(`${prefixOf(names)}_`)) prefixed.push(path) await writeOut(target, path, content, file.mode) } @@ -929,7 +900,10 @@ export const runNew = async ( const seeds = await specSeeds(source, names, spec) for (const [path, seed] of downloaded ? withDownloadedSpec(seeds, names, downloaded) : seeds) { if (rendered.has(path)) throw new Error(`the ${spec} seed ${path} would replace a template file`) - const content = answers.signIn?.[0] === "none" ? withoutApiSignIn(path, seed.toString("utf8")) : seed + const content = own( + path, + answers.signIn?.[0] === "none" ? withoutApiSignIn(path, seed.toString("utf8")) : seed.toString("utf8"), + ) if (content.includes(`${prefixOf(names)}_`)) prefixed.push(path) await writeOut(target, path, content) } diff --git a/kit/src/remove-sample.ts b/kit/src/remove-sample.ts index 6b5dfe4..77e94d9 100644 --- a/kit/src/remove-sample.ts +++ b/kit/src/remove-sample.ts @@ -3,20 +3,10 @@ import { existsSync } from "node:fs" import { readdir, readFile, rm, writeFile } from "node:fs/promises" import { dirname, join } from "node:path" import { readCliManifest } from "./manifest.js" +import { inSample, withoutMount } from "./sample.js" import { requireCleanTree } from "./upgrade.js" -const SAMPLE = [ - "src/sample/", - "src/commands/comments.ts", - "src/comments.test.ts", - "src/records.test.ts", - "src/testing/fake-comments.ts", -] const PROGRAM = "src/program.ts" -const MOUNT = [ - /^import \{ commentsCommand \} from "\.\/commands\/comments\.js"\r?\n/m, - /^[ \t]*commentsCommand\(\),\r?\n/m, -] export type Step = { command: string; args: string[] } @@ -44,9 +34,6 @@ const walk = async (root: string, dir: string): Promise => { return nested.flat() } -const inSample = (path: string): boolean => - SAMPLE.some((entry) => (entry.endsWith("/") ? path.startsWith(entry) : path === entry)) - const IMPORT = /(?:from|import)\s*\(?\s*["'](\.{1,2}\/[^"']+)["']/g const importsOf = (path: string, text: string): string[] => @@ -54,8 +41,6 @@ const importsOf = (path: string, text: string): string[] => join(dirname(path), spec).replaceAll("\\", "/").replace(/\.js$/, ".ts"), ) -export const withoutMount = (text: string): string => MOUNT.reduce((rest, line) => rest.replace(line, ""), text) - export const runRemoveSample = async (cwd: string, runStep: Runner = spawnStep): Promise => { await readCliManifest(cwd).catch(() => { throw new Error(`${cwd} is not a CLI made from cli-template: it has no template.json naming the CLI`) diff --git a/kit/src/sample.ts b/kit/src/sample.ts new file mode 100644 index 0000000..d8a3dec --- /dev/null +++ b/kit/src/sample.ts @@ -0,0 +1,17 @@ +/** The template's sample: the files `remove-sample` deletes, and `new` leaves out when a config replaces it. */ +const SAMPLE = [ + "src/sample/", + "src/commands/comments.ts", + "src/comments.test.ts", + "src/records.test.ts", + "src/testing/fake-comments.ts", +] +const MOUNT = [ + /^import \{ commentsCommand \} from "\.\/commands\/comments\.js"\r?\n/m, + /^[ \t]*commentsCommand\(\),\r?\n/m, +] + +export const inSample = (path: string): boolean => + SAMPLE.some((entry) => (entry.endsWith("/") ? path.startsWith(entry) : path === entry)) + +export const withoutMount = (text: string): string => MOUNT.reduce((rest, line) => rest.replace(line, ""), text) diff --git a/kit/src/spec-url.ts b/kit/src/spec-url.ts index 5398550..91d5c74 100644 --- a/kit/src/spec-url.ts +++ b/kit/src/spec-url.ts @@ -110,6 +110,17 @@ const withVariables = (server: Record): string => { const isAbsolute = (url: string) => /^https?:\/\//i.test(url) +/** The hosts an OAuth sign-in in the description reaches, such as Attio's app.attio.com: lint refuses one not listed. */ +const signInHosts = (document: Record): string[] => { + const components = isRecord(document.components) ? document.components.securitySchemes : undefined + const schemes = [components, document.securityDefinitions].flatMap((one) => (isRecord(one) ? Object.values(one) : [])) + const urls = schemes.filter(isRecord).flatMap((scheme) => { + const flows = isRecord(scheme.flows) ? Object.values(scheme.flows).filter(isRecord) : [scheme] + return flows.flatMap((flow) => [flow.authorizationUrl, flow.tokenUrl, flow.refreshUrl]) + }) + return urls.flatMap((url) => (typeof url === "string" && isAbsolute(url) ? [new URL(url).hostname] : [])) +} + /** The hosts the CLI will call, and a recorded correction when the description names its server relative to itself. */ const serversOf = (kind: DownloadedSpec["kind"], document: Record, specUrl: URL) => { if (kind === "swagger") { @@ -171,7 +182,7 @@ export const downloadSpec = async ( title: typeof info.title === "string" && info.title.trim() ? info.title.trim() : url.hostname, operations: countOperations(document), prefix: sharedPrefix(document), - ...serversOf(kind, document, url), + ...withSignInHosts(serversOf(kind, document, url), document), } } @@ -210,3 +221,8 @@ export const introspectSpec = async (raw: string, fetch: SpecFetch = globalThis. await rm(dir, { recursive: true, force: true }) } } + +const withSignInHosts = (servers: T, document: Record): T => ({ + ...servers, + hosts: [...new Set([...servers.hosts, ...signInHosts(document)])], +}) diff --git a/kit/test/from-config.test.ts b/kit/test/from-config.test.ts index c8245e3..707e856 100644 --- a/kit/test/from-config.test.ts +++ b/kit/test/from-config.test.ts @@ -1,4 +1,4 @@ -import { writeFileSync } from "node:fs" +import { existsSync, writeFileSync } from "node:fs" import { join } from "node:path" import { afterAll, describe, expect, it } from "vitest" import { configArgs, loadCliConfig } from "../src/from-config.js" @@ -26,6 +26,20 @@ const SPEC = JSON.stringify({ openapi: "3.0.3", info: { title: "Acme Public API v2", version: "1" }, servers: [{ url: "https://api.acme.example/v1" }], + components: { + securitySchemes: { + oauth: { + type: "oauth2", + flows: { + authorizationCode: { + authorizationUrl: "https://app.acme.example/authorize", + tokenUrl: "https://app.acme.example/token", + scopes: {}, + }, + }, + }, + }, + }, paths: { "/notes": { get: { operationId: "listNotes", responses: { "200": { description: "ok" } } } } }, }) @@ -92,7 +106,21 @@ describe("new --from-config", () => { expect(read(dir, "src/commands/session.test.ts")).toContain("recalledAccount(") expect(read(dir, "src/identity.ts")).toContain(' syncs: "your people",') const runScope = read(dir, "src/run-scope.test.ts") - expect(runScope).toContain("fetch: withServiceApi(fakeComments(), FAKE_TOKEN).fetch") + expect(runScope).toContain("fetch: fakeServiceApi().fetch") + expect(runScope).not.toContain("comments") + // The sample is left out, and nothing the CLI keeps reaches for it. + expect(existsSync(join(dir, "src/sample"))).toBe(false) + expect(existsSync(join(dir, "src/testing/fake-comments.ts"))).toBe(false) + for (const path of [ + "src/context.ts", + "src/program.ts", + "src/testing/harness.ts", + "src/mcp.test.ts", + "src/api.test.ts", + ]) + expect(read(dir, path), path).not.toMatch(/sample\/|fake-comments|commentsCommand/) + expect(read(dir, "src/identity.ts")).toContain('hosts: ["api.acme.example", "app.acme.example"],') + expect(read(dir, "src/commands/crm.ts")).toContain('live: ["user"]') expect(runScope).toContain("sync: \"cli-core's addCrmCommands runs it in its run") expect(read(dir, "src/program.ts")).toMatch(/apiCommand\(\),\n +\.\.\.crmCommands\(\),/) expect(read(dir, "src/crm-contracts.test.ts")).toContain("crmSourceContract(") diff --git a/kit/test/spec-url.test.ts b/kit/test/spec-url.test.ts index 0d96d20..4d5f4ba 100644 --- a/kit/test/spec-url.test.ts +++ b/kit/test/spec-url.test.ts @@ -72,6 +72,31 @@ describe("downloadSpec", () => { ]) }) + it("lists the hosts an OAuth sign-in in the description reaches beside the API's", async () => { + const attio = JSON.stringify({ + openapi: "3.1.0", + info: { title: "Attio", version: "2" }, + servers: [{ url: "https://api.attio.example" }], + paths: {}, + components: { + securitySchemes: { + oauth2: { + type: "oauth2", + flows: { + authorizationCode: { + authorizationUrl: "https://app.attio.example/authorize", + tokenUrl: "https://app.attio.example/oauth/token", + scopes: {}, + }, + }, + }, + }, + }, + }) + const spec = await downloadSpec("https://api.attio.example/openapi.json", serve({ body: attio }).fetch) + expect(spec.hosts).toEqual(["api.attio.example", "app.attio.example"]) + }) + it("reads a JSON Swagger 2.0 description and takes its host", async () => { const spec = await downloadSpec("https://notes.example/swagger.json", serve({ body: JSON_SPEC }).fetch)