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
17 changes: 17 additions & 0 deletions changes/fix-crm-manifest-attio.md
Original file line number Diff line number Diff line change
@@ -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`.
4 changes: 4 additions & 0 deletions docs/dev/CRM.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions src/crm/manifest/manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down
29 changes: 18 additions & 11 deletions src/crm/manifest/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>(
(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<unknown>(
(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)
Expand Down
50 changes: 50 additions & 0 deletions src/crm/testing/manifest-fake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, SchemaNode>) =>
obj({ active_from: str, attribute_type: { type: "string", enum: [type] }, ...fields }, [
"active_from",
"attribute_type",
...Object.keys(fields),
])
const typed: Record<string, SchemaNode> = {
...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<CrmPerson>
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<string, { attribute_type: string }[]> }
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<SchemaNode, { type: "object" }>
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<CrmPerson>
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)
})
33 changes: 31 additions & 2 deletions src/crm/testing/manifest-fake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(".")
Expand All @@ -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<string, unknown>
Expand Down Expand Up @@ -221,7 +248,9 @@ export const crmManifestFake = (input: CrmManifest, options: CrmManifestFakeOpti
n: number,
given: Readonly<Record<string, Value | undefined>>,
): 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)
Expand Down
2 changes: 1 addition & 1 deletion src/manifest/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 15 additions & 1 deletion src/manifest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 =
Expand Down Expand Up @@ -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({
Expand Down
Loading