diff --git a/changes/fix-crm-manifest-attio.md b/changes/fix-crm-manifest-attio.md new file mode 100644 index 000000000..cc03021e2 --- /dev/null +++ b/changes/fix-crm-manifest-attio.md @@ -0,0 +1,17 @@ +### Added + +- **`auth.verify.active` in `wirecat.config.ts`** names where the token check's answer says the token still works. + Attio's `GET /v2/self` answers 200 `{ "active": false }` to an unknown, revoked or deleted token; a CLI can now + refuse that sign-in instead of failing on a missing id. +- **`valueAt` from `@wirecat/cli-core/manifest`** reads a dotted path into an answer as the source manifest does, so + a CLI's sign-in reads `auth.verify`'s paths the same way. + +### Fixed + +- **A value the service cleared clears the stored copy.** A path past the end of a list, or under a `null` the + service sent, is now `null`, not "not sent": Attio's `values.job_title.0.value` on an empty list used to keep the + old position ([CRM.md](../docs/dev/CRM.md#a-source-from-a-manifest)). Watch for: an `each` rule under such + a `null` now gives an empty list, so the record's links are cleared too. +- **`crmManifestFake` takes its own answer when a value is one of several types.** It wrote a mapped value into a + sample of the union's first branch, and its strict fake refused the row: every contract case of an Attio CLI + failed. The row is now sampled from the branch each mapped path needs, through objects, lists and `allOf`. diff --git a/docs/dev/CRM.md b/docs/dev/CRM.md index 2e1e0a61f..e78ac8771 100644 --- a/docs/dev/CRM.md +++ b/docs/dev/CRM.md @@ -236,6 +236,8 @@ hooks })` builds the `CrmSource` (design and evidence: cli-private `plans/2026-1 `serviceApiCall` over the same fetch and token as the `api` group; in a test, the same over cli-base `fakeApi`. - Paging comes from each operation's `pagination`; an operation without it names its rows with `items`. - A path the answer does not have gives no key, never `null`: a read that did not select a field must not erase it. +- A path past the end of a list, or under a `null`, the answer has is `null`: Attio's `values.job_title.0.value` on + an empty list clears the copy's position. - A change read sorted by modified time names `keyset`: the next page starts at the last row's time, so a row that changes mid-round moves nothing under the next page (a service's own page token is an offset into a moving list). Past `cap` rows at one time the round answers `reset`. @@ -275,6 +277,8 @@ for (const { name, run } of crmManifestContract(SOURCE, { api, definitions, hook contact or company has `get` (after checking that `get` reads the row), `change` when a kind has `changes`, `skewed` and `sameToken`. `crmManifestContract` runs `crmSourceContract` over it and fails a case whose calls the fake refused. +- A value written inside a union (Attio's seventeen value types) goes into the branch that has its path, so the strict + fake takes its own answer. - A rule that is a function, an `each` or a `{ value }` writes nothing: the row keeps the sample there. `self` and a live `list` come from hooks, so their cases pass without running unless the hooks add them. The dead cursor is answered by the source itself (`reset` for a cursor that is not a time) and never reaches the fake. diff --git a/src/crm/manifest/manifest.test.ts b/src/crm/manifest/manifest.test.ts index b7ec238e6..f22969fe7 100644 --- a/src/crm/manifest/manifest.test.ts +++ b/src/crm/manifest/manifest.test.ts @@ -9,6 +9,7 @@ import { crmManifestContract } from "../testing/manifest-fake.js" import { crmSourceContract } from "../testing/source-contract.js" import { amountOf, type CrmRaw } from "../values.js" import { type CrmManifest, parseCrmManifest } from "./manifest.js" +import { mapFields } from "./map.js" import { crmManifestProblems } from "./paths.js" import { crmSourceFromManifest } from "./source.js" @@ -656,6 +657,21 @@ describe("crmSourceFromManifest", () => { expect(ben).toMatchObject({ name: "Ben", lastName: null, organizations: [] }) }) + it("reads a value under an empty list or a null the service sent as none, so a cleared value clears the copy", () => { + const rules = { externalId: "id", position: "values.job_title.0.value", url: "values.site.value" } + const types = { externalId: "id", position: "text", url: "text" } as const + expect(mapFields(rules, types, { id: "1", values: { job_title: [], site: null } })).toEqual({ + externalId: "1", + position: null, + url: null, + }) + expect(mapFields(rules, types, { id: "1", values: {} })).toEqual({ externalId: "1" }) + const links = { externalId: "id", organizations: { each: "values.company", id: "target_record_id" } } as const + expect( + mapFields(links, { externalId: "id", organizations: "organizations" }, { id: "1", values: { company: null } }), + ).toEqual({ externalId: "1", organizations: [] }) + }) + it("asks only for the properties the map reads and the account's own fields", async () => { const hub = fakeHubspot() await sourceOver(hub).changes("deal", null) diff --git a/src/crm/manifest/map.ts b/src/crm/manifest/map.ts index 5d3985bed..433f1a937 100644 --- a/src/crm/manifest/map.ts +++ b/src/crm/manifest/map.ts @@ -7,20 +7,27 @@ import type { CrmRule } from "./manifest.js" export const MISSING = Symbol("missing") type Read = unknown | typeof MISSING -/** A dotted path; a number steps into a list (`results.0.id`), and `.` is the value itself. */ +/** + * A dotted path; a number steps into a list (`results.0.id`), and `.` is the value itself. Past the end of a list the + * service sent, or under a `null` it sent, is "none" (`null`), not "not sent": a cleared value must clear the copy, + * as Attio's `values.job_title.0.value` on an empty list. + */ export const at = (value: unknown, path: string): unknown => path === "." ? value - : path.split(".").reduce( - (inner, key) => - // Past the end of a list the service sent is "none", not "not sent": a removed link must leave the copy. - Array.isArray(inner) && /^\d+$/.test(key) - ? (inner[Number(key)] ?? null) - : isRaw(inner) - ? inner[key] - : undefined, - value, - ) + : path + .split(".") + .reduce( + (inner, key) => + inner === null + ? null + : Array.isArray(inner) && /^\d+$/.test(key) + ? (inner[Number(key)] ?? null) + : isRaw(inner) + ? inner[key] + : undefined, + value, + ) const read = (raw: CrmRaw, path: string): Read => { const found = at(raw, path) diff --git a/src/crm/testing/manifest-fake.test.ts b/src/crm/testing/manifest-fake.test.ts index 17516c33d..b5d6f8ebb 100644 --- a/src/crm/testing/manifest-fake.test.ts +++ b/src/crm/testing/manifest-fake.test.ts @@ -279,3 +279,53 @@ describe("crmManifestContract catches a broken source", () => { expect(fake.refusals).toEqual(["records_query: a read of changes without its since"]) }) }) + +// Attio's real value schema: a oneOf of typed values, each with its own fields and an enum attribute_type, so a value +// sampled from the first branch with another branch's field written in matches none of them. +describe("crmManifestFake over values that are one of several types", () => { + const typedValue = (type: string, fields: Record) => + obj({ active_from: str, attribute_type: { type: "string", enum: [type] }, ...fields }, [ + "active_from", + "attribute_type", + ...Object.keys(fields), + ]) + const typed: Record = { + ...definitions, + Value: { + type: "union", + of: [ + typedValue("actor-reference", { referenced_actor_id: str }), + typedValue("text", { value: str }), + typedValue("personal-name", { full_name: str, first_name: str, last_name: str }), + typedValue("email-address", { email_address: str }), + ], + }, + } + + it("writes each mapped value into the branch that has it, and the strict fake takes its own answer", async () => { + const { source, fake } = crmManifestFixture(SOURCE, { api, definitions: typed }) + const people = (await source.changes("person", null)) as CrmPage + expect(people.items.map(({ name, lastName }) => [name, lastName])).toEqual([ + ["name 1", "lastName 1"], + ["name 2", "lastName 2"], + ]) + const raw = fake.rows.person?.[0] as { values: Record } + expect(raw.values.name?.[0]?.attribute_type).toBe("personal-name") + expect(fake.refusals).toEqual([]) + }) + + it("does the same when the row's first sample already holds the list, as a value object with fixed fields", async () => { + const entry = typed.Entry as Extract + const fixed = obj( + Object.fromEntries(["name", "owner", "updated_at", "email_addresses"].map((one) => [one, list(ref("Value"))])), + ["name", "owner", "updated_at", "email_addresses"], + ) + const closed = { ...typed, Entry: { ...entry, properties: { ...entry.properties, values: fixed } } } + const { source, fake } = crmManifestFixture(SOURCE, { api, definitions: closed }) + const people = (await source.changes("person", null)) as CrmPage + expect(people.items.map(({ name }) => name)).toEqual(["name 1", "name 2"]) + expect(fake.refusals).toEqual([]) + }) + + for (const { name, run } of crmManifestContract(SOURCE, { api, definitions: typed })) it(name, run) +}) diff --git a/src/crm/testing/manifest-fake.ts b/src/crm/testing/manifest-fake.ts index 3458303a3..4cc2414f5 100644 --- a/src/crm/testing/manifest-fake.ts +++ b/src/crm/testing/manifest-fake.ts @@ -165,6 +165,33 @@ export const crmManifestFake = (input: CrmManifest, options: CrmManifestFakeOpti return ["string", "unknown"].includes(type) ? String(value) : undefined } + /** + * The schema with each union on the way to `keys` cut to the branch that has the rest of the path: a sample of the + * first branch of Attio's seventeen value types would carry another type's fields, and the strict fake refuses it. + */ + const narrowed = (node: SchemaNode, keys: readonly string[]): SchemaNode => { + if (keys.length === 0) return node + const found = resolve(node, definitions) + const [key, ...rest] = keys as [string, ...string[]] + const fits = (one: SchemaNode) => "node" in walk(one, keys.join("."), definitions) + switch (found.type) { + case "union": { + const branch = found.of.find(fits) + return branch ? narrowed(branch, keys) : node + } + case "allOf": + return { ...found, of: found.of.map((one) => (fits(one) ? narrowed(one, keys) : one)) } + case "array": + return /^\d+$/.test(key) ? { ...found, items: narrowed(found.items, rest) } : node + case "object": { + const inner = found.properties[key] ?? found.additionalProperties + return inner ? { ...found, properties: { ...found.properties, [key]: narrowed(inner, rest) } } : node + } + default: + return node + } + } + /** The object a dotted path ends in, with its last key and schema: what is missing is built from the schema's sample. */ const parentOf = (row: CrmRaw, path: string, node: SchemaNode | undefined) => { const keys = path.split(".") @@ -174,7 +201,7 @@ export const crmManifestFake = (input: CrmManifest, options: CrmManifestFakeOpti for (const [place, one] of keys.entries()) { const inner = schema === undefined ? undefined : (step(schema, one, definitions) ?? undefined) if (typeof here[one] !== "object" || here[one] === null) { - const made = sample(inner) + const made = sample(inner && narrowed(inner, [...keys.slice(place + 1), key])) here[one] = typeof made === "object" && made !== null ? made : /^\d+$/.test(keys[place + 1] ?? key) ? [] : {} } here = here[one] as Record @@ -221,7 +248,9 @@ export const crmManifestFake = (input: CrmManifest, options: CrmManifestFakeOpti n: number, given: Readonly>, ): CrmRaw => { - const made = sample(node) + // Sampled already cut to the branches the rules write into, so a list the first sample holds has the right type. + const paths = Object.values(rules).flatMap((rule) => pathOf(rule) ?? []) + const made = sample(node && paths.reduce((cut, path) => narrowed(cut, path.split(".")), node)) const row = isRaw(made) ? made : {} for (const [field, rule] of Object.entries(rules)) { const path = pathOf(rule) diff --git a/src/manifest/index.test.ts b/src/manifest/index.test.ts index c87237af8..b3c027887 100644 --- a/src/manifest/index.test.ts +++ b/src/manifest/index.test.ts @@ -76,7 +76,7 @@ describe("parseCliConfig", () => { expect(() => parseCliConfig({ ...config, category: "erp" })).toThrow("wirecat.config: category") const crm = { ...config, category: "crm" as const, source: { provider: "acme" } } expect(parseCliConfig(crm).source).toEqual({ provider: "acme" }) - const verify = { operation: "getV2Self", id: "workspace_id", name: "workspace_name" } + const verify = { operation: "getV2Self", id: "workspace_id", name: "workspace_name", active: "active" } expect(parseCliConfig({ ...config, auth: { preset: "token", verify } }).auth).toEqual({ preset: "token", verify }) expect(() => parseCliConfig({ ...config, auth: { preset: "token", verify: { id: "id" } } })).toThrow( "wirecat.config: auth.verify.operation: missing", diff --git a/src/manifest/index.ts b/src/manifest/index.ts index 41468765f..0d07504d4 100644 --- a/src/manifest/index.ts +++ b/src/manifest/index.ts @@ -4,6 +4,9 @@ import type { CrmManifest } from "../crm/manifest/manifest.js" import type { OAuthProvider } from "../oauth/provider.js" import type { TrackerManifest } from "../tracker/manifest/manifest.js" +/** A dotted path into an answer, as the source manifest reads it: `auth.verify`'s `id`, `name` and `active`. */ +export { at as valueAt } from "../crm/manifest/map.js" + /** * The operation that answers whose the token is, and where its answer holds the account's id (and a name to show): * `{ operation: "getV2Self", id: "workspace_id", name: "workspace_name" }`. `session start` checks a token with it. @@ -12,6 +15,12 @@ export interface CliVerify { operation: string id: string name?: string + /** + * Where the answer says the token still works, for a service that answers a dead token with success: Attio's + * `GET /v2/self` gives 200 `{ "active": false }` to an unknown, revoked or deleted token. Anything but `true` there is + * a refused sign-in. + */ + active?: string } export type CliAuth = @@ -128,7 +137,12 @@ const httpsUrl = v.pipe( const dotPath = v.pipe(v.string(), v.regex(/^[^.\s]+(\.[^.\s]+)*$/, "a dot path into the answer, such as data.id")) const verify = v.optional( - v.strictObject({ operation: v.pipe(v.string(), v.minLength(1)), id: dotPath, name: v.optional(dotPath) }), + v.strictObject({ + operation: v.pipe(v.string(), v.minLength(1)), + id: dotPath, + name: v.optional(dotPath), + active: v.optional(dotPath), + }), ) const schema = v.strictObject({