Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions changes/test-kit-e2e-from-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
### Fixed

- **A CLI made by `new --from-config` passes its own `pnpm test:coverage`.** A CRM or tracker CLI with `auth.verify`
failed it (a tracker reached 52% of branches against 75%): the seed held the token check and branches on the
config's values that no test of that CLI could take both ways. The check is now cli-core's `manifestVerify`,
`pnpm generate` writes the config's values with their exact types (a removed `verify` fails typecheck), the hosts-only
fetch is cli-core's `hostsFetch`, the CRM store is cli-core's `openAppStore`, and new tests cover a sync before
`session start`, `--offline` and `api show`.
- **The daily "a newer version exists" line shows.** `src/program.ts` set up the update check but never handed it to
cli-core's `run()`, so no CLI made from the template printed it.
- **The `--offline` refusal is cli-core's `refuseOffline`**, in place of a copy in `src/context.ts`.

### Added

- **`kit:e2e` makes two CLIs with `new --from-config`**, a CRM over an OpenAPI description and a tracker over a GraphQL
schema, both with a token check and so without the sample, and runs the same checks as the others. The path was
checked only by hand before.

### Changed — may break callers

- **A new CLI pins `@wirecat/cli-core` 0.699.0**, which has `manifestVerify` and `hostsFetch`.
8 changes: 5 additions & 3 deletions kit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ read before the CLI has `node_modules`: its import resolves to the kit's own cli
`parseCliConfig`. In the CLI:

- `pnpm generate` first writes `src/generated/wirecat-config.ts` from the config (`scripts/from-config.ts`): the
token check, the source and the hooks, which `src/` reads; the config stays the one file to edit.
token check, the source and the hooks, which `src/` reads; the config stays the one file to edit. Each value has its
exact type, and one the config leaves out is a bare `undefined`: a command that still reads it fails typecheck, not a
run.
- 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. `verify.active` names a field that must be `true`, for a service that
- With `auth.verify`, `session start` is cli-core's `tokenSessionCommand` over `manifestVerify`: it checks a token with
that operation and keeps the account's id from the answer; the CLI holds no check of its own. `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
Expand Down
49 changes: 37 additions & 12 deletions kit/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,19 @@ pnpm build >/dev/null
tarball=$(npm pack --silent --ignore-scripts --pack-destination "$work" | tail -n 1)
npm install --silent --no-save --prefix "$work/install" "$work/$tarball"

# build <name> <command> <category> <spec>; outside @wirecat, as a third party makes one, and with every flag new takes.
build() {
mkdir "$work/new-$2"
cd "$work/new-$2"
"$work/install/node_modules/.bin/cli-template" new "$1" --command "$2" --package "@example/$1" \
--owner example --tagline "Made by kit e2e" --category "$3" --content-lang en --sign-in none \
--api official-docs --spec "$4" --filters time --never-store tokens --untrusted none >/dev/null
cmp "$root/.gitignore" "$1/.gitignore"
cd "$1"
# checks <command> <spec>: what a CLI made by new must pass, in its own folder.
checks() {
pnpm install
# As new's next steps say: the rename changes line lengths, so Biome formats the renamed files once.
pnpm lint:fix
if [ "$4" != none ]; then
if [ "$2" != none ]; then
# A fresh CLI builds and runs before its first pnpm generate.
pnpm build
env -u XDG_CONFIG_HOME -u XDG_DATA_HOME -u XDG_STATE_HOME -u XDG_CACHE_HOME HOME="$work/home" \
DBUS_SESSION_BUS_ADDRESS=disabled: node "dist/bin/$2.js" doctor >/dev/null
DBUS_SESSION_BUS_ADDRESS=disabled: node "dist/bin/$1.js" doctor >/dev/null
fi
pnpm generate
if [ "$4" != none ]; then
if [ "$2" != none ]; then
pnpm exec cli-contract contracts/plan.json --update
pnpm exec vitest run src/mcp.test.ts --update
pnpm lint:fix
Expand All @@ -43,11 +36,43 @@ build() {
pnpm contracts:check
pnpm test:coverage
pnpm docs:check
}

# build <name> <command> <category> <spec>; outside @wirecat, as a third party makes one, and with every flag new takes.
build() {
mkdir "$work/new-$2"
cd "$work/new-$2"
"$work/install/node_modules/.bin/cli-template" new "$1" --command "$2" --package "@example/$1" \
--owner example --tagline "Made by kit e2e" --category "$3" --content-lang en --sign-in none \
--api official-docs --spec "$4" --filters time --never-store tokens --untrusted none >/dev/null
cmp "$root/.gitignore" "$1/.gitignore"
cd "$1"
checks "$2" "$4"
echo "kit e2e: new $1 ($2, $3, --spec $4) passed lint, generate, ci:quick, contracts:check, test:coverage and docs:check"
}

# from_config <fixture> <command> <spec URL> <spec file>: new --from-config over kit/test/from-config/<fixture>, a config
# with a token check (auth.verify) and a source, so the CLI starts without the sample. The preload answers the spec URL
# from the file, for `new` only: install and generate run without it.
from_config() {
mkdir "$work/config-$2"
cp "$root/kit/test/from-config/$1/wirecat.config.ts" "$work/config-$2/"
cd "$work/config-$2"
E2E_SPEC_URL="$3" E2E_SPEC_FILE="$root/kit/test/from-config/$1/$4" \
node --import "$root/kit/test/from-config/spec-fetch.mjs" \
"$work/install/node_modules/@wirecat/cli-template/kit/dist/main.js" \
new --from-config wirecat.config.ts --content-lang en --api official-docs --filters text --never-store tokens \
--untrusted records >/dev/null
[ ! -e "$2-cli/src/sample" ]
cd "$2-cli"
checks "$2" "$1"
echo "kit e2e: new --from-config ($1, $2) started without the sample and passed the same checks"
}

build demo-cli demo notes openapi
build acme-workspace-cli acme-workspace crm none
from_config crm acme https://docs.acme.example/openapi.json openapi.json
from_config tracker acmet https://api.acme-tracker.example/graphql schema.json

cd "$work/new-demo/demo-cli"
bin=$(node -p 'require("./package.json").bin.demo')
Expand Down
28 changes: 22 additions & 6 deletions kit/seeds/config/scripts/from-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ if (functions.length)
"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))
// A value the config leaves out is a bare `undefined`, so a command that still reads it fails typecheck, not a run.
const constant = (name: string, type: string, value: unknown) =>
value === undefined
? `export const ${name} = undefined`
: `export const ${name}: ${type} = ${JSON.stringify(value, null, 2)}`
// The hooks module is imported from src/generated/, inside the bundle: it must live under src/.
const hooksImport = (() => {
if (!cli.hooks) return undefined
Expand All @@ -49,24 +53,36 @@ 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; active?: string } | undefined = ${literal(verify)}`,
constant("VERIFY", "{ operation: string; id: string; name?: string; active?: string }", verify),
"",
`export const SOURCE: Readonly<Record<string, unknown>> | undefined = ${literal(cli.source)}`,
constant(
"SOURCE",
"Readonly<Record<string, unknown>> & { readonly provider: string; readonly kinds: readonly string[] }",
cli.source,
),
"",
// Spread into the source's options: `{}` without a hooks module.
...(hooksImport
? [`export { hooks as HOOKS } from "${hooksImport}"`]
? [`import { hooks } from "${hooksImport}"`, "", "export const HOOK_OPTIONS = { hooks }"]
: cli.category === "tracker"
? [
'import type { TrackerManifestHooks } from "@wirecat/cli-core/tracker"',
"",
"export const HOOKS: TrackerManifestHooks | undefined = undefined",
"export const HOOK_OPTIONS: { hooks?: TrackerManifestHooks } = {}",
]
: [
'import type { CrmManifestHooks } from "@wirecat/cli-core/crm"',
"",
"export const HOOKS: CrmManifestHooks | undefined = undefined",
"export const HOOK_OPTIONS: { hooks?: CrmManifestHooks } = {}",
]),
"",
// What a CRM reads live: its users, when the source names a call for them.
...(cli.category === "crm"
? [
`export const LIVE: readonly "user"[] = ${cli.source && "users" in cli.source && cli.source.users ? '["user"]' : "[]"}`,
"",
]
: []),
].join("\n"),
)

Expand Down
64 changes: 18 additions & 46 deletions kit/seeds/config/src/commands/crm.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,20 @@
import { CliError } from "@wirecat/cli-base"
import { hostFetch, retrying } from "@wirecat/cli-base/http"
import { recalledAccount, serviceApiCall } from "@wirecat/cli-core/cli"
import { retrying } from "@wirecat/cli-base/http"
import { openAppStore, recalledAccount, serviceApiCall } from "@wirecat/cli-core/cli"
import { crmSourceFromManifest } from "@wirecat/cli-core/crm"
import { addCrmCommands, type CrmRun, openCrmStore } from "@wirecat/cli-core/crm/cli"
import { Pacer, pacePathFor } from "@wirecat/cli-core/sends"
import type { MessageStore } from "@wirecat/cli-core/store"
import { Command } from "commander"
import { API } from "../config.js"
import { contextOf, PACE, tokenOf } from "../context.js"
import { HOOKS, SOURCE } from "../generated/wirecat-config.js"
import { DESKTOP, IDENTITY } from "../identity.js"

const provider = (): string => {
if (typeof SOURCE?.provider !== "string") throw new CliError("configuration_error", "wirecat.config.ts has no source")
return SOURCE.provider
}

/**
* The shared store, with this CLI's provider entry written, so every other CLI's search knows it (WC-ARCH-21). A
* read-only open writes nothing, the entry included.
*/
const openStore = async (env: NodeJS.ProcessEnv, { readOnly = false } = {}): Promise<MessageStore> => {
const { ensureSqlite } = await import("@wirecat/cli-core/sqlite-runtime")
await ensureSqlite()
const { openStore: open, storePath } = await import("@wirecat/cli-core/store")
const store = await open({
path: storePath(env),
env,
command: IDENTITY.command,
version: IDENTITY.version,
...(readOnly ? { readOnly } : {}),
})
try {
if (!readOnly)
await store.accounts.registerProvider({ name: provider(), kind: "crm", aliases: [], command: IDENTITY.command })
return store
} catch (error) {
await store.close()
throw error
}
}
import { HOOK_OPTIONS, LIVE, SOURCE } from "../generated/wirecat-config.js"
import { IDENTITY } from "../identity.js"
import { serviceFetch } from "./api.js"

// The account is the one `session start` checked the token against; the store keeps each account's rows apart.
const accountOf = (command: Command) => {
const { env, profile } = contextOf(command)
const account = recalledAccount(IDENTITY, provider(), profile, env)
const account = recalledAccount(IDENTITY, SOURCE.provider, profile, env)
if (!account)
throw new CliError(
"authentication_error",
Expand All @@ -63,14 +33,17 @@ const open = async (command: Command, { readOnly }: { readOnly: boolean }): Prom
env,
profile,
readOnly,
provider: provider(),
open: openStore,
provider: SOURCE.provider,
// The shared store, with this CLI's provider entry written so every other CLI's search knows it (WC-ARCH-21).
open: (at, options) =>
openAppStore(at, {
app: IDENTITY,
provider: { name: SOURCE.provider, kind: "crm", aliases: [], command: IDENTITY.command },
...options,
}),
account: () => accountOf(command),
})
const fetch = retrying(
hostFetch({ hosts: DESKTOP.hosts ?? [], ...(environment.fetch ? { fetch: environment.fetch } : {}) }),
{ pacer: new Pacer(pacePathFor(IDENTITY, profile, env), PACE) },
)
const fetch = retrying(serviceFetch(environment), { pacer: new Pacer(pacePathFor(IDENTITY, profile, env), PACE) })
const call = serviceApiCall({ manifest: API.manifest, service: API.service, fetch, token: () => tokenOf(command) })
let source: ReturnType<typeof crmSourceFromManifest> | undefined
return {
Expand All @@ -81,8 +54,7 @@ const open = async (command: Command, { readOnly }: { readOnly: boolean }): Prom
store,
env,
locked: Boolean(env[`${IDENTITY.envPrefix}_PROFILE_LOCK`]),
source: () =>
(source ??= crmSourceFromManifest(SOURCE, { api: API.manifest, call, ...(HOOKS ? { hooks: HOOKS } : {}) })),
source: () => (source ??= crmSourceFromManifest(SOURCE, { api: API.manifest, call, ...HOOK_OPTIONS })),
close: () => store.close(),
}
}
Expand All @@ -93,9 +65,9 @@ export const crmCommands = (): Command[] => {
addCrmCommands(holder, {
app: IDENTITY,
open,
leads: Array.isArray(SOURCE?.kinds) && SOURCE.kinds.includes("lead"),
leads: 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 } : {}),
live: LIVE,
})
return [...holder.commands]
}
2 changes: 1 addition & 1 deletion kit/seeds/config/src/commands/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ it("session start checks the token with the call auth.verify names and remembers
stdin: FAKE_API_TOKEN,
})
expect(started.code, started.err).toBe(0)
expect(fake.calls.map((call) => call.operation)).toEqual([VERIFY?.operation])
expect(fake.calls.map((call) => call.operation)).toEqual([VERIFY.operation])
expect(fake.refusals).toEqual([])
const { id } = started.json().item as { id: string }
expect(id).toEqual(expect.any(String))
Expand Down
35 changes: 9 additions & 26 deletions kit/seeds/config/src/commands/session.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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 { manifestVerify, tokenSessionCommand } from "@wirecat/cli-core/cli"
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"
import { IDENTITY } from "../identity.js"
import { serviceFetch } from "./api.js"

/**
* `session start | status | end | list` (WC-ACCT-12): the token is checked by the call `auth.verify` names in
Expand All @@ -17,25 +15,10 @@ export const sessionCommand = (): Command =>
app: IDENTITY,
service: API.service,
resolveSettings: configuration.resolveSettings,
verify: async (command, token) => {
if (!VERIFY) throw new CliError("configuration_error", "wirecat.config.ts names no auth.verify call")
const { environment } = contextOf(command)
const call = serviceApiCall({
manifest: API.manifest,
service: API.service,
fetch: hostFetch({ hosts: DESKTOP.hosts ?? [], ...(environment.fetch ? { fetch: environment.fetch } : {}) }),
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}`, {
reason: "verify.no-id",
})
const name = VERIFY.name ? valueAt(answer, VERIFY.name) : undefined
return { id: String(id), account: { id: String(id), ...(typeof name === "string" ? { name } : {}) } }
},
verify: manifestVerify({
manifest: API.manifest,
service: API.service,
verify: VERIFY,
fetch: (command) => serviceFetch(contextOf(command).environment),
}),
})
24 changes: 8 additions & 16 deletions kit/seeds/config/src/commands/tracker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CliError } from "@wirecat/cli-base"
import { hostFetch, retrying } from "@wirecat/cli-base/http"
import { retrying } from "@wirecat/cli-base/http"
import { recalledAccount, serviceApiCall } from "@wirecat/cli-core/cli"
import { openCrmStore } from "@wirecat/cli-core/crm/cli"
import { Pacer, pacePathFor } from "@wirecat/cli-core/sends"
Expand All @@ -8,13 +8,9 @@ import { addTrackerCommands, type TrackerRun, trackerSourceFromManifest } from "
import { Command } from "commander"
import { API } from "../config.js"
import { contextOf, PACE, tokenOf } from "../context.js"
import { HOOKS, SOURCE } from "../generated/wirecat-config.js"
import { DESKTOP, IDENTITY } from "../identity.js"

const provider = (): string => {
if (typeof SOURCE?.provider !== "string") throw new CliError("configuration_error", "wirecat.config.ts has no source")
return SOURCE.provider
}
import { HOOK_OPTIONS, SOURCE } from "../generated/wirecat-config.js"
import { IDENTITY } from "../identity.js"
import { serviceFetch } from "./api.js"

// A tracker's items are not a kind the shared search reads yet, so the store gets no provider entry.
const openStore = async (env: NodeJS.ProcessEnv, { readOnly = false } = {}): Promise<MessageStore> => {
Expand All @@ -33,7 +29,7 @@ const openStore = async (env: NodeJS.ProcessEnv, { readOnly = false } = {}): Pro
// The account is the one `session start` checked the token against; the store keeps each account's rows apart.
const accountOf = (command: Command) => {
const { env, profile } = contextOf(command)
const account = recalledAccount(IDENTITY, provider(), profile, env)
const account = recalledAccount(IDENTITY, SOURCE.provider, profile, env)
if (!account)
throw new CliError(
"authentication_error",
Expand All @@ -50,21 +46,17 @@ const open = async (command: Command, { readOnly }: { readOnly: boolean }): Prom
env,
profile,
readOnly,
provider: provider(),
provider: SOURCE.provider,
open: openStore,
account: () => accountOf(command),
})
const fetch = retrying(
hostFetch({ hosts: DESKTOP.hosts ?? [], ...(environment.fetch ? { fetch: environment.fetch } : {}) }),
{ pacer: new Pacer(pacePathFor(IDENTITY, profile, env), PACE) },
)
const fetch = retrying(serviceFetch(environment), { pacer: new Pacer(pacePathFor(IDENTITY, profile, env), PACE) })
const call = serviceApiCall({ manifest: API.manifest, service: API.service, fetch, token: () => tokenOf(command) })
let source: ReturnType<typeof trackerSourceFromManifest> | undefined
return {
account,
store,
source: () =>
(source ??= trackerSourceFromManifest(SOURCE, { api: API.manifest, call, ...(HOOKS ? { hooks: HOOKS } : {}) })),
source: () => (source ??= trackerSourceFromManifest(SOURCE, { api: API.manifest, call, ...HOOK_OPTIONS })),
close: () => store.close(),
}
}
Expand Down
Loading
Loading