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
24 changes: 24 additions & 0 deletions changes/fix-kit-tracker-pilot-gaps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
### Fixed

- **A tracker CLI made from a config builds.** Its line in WireCat Desktop's manifest listed all seven kinds (64
characters), and `pnpm generate` stopped on `desktop.sync.what`, which takes 40. The line now keeps the kinds a sync
reads last: "your projects, issues and comments".
- **A tracker's sync test fails when a kind fails.** A typo in a variable of the source failed every kind before a
request was sent, and the test passed: it read `issues` where the answer has none (they are under `meta`), and not each kind's `error`.
- **The `api` test of a GraphQL CLI fits a large schema.** It sends what an operation requires plus its optional plain
fields, and an object flag as JSON. A sample of every optional field nests filter in filter (Linear's
`IssueFilter` reaches `TeamFilter` and back), and the worker ran out of memory; an object flag went as
`[object Object]`. Linear's 493 operations now pass in about 20 seconds.
- **No `api` test waits for the pace.** Every case shared the profile's pace file, so past the burst each case
waited for real; the file is cleared after each case.
- **Spell checking skips `skills/*/references/api/`,** written by `pnpm generate` from the service's own words, as it
skips `docs/commands.md`. Linear's descriptions of its API stopped `pnpm docs:check` on twelve words.
- **A tracker CLI from a config runs `trackerSourceContract`** over cli-core's `trackerManifestFixture`, a fake of
its API built from the source, so `cli-dev ready` verifies its category (cli-base 0.119.0 asks a tracker for that
contract). cli-core 0.701.0 and cli-base 0.119.0.
- **A CLI from a config drives every command and option in its tests**, so its test matrix starts with nothing
untested: the session test covers `--credential-storage`, `session list` and `api list`; a tracker gets
`src/tracker-options.test.ts`, which runs each tracker command and option over a synced copy; the GraphQL `api`
test sends every variable, `--body` and `--body-file`, and `--all` on a paged list.
- **`new --from-config` refuses a tracker over an OpenAPI description** with what is missing: cli-core's tracker
fake answers GraphQL only, so its contract test would fail every case.
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "0.2",
"import": ["./config/wirecat/cspell.json"],
"ignorePaths": ["docs/commands.md", "docs/dev/**"],
"ignorePaths": ["docs/commands.md", "docs/dev/**", "skills/*/references/api.md", "skills/*/references/api/**"],
"words": ["Attio", "clidev", "gifsicle", "Petstore", "typechecks", "ydisk"]
}
16 changes: 16 additions & 0 deletions kit/seeds/config/src/commands/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,19 @@ it("session start keeps nothing when the service refuses the token", async () =>
const status = await runCli(["personal", "session", "status", "--json"], { keyring })
expect(status.json().item).toMatchObject({ state: "missing" })
})

it("a sign-in kept in a private file is listed among the sessions; api list names the generated operations", async () => {
const keyring = memoryKeyring()
const started = await runCli(["files", "session", "start", "--credential-storage", "file", "--json"], {
keyring,
service: fakeServiceApi(),
stdin: FAKE_API_TOKEN,
})
expect(started.code, started.err).toBe(0)
const sessions = await runCli(["session", "list", "--json"], { keyring })
expect(sessions.code, sessions.err).toBe(0)
expect(sessions.json().items).toEqual(expect.arrayContaining([expect.objectContaining({ profile: "files" })]))
const operations = await runCli(["api", "list", "--json"], { keyring })
expect((operations.json().items as unknown[]).length).toBeGreaterThan(0)
expect((await runCli(["files", "session", "end", "--json"], { keyring })).code).toBe(0)
})
33 changes: 29 additions & 4 deletions kit/seeds/config/src/tracker-contracts.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import { memoryKeyring } from "@wirecat/cli-base"
import { parseTrackerManifest } from "@wirecat/cli-core/tracker"
import { expect, it } from "vitest"
import {
type TrackerManifestFake,
trackerManifestFixture,
trackerSourceContract,
} from "@wirecat/cli-core/tracker/testing"
import { afterEach, describe, expect, it } from "vitest"
import { API } from "./config.js"
import { SOURCE } from "./generated/wirecat-config.js"
import { definitions } from "./generated/definitions.js"
import { HOOK_OPTIONS, SOURCE } from "./generated/wirecat-config.js"
import { FAKE_API_TOKEN, fakeServiceApi } from "./testing/fake-api.js"
import { runCli } from "./testing/harness.js"

const fakes: TrackerManifestFake[] = []

afterEach(() => {
// A call the fake refused is one the schema or the tracker would refuse: the source would fail on the real service.
expect(fakes.splice(0).flatMap((fake) => fake.refusals)).toEqual([])
})

describe("trackerSourceContract over a fake of the API, from wirecat.config.ts", () => {
for (const { name, run } of trackerSourceContract(() => {
const fixture = trackerManifestFixture(SOURCE, { api: API.manifest, definitions, ...HOOK_OPTIONS })
fakes.push(fixture.fake)
return fixture
}))
it(name, run)
})

it("the source in wirecat.config.ts reads only what the generated API offers and selects", () => {
expect(() => parseTrackerManifest(SOURCE, API.manifest)).not.toThrow()
})
Expand All @@ -22,8 +44,11 @@ it("sync through this CLI sends only calls the schema allows, under the account
expect(started.code, started.err).toBe(0)
const synced = await runCli(["personal", "sync", "--json"], { keyring, service: fake })
expect(synced.code, synced.err).toBe(0)
// A kind that failed is a partial answer with exit 0: its issues name it.
expect(synced.json().issues ?? []).toEqual([])
// A kind that failed is a partial answer with exit 0: its error is on its row, and a typo in a variable fails
// every kind before anything is sent.
const answer = synced.json() as { meta?: { issues?: unknown[] }; item: { synced: { error?: string }[] } }
expect(answer.meta?.issues ?? []).toEqual([])
expect(answer.item.synced.filter((one) => one.error)).toEqual([])
const listed = await runCli(["personal", "issues", "list", "--json"], { keyring, service: fake })
expect(listed.code, listed.err).toBe(0)
expect(fake.refusals).toEqual([])
Expand Down
41 changes: 41 additions & 0 deletions kit/seeds/config/src/tracker-options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { memoryKeyring } from "@wirecat/cli-base"
import { expect, it } from "vitest"
import { FAKE_API_TOKEN, fakeServiceApi } from "./testing/fake-api.js"
import { runCli } from "./testing/harness.js"

// The fake answers a sample of each selection, so this proves every command and option runs, not what a real row holds.
it("every option of the tracker commands runs over a synced copy", async () => {
const keyring = memoryKeyring()
const service = fakeServiceApi()
const started = await runCli(["options", "session", "start", "--json"], { keyring, service, stdin: FAKE_API_TOKEN })
expect(started.code, started.err).toBe(0)
const ran = (argv: string[]) => runCli(["options", ...argv, "--json"], { keyring, service })
const synced = await ran(["sync", "--rescan", "--max-pages", "50"])
expect(synced.code, synced.err).toBe(0)
const listed = (await ran(["issues", "list"])).json() as { items: { key?: string; id?: string; title?: string }[] }
const [issue] = listed.items
expect(issue).toBeDefined()
const ref = String(issue?.key ?? issue?.id)
const word =
String(issue?.title ?? "")
.split(/\W+/)
.find((one) => one.length > 2) ?? ref
for (const argv of [
["issues", "list", "--limit", "1", "--page", "1"],
["issues", "show", ref],
["issues", "search", word, "--limit", "1", "--page", "1"],
["issues", "assign", ref, "--due", "3d"],
...["projects", "teams", "users", "states", "labels"].map((group) => [
group,
"list",
"--limit",
"1",
"--page",
"1",
]),
]) {
const answer = await ran(argv)
expect(answer.code, `${argv.join(" ")}: ${answer.err}`).toBe(0)
}
expect(service.refusals).toEqual([])
})
76 changes: 70 additions & 6 deletions kit/seeds/graphql/src/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { mkdirSync, rmSync, writeFileSync } from "node:fs"
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
import { join } from "node:path"
import { graphqlFake, sampleOf } from "@wirecat/cli-base/codegen"
import { graphqlFake, type SchemaNode, sampleOf } from "@wirecat/cli-base/codegen"
import { apiFlagOf, manifestBaseUrl, sendsBytes } from "@wirecat/cli-core/cli"
import { pacePathFor } from "@wirecat/cli-core/sends"
import { afterEach, expect, it } from "vitest"
import { API } from "./config.js"
import { definitions } from "./generated/definitions.js"
import { IDENTITY } from "./identity.js"
import { FAKE_TOKEN } from "./testing/fake-comments.js"
import { runCli } from "./testing/harness.js"

Expand All @@ -14,9 +16,11 @@ const withPermissions = (permissions: Record<string, string>) => {
mkdirSync(join(configFile, ".."), { recursive: true })
writeFileSync(configFile, JSON.stringify({ profiles: { personal: { permissions } } }))
}
// Every case shares the profile's pace file, and a few hundred operations would wait for real past its burst.
afterEach(() => {
rmSync(configFile, { force: true })
rmSync(upload, { force: true })
rmSync(pacePathFor(IDENTITY, "personal", process.env), { force: true })
})

type Operation = (typeof API.manifest.operations)[number]
Expand All @@ -38,13 +42,45 @@ const deleteKeys = [
),
]

// What an operation requires, plus its optional fields that hold a plain value. GraphQL inputs nest in each other
// (Linear's IssueFilter reaches TeamFilter and back), so sampling every optional object grows past memory and past
// what one argument can hold.
const NESTED = new Set(["ref", "object", "array", "union", "allOf"])
const requiredSample = (node: SchemaNode, seen: ReadonlySet<string> = new Set()): unknown => {
if (node.type === "ref") {
const target = definitions[node.ref as keyof typeof definitions] as SchemaNode | undefined
return target && !seen.has(node.ref) ? requiredSample(target, new Set([...seen, node.ref])) : null
}
if (node.type === "object")
return Object.fromEntries(
Object.entries(node.properties)
.filter(([name, property]) => node.required.includes(name) || !NESTED.has(property.type))
.map(([name, property]) => [name, requiredSample(property, seen)]),
)
if (node.type === "array") {
const item = requiredSample(node.items, seen)
return item === null ? [] : Array.from({ length: Math.max(node.minItems ?? 1, 1) }, () => item)
}
return sampleOf(node, definitions)
}

const flagValue = (value: unknown) => (typeof value === "object" ? JSON.stringify(value) : String(value))

const bodyFileOf = (operation: Operation) => {
const path = join(upload, "..", `${operation.command}.json`)
writeFileSync(path, JSON.stringify(requiredSample({ type: "ref", ref: String(operation.request?.schema) })))
return path
}

const argsOf = (operation: Operation) => [
...operation.parameters
.filter((parameter) => parameter.required)
.flatMap((parameter) => [`--${apiFlagOf(parameter.name)}`, String(sampleOf(parameter.schema, definitions))]),
// Every variable as its flag, so each flag is driven once; an optional input object goes as its required part.
...operation.parameters.flatMap((parameter) => [
`--${apiFlagOf(parameter.name)}`,
flagValue(requiredSample(parameter.schema)),
]),
...(sendsBytes(operation) ? ["--body-file", upload] : []),
...(operation.request?.schema
? ["--body", JSON.stringify(sampleOf({ type: "ref", ref: operation.request.schema }, definitions))]
? ["--body", JSON.stringify(requiredSample({ type: "ref", ref: operation.request.schema }))]
: []),
...(operation.effect === "destructive" ? ["--yes"] : []),
]
Expand Down Expand Up @@ -73,6 +109,34 @@ it.each(API.manifest.operations.map((operation) => [operation.command, operation
expect(ran.code, ran.err).toBe(0)
expect(service.refusals).toEqual([])
expect(service.calls).toHaveLength(1)
if (operation.request?.schema) {
// The body file alone: with the variables also given as flags, an ignored file would still send them.
const bodyFile = bodyFileOf(operation)
const fromFile = await runCli(
[
"personal",
"api",
operation.command,
"--body-file",
bodyFile,
...(operation.effect === "destructive" ? ["--yes"] : []),
"--json",
],
{ service },
)
expect(fromFile.code, fromFile.err).toBe(0)
expect(service.calls[1]?.variables).toEqual(JSON.parse(readFileSync(bodyFile, "utf8")))
}
if (!operation.pagination) return
// The fake's sample of pageInfo says no next page, so --all sends one more call and ends.
const before = service.calls.length
const all = await runCli(["personal", "api", operation.command, ...argsOf(operation), "--all", "--json"], {
service,
})
expect(all.code, all.err).toBe(0)
expect(service.calls).toHaveLength(before + 1)
expect(service.calls.at(-1)?.operation).toBe(operation.id)
expect(service.refusals).toEqual([])
},
)

Expand Down
4 changes: 4 additions & 0 deletions kit/seeds/shared/src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { mkdirSync, rmSync, writeFileSync } from "node:fs"
import { join } from "node:path"
import { fakeApi, sampleOf } from "@wirecat/cli-base/codegen"
import { apiFlagOf, manifestBaseUrl, type ServiceApiManifest, sendsBytes } from "@wirecat/cli-core/cli"
import { pacePathFor } from "@wirecat/cli-core/sends"
import { afterEach, expect, it } from "vitest"
import { API } from "./config.js"
import { definitions } from "./generated/definitions.js"
import { IDENTITY } from "./identity.js"
import { FAKE_TOKEN } from "./testing/fake-comments.js"
import { runCli } from "./testing/harness.js"

Expand All @@ -14,9 +16,11 @@ const withPermissions = (permissions: Record<string, string>) => {
mkdirSync(join(configFile, ".."), { recursive: true })
writeFileSync(configFile, JSON.stringify({ profiles: { personal: { permissions } } }))
}
// Every case shares the profile's pace file, and a few hundred operations would wait for real past its burst.
afterEach(() => {
rmSync(configFile, { force: true })
rmSync(upload, { force: true })
rmSync(pacePathFor(IDENTITY, "personal", process.env), { force: true })
})

type Operation = (typeof API.manifest.operations)[number]
Expand Down
5 changes: 5 additions & 0 deletions kit/src/from-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export const loadCliConfig = async (path: string): Promise<LoadedConfig> => {
throw new Error(`${path}: export the config as default: export default defineCli({…})`)
const config = parseCliConfig(module.default)
const verifies = config.auth.preset === "token" && config.auth.verify !== undefined
// cli-core's trackerManifestFake, which the tracker's contract test runs over, answers GraphQL only.
if (config.category === "tracker" && config.spec.kind !== "graphql")
throw new Error(
"wirecat.config: a tracker source reads a GraphQL schema yet; cli-core's tracker fake answers no other API",
)
// The source's commands key the store by the account session start checked; the sample's session checks nothing.
if (config.source && !verifies)
throw new Error(
Expand Down
20 changes: 15 additions & 5 deletions kit/src/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,21 @@ const KIND_WORDS: Readonly<Record<string, string>> = {
lead: "leads",
}

/** A CLI with `sync` says what it copies in (contracts/wirecat.json): for a config's source, its kinds. */
// The manifest's desktop.sync.what takes 40 characters at most.
const SYNCS_MAX = 40

const syncsOf = (words: readonly string[]): string =>
`your ${words.length > 1 ? `${words.slice(0, -1).join(", ")} and ${words.at(-1)}` : (words[0] ?? "records")}`

/**
* A CLI with `sync` says what it copies in (contracts/wirecat.json): for a config's source, its kinds. Kinds come
* in the order a sync reads them, the ones others refer to first, so a list too long keeps the last ones: a
* tracker's issues, projects and comments rather than its teams and states.
*/
export const withSyncs = (text: string, kinds: readonly string[]): string => {
const words = kinds.map((kind) => KIND_WORDS[kind] ?? `${kind}s`)
const list = words.length > 1 ? `${words.slice(0, -1).join(", ")} and ${words.at(-1)}` : (words[0] ?? "records")
return replaceOnce(IDENTITY_FILE, text, /^( {2}tagline: .*\n)/m, `$1 syncs: ${JSON.stringify(`your ${list}`)},\n`)
let words = kinds.map((kind) => KIND_WORDS[kind] ?? `${kind}s`)
while (words.length > 1 && syncsOf(words).length > SYNCS_MAX) words = words.slice(1)
return replaceOnce(IDENTITY_FILE, text, /^( {2}tagline: .*\n)/m, `$1 syncs: ${JSON.stringify(syncsOf(words))},\n`)
}

export const withContentLanguages = (text: string, languages: readonly string[]): string => {
Expand Down Expand Up @@ -743,7 +753,7 @@ const SOURCE_CHECK = "scripts/source-check.ts"
const FROM_CONFIG = "scripts/from-config.ts"
const SESSION = ["src/commands/session.ts", "src/commands/session.test.ts", "src/testing/fake-api.ts"]
const CRM = ["src/commands/crm.ts", "src/crm-contracts.test.ts", "lint/contract-exceptions.json"]
const TRACKER = ["src/commands/tracker.ts", "src/tracker-contracts.test.ts"]
const TRACKER = ["src/commands/tracker.ts", "src/tracker-contracts.test.ts", "src/tracker-options.test.ts"]

const sourceKind = (config: LoadedConfig["config"]): "crm" | "tracker" =>
config.category === "tracker" ? "tracker" : "crm"
Expand Down
12 changes: 12 additions & 0 deletions kit/test/from-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ describe("new --from-config", () => {
await expect(loadCliConfig(join(cwd, "wirecat.config.ts"))).rejects.toThrow("a source needs auth")
})

it("refuses a tracker source over an OpenAPI description", async () => {
const cwd = tempDir("from-config-tracker-openapi")
writeFileSync(
join(cwd, "wirecat.config.ts"),
CONFIG.replace('category: "other",', 'category: "tracker",\n source: { provider: "acme" },'),
)

await expect(loadCliConfig(join(cwd, "wirecat.config.ts"))).rejects.toThrow(
"a tracker source reads a GraphQL schema",
)
})

it("refuses a flag or a folder name the config already sets", async () => {
const cwd = tempDir("from-config-twice")
writeFileSync(join(cwd, "wirecat.config.ts"), CONFIG)
Expand Down
10 changes: 10 additions & 0 deletions kit/test/new.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
takenMinutes,
wirecatMonthlyTime,
withContentLanguages,
withSyncs,
} from "../src/new.js"
import { renamer, stripKit } from "../src/rename.js"
import { listTemplate, loadSource, renderTemplate } from "../src/source.js"
Expand Down Expand Up @@ -109,6 +110,15 @@ describe("new", () => {
})
})

it("says what a config's sync copies within the manifest's 40 characters, keeping the kinds read last", () => {
const identity = ' tagline: "x",\n'
expect(withSyncs(identity, ["organization", "person", "deal"])).toContain(
'syncs: "your companies, people and deals"',
)
const tracker = ["team", "user", "state", "label", "project", "issue", "comment"]
expect(withSyncs(identity, tracker)).toContain('syncs: "your projects, issues and comments"')
})

it("picks a WireCat CLI's monthly minute inside WC-CI-9's window, from the free ones", () => {
const taken = takenMinutes("| UTC | Repository |\n|---|---|\n| 02:17 | cli-base |\n| 02:20 | notion-cli |\n")
const times = ["a-cli", "b-cli", "c-cli", "d-cli"].map((repo) => wirecatMonthlyTime(repo, taken))
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@
"kit:docs": "cspell lint --config cspell.json --no-progress --no-summary kit/*.md SKILL.md && rumdl check --config config/wirecat/rumdl.toml kit/*.md SKILL.md"
},
"dependencies": {
"@wirecat/cli-base": "0.117.0",
"@wirecat/cli-core": "0.699.0",
"@wirecat/cli-base": "0.119.0",
"@wirecat/cli-core": "0.701.0",
"commander": "15.0.0",
"valibot": "1.5.0",
"yaml": "2.9.1"
Expand Down
Loading
Loading