@sanity-labs/client-fake-for-test 0.22.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/errors.ts","../src/resources.ts","../src/token-grants.ts","../src/history.ts","../src/platform-api.ts","../src/access-control.ts","../src/mutation-guard.ts","../src/patch.ts","../src/perspective.ts","../src/store.ts","../src/actions.ts","../src/transaction.ts","../src/request-url.ts","../src/unsupported.ts","../src/client.ts"],"sourcesContent":["/**\n * Error helpers shared across the fake.\n *\n * Every error the fake throws is prefixed with {@link ERR_PREFIX} so it's\n * unmistakably attributable to *this fake* — not to `@sanity/client`,\n * groq-js, or the code under test — and carries enough context to fix\n * the problem without reading the fake's source. Short, bare messages\n * (\"not found\", \"invalid id\") are a poor developer experience in a test\n * double: prefer stating what happened, the offending value, and what to\n * do about it.\n */\nexport const ERR_PREFIX = \"[client-fake-for-test]\";\n\n/** Build a prefixed Error. Pass a fully-formed, dev-facing message. */\nexport function fakeError(message: string): Error {\n return new Error(`${ERR_PREFIX} ${message}`);\n}\n","/**\n * One capability table per resource type: how `@sanity/client` addresses\n * the resource, and what the platform serves or refuses there.\n *\n * Every value is taken from the staging capability probe (EDEX-2884): where\n * the platform says no, the entry holds the platform's own answer — status\n * and body — and the fake throws exactly that. Nothing here is policy of the\n * fake; adding a resource type means adding one row.\n */\n\nimport { ClientError, type HttpError } from \"@sanity/client\";\n\nimport type { CurrentUser, TestClientIdentity } from \"./access-control.ts\";\nimport { fakeError } from \"./errors.ts\";\n\n/** Resource types `@sanity/client`'s `resource` config can name (the ones staging probed). */\nexport type ResourceType = \"dataset\" | \"media-library\" | \"canvas\" | \"dashboard\";\n\n/**\n * A resource, named exactly as `@sanity/client`'s `resource` config names\n * it: `{ type: \"media-library\", id: \"ml…\" }`, or `{ type: \"dataset\", id:\n * \"<projectId>.<dataset>\" }`.\n */\nexport interface TestClientResource {\n type: ResourceType;\n id: string;\n}\n\n/**\n * The platform's refusal, as the `ClientError` `@sanity/client` throws for\n * the same response: the fake hands `ClientError` the response the client\n * would have received, and `ClientError` derives `statusCode`, `message`,\n * `responseBody` and `details` from it. So `instanceof ClientError` holds for\n * the `@sanity/client` the consumer has installed.\n */\nexport class PlatformHttpError extends ClientError {\n constructor(args: PlatformResponseArgs) {\n super(platformResponse(args));\n this.name = \"PlatformHttpError\";\n }\n}\n\n/** What the platform answered, and to which request. */\ninterface PlatformResponseArgs {\n method: string;\n /** The URL `@sanity/client` requested; see {@link PlatformRequest.url}. */\n url: string;\n statusCode: number;\n statusMessage: string;\n body: unknown;\n}\n\n/**\n * The response `@sanity/client` reads an HTTP error from. The platform sends\n * JSON bodies as `application/json` and Express's text bodies as\n * `text/html`; `ClientError` only stringifies `responseBody` for the former.\n */\nexport function platformResponse(args: PlatformResponseArgs): HttpError[\"response\"] {\n const { method, url, statusCode, statusMessage, body } = args;\n const contentType = typeof body === \"string\" ? \"text/html\" : \"application/json\";\n return {\n body,\n url,\n method,\n headers: { \"content-type\": `${contentType}; charset=utf-8` },\n statusCode,\n statusMessage,\n };\n}\n\n/**\n * The Content Lake's 403 for a transaction it refuses: the cause wrapped in a\n * `mutationError`, as gradient's `convertMutationError` (a\n * `TransactionFailure`) and `proto_errors.DoError` write it.\n */\nexport function transactionRefused(\n request: PlatformRequest,\n cause: { description: string; type: string } & Record<string, unknown>,\n): HttpError[\"response\"] {\n return platformResponse({\n ...request,\n statusCode: 403,\n statusMessage: \"Forbidden\",\n body: {\n error: {\n description: `transaction failed: ${cause.description}`,\n items: [{ error: cause, index: 0 }],\n type: \"mutationError\",\n },\n },\n });\n}\n\n/** A request the platform answers. */\nexport interface PlatformRequest {\n method: \"GET\" | \"POST\";\n /** The API path the platform routes on, without host, API version or query string. */\n path: string;\n /**\n * The full URL `@sanity/client` requests, host, `/v<apiVersion>` and query\n * string included, which its errors carry as `response.url` (see\n * `./request-url.ts`).\n */\n url: string;\n}\n\n/**\n * The request a write error names when its caller gives none: an error built\n * outside any client has no host to name, so its URL is the API path alone.\n */\nexport const DEFAULT_WRITE: PlatformRequest = {\n method: \"POST\",\n path: \"/data/mutate\",\n url: \"/data/mutate\",\n};\n\n/** The platform's answer where it says no. */\nexport type Refusal = (request: PlatformRequest) => PlatformHttpError;\n\n/** Either the platform serves the capability, or it refuses with a fixed answer. */\nexport type Capability = \"served\" | Refusal;\n\n/** Express's 404 for a route the resource does not mount: `Cannot GET /dashboards/<id>/acl`. */\nconst notMounted: Refusal = ({ method, path, url }) =>\n new PlatformHttpError({\n method,\n url,\n statusCode: 404,\n statusMessage: \"Not Found\",\n body: `Cannot ${method} ${path}`,\n });\n\n/** An Access API or project API error: `{ statusCode, error, message }`, the body shape staging returns. */\nfunction accessApiError(args: {\n request: PlatformRequest;\n statusCode: number;\n statusMessage: string;\n message: string;\n}): PlatformHttpError {\n const { request, statusCode, statusMessage, message } = args;\n return new PlatformHttpError({\n ...request,\n statusCode,\n statusMessage,\n body: { statusCode, error: statusMessage, message },\n });\n}\n\n/**\n * The platform's answer to a token it has no session for — revoked, expired\n * or never issued — on every API, data and platform routes alike. Probed on\n * staging and production (EDEX-3204).\n */\nexport const sessionNotFound: Refusal = (request) =>\n new PlatformHttpError({\n ...request,\n statusCode: 401,\n statusMessage: \"Unauthorized\",\n body: {\n error: \"Unauthorized\",\n errorCode: \"SIO-401-ANF\",\n message: \"Session not found\",\n statusCode: 401,\n },\n });\n\n/**\n * The gateway's answer to a project-scoped token (a robot token) on another\n * project's API host, whatever the route: kong doorman `SIO-401-AWH`.\n * Probed on staging (EDEX-3204).\n */\nexport const sessionHostMismatch: Refusal = (request) =>\n new PlatformHttpError({\n ...request,\n statusCode: 401,\n statusMessage: \"Unauthorized\",\n body: {\n statusCode: 401,\n errorCode: \"SIO-401-AWH\",\n error: \"Unauthorized\",\n message: \"Session does not match project host\",\n },\n });\n\n/** The project API's 401 for a caller outside the project (populus `requireProjectMembership`). */\nexport const projectMembershipRequired: Refusal = (request) =>\n accessApiError({\n request,\n statusCode: 401,\n statusMessage: \"Unauthorized\",\n message: \"User must be a member of this project to perform this operation\",\n });\n\n/** Media Library's 401 for a caller without `sanity.media-library.read` on the library. */\nconst mediaLibraryReadRequired: Refusal = (request) =>\n accessApiError({\n request,\n statusCode: 401,\n statusMessage: \"Unauthorized\",\n message:\n \"User must be able to read media libraries for this organization to perform this operation\",\n });\n\n/** The Access API's 403 when the caller lacks the permission; also the answer for unproven cells. */\nexport const noPermission: Refusal = (request) =>\n accessApiError({\n request,\n statusCode: 403,\n statusMessage: \"Forbidden\",\n message: \"You do not have permission to access this resource\",\n });\n\n/** The Access API's 400 for a resource type it does not know: `/access/bogustype/<id>/…`. */\nexport function invalidAccessType(request: PlatformRequest, type: string): PlatformHttpError {\n return accessApiError({\n request,\n statusCode: 400,\n statusMessage: \"Bad Request\",\n message: `Invalid resource type ${type}`,\n });\n}\n\n/** The Access API's 404 for an unknown id: `\"Canvas not found\"`, `\"User not found\"`. */\nexport function notFound(name: string): Refusal {\n return (request) =>\n accessApiError({\n request,\n statusCode: 404,\n statusMessage: \"Not Found\",\n message: `${name} not found`,\n });\n}\n\n/** Media Library's plan quota on releases: `releaseLimitExceededError`, limit 0. */\nconst releaseLimitExceeded: Refusal = ({ method, url }) =>\n new PlatformHttpError({\n method,\n url,\n statusCode: 403,\n statusMessage: \"Forbidden\",\n body: {\n error: {\n description: \"active release count exceeds the limit of 0\",\n type: \"releaseLimitExceededError\",\n },\n },\n });\n\n/** Throw the refusal, or run `serve` when the platform serves the capability. */\nexport function serveCapability<T>(\n capability: Capability,\n request: PlatformRequest,\n serve: () => T,\n): T {\n if (capability !== \"served\") throw capability(request);\n return serve();\n}\n\n/** The Access API's resource types (`/access/<type>/<id>/…`). */\nexport type AccessResourceType =\n | \"organization\"\n | \"project\"\n | \"media-library\"\n | \"canvas\"\n | \"dashboard\";\n\nexport interface ResourceCapabilities {\n /** How the fake's own error messages name the resource. */\n label: (id: string) => string;\n /** The data API base `@sanity/client`'s `resourceDataBase` builds for the resource. */\n dataBase: (id: string) => string;\n /** Match a request path on the data API base; `rest` is what follows it (`\"\"`, `/acl`, …). */\n matchBase: (path: string) => { id: string; rest: string } | undefined;\n /** Whether `GET <base>` answers the resource record (`{ organizationId, id, status }`). */\n lookupAtBase: boolean;\n /**\n * Whose id grant filters see as `identity()`: the project-scoped user\n * record for a dataset, the account-global one for every other resource.\n */\n identity: \"project\" | \"global\";\n /** `<base>/acl` → the caller's `Grant[]`. */\n acl: Capability;\n /**\n * Where `<base>/acl` is served: the answer for an id the registry does not\n * hold, and the Access API permission it needs. Absent where staging did not\n * probe it.\n */\n aclRefusals?: {\n unknown: Refusal;\n read?: { permission: string; refusal: Refusal };\n };\n /** `<base>/history/documents | transactions | events`. */\n history: Capability;\n /** `<base>/actions` — release, version and document actions. */\n actions: Capability;\n /** Action types refused where `/actions` is mounted. */\n refusedActions: Partial<Record<string, Refusal>>;\n /** The Access API addresses the resource as `/access/<type>/<id(resource id)>`. */\n access: { type: AccessResourceType; id: (id: string) => string };\n}\n\n/** An Access API resource: `/access/<type>/<id>`. */\nexport interface AccessResource {\n type: AccessResourceType;\n id: string;\n}\n\n/** What the Access API knows of the client family: its organization and the resources it holds. */\nexport interface AccessRegistry {\n organizationId: string | undefined;\n /** Every resource that exists: seeded or written to (a handle pointed at one does not create it). */\n resources: () => TestClientResource[];\n}\n\n/** One Access API resource type: what `/access/<type>/<id>/…` serves or refuses. */\nexport interface AccessCapabilities {\n /** `GET …/roles`. */\n roles: Capability;\n /** `GET …/users`. */\n users: Capability;\n /** `GET …/users/<sanityUserId>`. */\n user: Capability;\n /**\n * The permission served `users` and `user` need, checked against the\n * actor's `access` entry for the resource (what `user-permissions/me`\n * reports). When absent, no permission check runs.\n */\n usersPermission?: string;\n /** The permission served `roles` needs, checked like {@link usersPermission}. */\n rolesPermission?: string;\n /**\n * The answer for an id the family does not hold, on every endpoint but\n * `user-permissions/me/check` (which answers `false` even then). Absent\n * where staging did not probe it.\n */\n unknown?: { holds: (registry: AccessRegistry, id: string) => boolean; refusal: Refusal };\n /** The identity's role records on exactly this resource, or `undefined` when it is not a member. */\n memberRoles: (args: {\n identity: TestClientIdentity;\n id: string;\n }) => NonNullable<CurrentUser[\"roles\"]> | undefined;\n /**\n * Resources whose memberships a user row here lists besides its own: the\n * organization's users answer every membership in the organization.\n */\n owns?: (registry: AccessRegistry) => AccessResource[];\n}\n\nconst ACCESS_KEY_SEPARATOR = \":\";\n\n/** Key of an identity's `access` entry for a resource: `\"<access type>:<access id>\"`. */\nexport function accessKey(type: AccessResourceType, id: string): string {\n return `${type}${ACCESS_KEY_SEPARATOR}${id}`;\n}\n\n/** Match `/<segment>/<id>` and whatever follows. */\nfunction segmentBase(segment: string): ResourceCapabilities[\"matchBase\"] {\n const pattern = new RegExp(`^/${segment}/([^/]+)(/.*)?$`);\n return (path) => {\n const match = path.match(pattern);\n if (match === null) return undefined;\n return { id: decodeURIComponent(match[1] as string), rest: match[2] ?? \"\" };\n };\n}\n\nconst DATASET_BASE = /^\\/projects\\/([^/]+)\\/datasets\\/([^/]+)(\\/.*)?$/;\n\n/**\n * Split a dataset resource id, `\"<projectId>.<dataset>\"`, at its first dot.\n * Lenient on purpose: the fake builds these ids from any `(projectId,\n * dataset)` pair; caller-supplied ids are checked by {@link validateResource}.\n */\nexport function datasetParts(id: string): { projectId: string; dataset: string } {\n const dot = id.indexOf(\".\");\n return { projectId: id.slice(0, dot), dataset: id.slice(dot + 1) };\n}\n\n/**\n * Whether the identity is a member of the project: a project-scoped token\n * belongs to its own project only, any other identity to every project.\n */\nexport function isProjectMember(tokenProjectId: string | undefined, projectId: string): boolean {\n return tokenProjectId === undefined || tokenProjectId === projectId;\n}\n\n/** A non-project resource's roles live in the identity's `access` entry for it. */\nfunction accessEntryRoles(type: AccessResourceType): AccessCapabilities[\"memberRoles\"] {\n return ({ identity, id }) => identity.access?.[accessKey(type, id)]?.roles;\n}\n\nexport const RESOURCE_CAPABILITIES: Record<ResourceType, ResourceCapabilities> = {\n dataset: {\n label: (id) => {\n const { projectId, dataset } = datasetParts(id);\n return `dataset \"${projectId}:${dataset}\"`;\n },\n dataBase: (id) => {\n const { projectId, dataset } = datasetParts(id);\n return `/projects/${projectId}/datasets/${dataset}`;\n },\n matchBase: (path) => {\n const match = path.match(DATASET_BASE);\n if (match === null) return undefined;\n return { id: `${match[1]}.${match[2]}`, rest: match[3] ?? \"\" };\n },\n // A dataset's organization comes from its project: `GET /projects/<projectId>`.\n lookupAtBase: false,\n identity: \"project\",\n acl: \"served\",\n history: \"served\",\n actions: \"served\",\n refusedActions: {},\n access: { type: \"project\", id: (id) => datasetParts(id).projectId },\n },\n \"media-library\": {\n label: (id) => `media library \"${id}\"`,\n dataBase: (id) => `/media-libraries/${id}`,\n matchBase: segmentBase(\"media-libraries\"),\n lookupAtBase: true,\n identity: \"global\",\n acl: \"served\",\n aclRefusals: {\n unknown: notFound(\"Media library\"),\n read: { permission: \"sanity.media-library.read\", refusal: mediaLibraryReadRequired },\n },\n history: \"served\",\n actions: \"served\",\n refusedActions: { \"sanity.action.release.create\": releaseLimitExceeded },\n access: { type: \"media-library\", id: (id) => id },\n },\n canvas: {\n label: (id) => `canvas \"${id}\"`,\n dataBase: (id) => `/canvases/${id}`,\n matchBase: segmentBase(\"canvases\"),\n lookupAtBase: true,\n identity: \"global\",\n acl: \"served\",\n aclRefusals: { unknown: notFound(\"Resource\") },\n history: \"served\",\n actions: notMounted,\n refusedActions: {},\n access: { type: \"canvas\", id: (id) => id },\n },\n dashboard: {\n label: (id) => `dashboard \"${id}\"`,\n dataBase: (id) => `/dashboards/${id}`,\n matchBase: segmentBase(\"dashboards\"),\n lookupAtBase: true,\n identity: \"global\",\n acl: notMounted,\n history: notMounted,\n actions: notMounted,\n refusedActions: {},\n access: { type: \"dashboard\", id: (id) => id },\n },\n};\n\n/** The capability row for `type`, or a fake error naming the supported types. */\nexport function capabilitiesOf(type: string): ResourceCapabilities {\n // Own keys only: `toString` is no more a resource type than `bogustype`.\n const row = Object.hasOwn(RESOURCE_CAPABILITIES, type)\n ? RESOURCE_CAPABILITIES[type as ResourceType]\n : undefined;\n if (row === undefined) {\n throw fakeError(\n `Unsupported resource type ${JSON.stringify(type)}. The fake serves ` +\n `${Object.keys(RESOURCE_CAPABILITIES)\n .map((t) => `\"${t}\"`)\n .join(\", \")}.`,\n );\n }\n return row;\n}\n\n/** Validate a `resource` config the way `@sanity/client` does, returning its capability row. */\nexport function validateResource(resource: TestClientResource): ResourceCapabilities {\n const row = capabilitiesOf(resource.type);\n if (resource.type === \"dataset\" && resource.id.split(\".\").length !== 2) {\n throw fakeError(\n `Dataset resource ID must be in the format \"project.dataset\" (got ${JSON.stringify(resource.id)}), ` +\n \"the same rule @sanity/client applies to `resource: { type: 'dataset', id }`.\",\n );\n }\n return row;\n}\n\n/** Find the resource a data API path addresses, by trying every row's base. */\nexport function matchResourcePath(\n path: string,\n): { resource: TestClientResource; rest: string } | undefined {\n for (const [type, row] of Object.entries(RESOURCE_CAPABILITIES)) {\n const match = row.matchBase(path);\n if (match !== undefined) {\n return { resource: { type: type as ResourceType, id: match.id }, rest: match.rest };\n }\n }\n return undefined;\n}\n\n/** The Access API resource a registry resource is: a dataset is its project. */\nfunction accessResourceOf(resource: TestClientResource): AccessResource {\n const { access } = capabilitiesOf(resource.type);\n return { type: access.type, id: access.id(resource.id) };\n}\n\n/** Whether the registry holds an Access API resource of `type` with `id`. */\nexport function holdsResource(\n type: AccessResourceType,\n): (registry: AccessRegistry, id: string) => boolean {\n return (registry, id) =>\n registry.resources().some((resource) => {\n const access = accessResourceOf(resource);\n return access.type === type && access.id === id;\n });\n}\n\nexport const ACCESS_CAPABILITIES: Record<AccessResourceType, AccessCapabilities> = {\n organization: {\n // Proven on staging, but outside what the fake serves: refused like a missing permission.\n roles: noPermission,\n users: \"served\",\n user: \"served\",\n // Granted by the built-in organization `member` role.\n usersPermission: \"sanity.organization.members.read\",\n unknown: {\n holds: (registry, id) => id === registry.organizationId,\n refusal: notFound(\"Organization\"),\n },\n memberRoles: accessEntryRoles(\"organization\"),\n // Every resource of the family belongs to its organization (see `organizationId`).\n owns: (registry) => {\n const byKey = new Map<string, AccessResource>();\n for (const resource of registry.resources()) {\n const access = accessResourceOf(resource);\n byKey.set(accessKey(access.type, access.id), access);\n }\n return [...byKey.values()];\n },\n },\n project: {\n roles: \"served\",\n // Also gates `GET /projects/<id>/roles`; the stock user roles all hold it on staging.\n rolesPermission: \"sanity.project.roles.read\",\n users: \"served\",\n // Single-user reads are served only where the fake checks members read (the\n // organization); elsewhere they refuse rather than answer unchecked.\n user: noPermission,\n // A member holds its project roles; see `isProjectMember`.\n memberRoles: ({ identity, id }) =>\n isProjectMember(identity.projectId, id)\n ? ((identity.usersByProject?.[id] ?? identity.user).roles ?? [])\n : undefined,\n },\n \"media-library\": {\n // Staging refuses it only because no probe token held roles read.\n roles: \"served\",\n rolesPermission: \"sanity.media-library.roles.read\",\n users: \"served\",\n // Unproven on staging (only the listing was probed); refused as on `project`.\n user: noPermission,\n unknown: { holds: holdsResource(\"media-library\"), refusal: notFound(\"Media library\") },\n memberRoles: accessEntryRoles(\"media-library\"),\n },\n canvas: {\n // Unproven on staging (no canvas roles/members permission exists to grant).\n roles: noPermission,\n users: noPermission,\n user: noPermission,\n unknown: { holds: holdsResource(\"canvas\"), refusal: notFound(\"Canvas\") },\n memberRoles: accessEntryRoles(\"canvas\"),\n },\n dashboard: {\n // Unproven on staging, as for canvas.\n roles: noPermission,\n users: noPermission,\n user: noPermission,\n unknown: { holds: holdsResource(\"dashboard\"), refusal: notFound(\"Dashboard\") },\n memberRoles: accessEntryRoles(\"dashboard\"),\n },\n};\n\n/** The Access API row for `type`, if the Access API knows the type. */\nexport function accessCapabilitiesOf(type: string): AccessCapabilities | undefined {\n // Own keys only: `toString` or `constructor` is as unknown to the platform as `bogustype`.\n return Object.hasOwn(ACCESS_CAPABILITIES, type)\n ? ACCESS_CAPABILITIES[type as AccessResourceType]\n : undefined;\n}\n\n/**\n * `@sanity/client`'s `getDataUrl(operation, path)`: a `resource`-configured\n * handle addresses `<resource base>/<operation>/<path>`, a plain dataset\n * handle `/data/<operation>/<dataset>/<path>`.\n */\nexport function dataUrl(args: {\n resource: TestClientResource | undefined;\n dataset: string;\n operation: string;\n path?: string | undefined;\n}): string {\n const { resource, dataset, operation, path } = args;\n if (resource !== undefined) {\n const uri = path !== undefined ? `${operation}/${path}` : operation;\n return `${capabilitiesOf(resource.type).dataBase(resource.id)}/${uri}`.replace(\n /\\/($|\\?)/,\n \"$1\",\n );\n }\n const base = `/${operation}/${dataset}`;\n const uri = path !== undefined ? `${base}/${path}` : base;\n return `/data${uri}`.replace(/\\/($|\\?)/, \"$1\");\n}\n","/**\n * The grants the Content Lake refuses in a token.\n *\n * The Content Lake parses a token's grants on every request it serves\n * (gradient `pkg/server/httpapi/middlewares/sessions/session.go`\n * `ParseClaimFromToken`) and answers the whole request with 400 when one\n * does not parse. A token grant is a path and its permissions (gradient\n * `pkg/security/tokens.go` `deserializeGrant`): the path must parse\n * (`pkg/documents/paths/paths.go` `Parse`) and every permission must be one\n * the Content Lake knows (`pkg/security/permissions.go`\n * `PermissionFromString`). Filter grants come from the project's ACL, not\n * from the token, and are not parsed there.\n */\n\nimport type { Grant } from \"./access-control.ts\";\nimport { PlatformHttpError, type PlatformRequest } from \"./resources.ts\";\n\n/** `paths.MaxPathLength`, counted in UTF-8 bytes. */\nconst MAX_PATH_BYTES = 128;\n\n/** `security.AllPermissions`. */\nconst PERMISSIONS = new Set([\"read\", \"update\", \"create\", \"manage\", \"history\", \"editHistory\"]);\n\n/** A wildcard element and gradient's message when it is not the last element. */\nconst MISPLACED_WILDCARD = new Map([\n [\"*\", \"wildcard must be last component\"],\n [\"**\", \"descendant wildcard must be last component\"],\n]);\n\n/** `validElementLine`: ASCII letters, digits, `_` and `-`, not starting with `-`. */\nconst ELEMENT_LINE = /^[A-Za-z0-9_][A-Za-z0-9_-]*$/;\n\n/**\n * `validElement`: gradient accepts an element when any one of its\n * newline-separated lines is valid, a quirk it keeps for existing ids.\n */\nfunction validElement(element: string): boolean {\n return element.split(\"\\n\").some((line) => ELEMENT_LINE.test(line));\n}\n\n/** Go's `\\a` … `\\v` escapes, which `strconv.Quote` writes for these control characters. */\nconst GO_ESCAPES: Record<string, string> = {\n \"\\u0007\": \"\\\\a\",\n \"\\b\": \"\\\\b\",\n \"\\f\": \"\\\\f\",\n \"\\n\": \"\\\\n\",\n \"\\r\": \"\\\\r\",\n \"\\t\": \"\\\\t\",\n \"\\v\": \"\\\\v\",\n '\"': '\\\\\"',\n \"\\\\\": \"\\\\\\\\\",\n};\n\n/** Letters, marks, numbers, punctuation, symbols and the ASCII space: what Go's `IsPrint` keeps. */\nconst GO_PRINTABLE = /^[\\p{L}\\p{M}\\p{N}\\p{P}\\p{S} ]$/u;\n\n/**\n * Go's `%q` (and `%#v`) of a string, `strconv.Quote`: printable characters\n * as they are, the named control escapes, then `\\xNN`, `\\uNNNN` or\n * `\\UNNNNNNNN` for everything else.\n */\nfunction quoted(value: string): string {\n let out = '\"';\n for (const char of value) {\n const escape = GO_ESCAPES[char];\n if (escape !== undefined) {\n out += escape;\n continue;\n }\n if (GO_PRINTABLE.test(char)) {\n out += char;\n continue;\n }\n const code = char.codePointAt(0) as number;\n out +=\n code < 0x80\n ? `\\\\x${code.toString(16).padStart(2, \"0\")}`\n : code <= 0xffff\n ? `\\\\u${code.toString(16).padStart(4, \"0\")}`\n : `\\\\U${code.toString(16).padStart(8, \"0\")}`;\n }\n return `${out}\"`;\n}\n\n/**\n * Gradient's message for the first rule `path` breaks, checked in\n * `Parse`'s order, or `undefined` when the path parses.\n */\nexport function pathError(path: string): string | undefined {\n if (path === \"\") return \"path must contain at least one element\";\n if (path.startsWith(\".\")) return 'path cannot begin with delimiter \".\"';\n const elements = path.split(\".\");\n if (elements.includes(\"\")) return \"path contains empty element\";\n if (new TextEncoder().encode(path).length > MAX_PATH_BYTES) {\n return `path exceeds maximum length of ${MAX_PATH_BYTES}`;\n }\n const last = elements.length - 1;\n for (const [index, element] of elements.entries()) {\n const misplaced = MISPLACED_WILDCARD.get(element);\n if (misplaced !== undefined) {\n if (index < last) return misplaced;\n } else if (!validElement(element)) {\n return `invalid element ${quoted(element)}`;\n }\n }\n return undefined;\n}\n\n/**\n * Whether the path glob `path` includes the document id `id`, as gradient's\n * `Path.Includes` decides for `_id in path(...)`: elements compare\n * literally, `*` matches exactly one element and a trailing `**` one or\n * more. groq-js's `path()` is not used: it matches `.` as any character.\n */\nexport function pathIncludes(path: string, id: string): boolean {\n const pattern = path.split(\".\");\n const elements = id.split(\".\");\n for (const [index, element] of pattern.entries()) {\n if (index >= elements.length) return false;\n if (element === \"**\") return true;\n // `*` covers one element, but not a `**` one.\n if (element === \"*\" ? elements[index] === \"**\" : element !== elements[index]) return false;\n }\n return pattern.length === elements.length;\n}\n\n/** Gradient's message for the first grant in `grants` it cannot parse, or `undefined`. */\nfunction grantsError(grants: readonly Grant[]): string | undefined {\n for (const grant of grants) {\n if (!(\"path\" in grant)) continue;\n const error = pathError(grant.path);\n if (error !== undefined) return error;\n const invalid = grant.permissions.find((permission) => !PERMISSIONS.has(permission));\n if (invalid !== undefined) return `invalid permission ${quoted(invalid)}`;\n }\n return undefined;\n}\n\n/**\n * The Content Lake's answer to `request` when its token carries a grant it\n * cannot parse: 400 with an `httpBadRequest` error describing the first\n * such grant, as `httputils.WriteError` writes `proto.HTTPBadRequest`.\n * `undefined` when every grant parses.\n */\nexport function refusedToken(\n request: PlatformRequest,\n grants: readonly Grant[],\n): PlatformHttpError | undefined {\n const description = grantsError(grants);\n if (description === undefined) return undefined;\n return new PlatformHttpError({\n ...request,\n statusCode: 400,\n statusMessage: \"Bad Request\",\n body: { error: { description, type: \"httpBadRequest\" } },\n });\n}\n","/**\n * Document history for one store — the fake's mirror of the History API\n * (`<data base>/history/documents | transactions | events`).\n *\n * Every committed write is recorded as one transaction holding the net\n * change of each document it touched. Revisions are kept by `_rev`, so a\n * document can be read at any revision it has held since the client was\n * created, plus the revision it holds now (seeded documents included).\n */\n\nimport type { SanityDocument } from \"@sanity/types\";\n\n/** One document's net change in a committed transaction. */\ninterface HistoryEffect {\n documentId: string;\n /** The document after the transaction; `undefined` when it was deleted. */\n after: SanityDocument | undefined;\n /** Whether the document existed before the transaction. */\n existed: boolean;\n}\n\ninterface HistoryTransaction {\n id: string;\n timestamp: string;\n author: string | undefined;\n effects: HistoryEffect[];\n}\n\n/** A transaction as `GET <data base>/history/transactions/<ids>` serves it, one per NDJSON line. */\nexport interface HistoryTransactionRecord {\n id: string;\n timestamp: string;\n author?: string;\n mutations: (\n | { create: SanityDocument }\n | { createOrReplace: SanityDocument }\n | { delete: { id: string } }\n )[];\n documentIDs: string[];\n}\n\n/** An event as `GET <data base>/history/events/documents/<ids>` serves it. */\nexport interface HistoryEvent {\n type: \"createLiveDocument\" | \"updateLiveDocument\";\n id: string;\n documentId: string;\n revisionId: string;\n timestamp: string;\n author?: string;\n}\n\nexport interface DocumentHistory {\n /**\n * Record a committed write: compare the images from before the write with\n * the store after it, and keep one transaction for the documents that\n * changed. A write that changed nothing records nothing.\n */\n commit: (args: {\n transactionId: string;\n author: string | undefined;\n before: ReadonlyMap<string, SanityDocument>;\n after: readonly SanityDocument[];\n }) => void;\n /** `?revision=<rev>`: the listed documents as they were at that revision. */\n documentsAt: (args: {\n ids: readonly string[];\n revision: string;\n current: readonly SanityDocument[];\n }) => SanityDocument[];\n /** Every document id the history has recorded a change for. */\n documentIds: () => string[];\n /** The last image `id` held before it was deleted, or `undefined` if it never changed. */\n lastImage: (id: string) => SanityDocument | undefined;\n /**\n * Transactions touching any of `ids`, oldest first. Each record keeps only\n * the effects on documents `visible` lets through, so a transaction shared\n * with a document the reader cannot see does not reveal it.\n */\n transactions: (\n ids: readonly string[],\n visible: (documentId: string) => boolean,\n ) => HistoryTransactionRecord[];\n /** Live-document events for each of `ids`, newest first. */\n events: (ids: readonly string[]) => Record<string, HistoryEvent[]>;\n}\n\nexport function createDocumentHistory(): DocumentHistory {\n const transactions: HistoryTransaction[] = [];\n /** Every image a document has held, keyed `<id> <rev>`. */\n const revisions = new Map<string, SanityDocument>();\n /** Every image is a copy, so neither the store nor a caller's edits rewrite history. */\n const keep = (doc: SanityDocument) =>\n revisions.set(`${doc._id} ${doc._rev}`, structuredClone(doc));\n /** The last image each deleted document held. */\n const deleted = new Map<string, SanityDocument>();\n\n return {\n commit: ({ transactionId, author, before, after }) => {\n const afterById = new Map(after.map((doc) => [doc._id, doc]));\n const ids = new Set([...before.keys(), ...afterById.keys()]);\n const effects: HistoryEffect[] = [];\n for (const id of ids) {\n const previous = before.get(id);\n const next = afterById.get(id);\n if (previous === next) continue;\n if (previous !== undefined) keep(previous);\n if (next !== undefined) keep(next);\n if (next === undefined && previous !== undefined)\n deleted.set(id, structuredClone(previous));\n else deleted.delete(id);\n effects.push({\n documentId: id,\n after: next === undefined ? undefined : structuredClone(next),\n existed: previous !== undefined,\n });\n }\n if (effects.length === 0) return;\n transactions.push({\n id: transactionId,\n timestamp: new Date().toISOString(),\n author,\n effects,\n });\n },\n\n documentsAt: ({ ids, revision, current }) =>\n ids.flatMap((id) => {\n const image =\n revisions.get(`${id} ${revision}`) ??\n current.find((doc) => doc._id === id && doc._rev === revision);\n return image === undefined ? [] : [structuredClone(image)];\n }),\n\n lastImage: (id) => deleted.get(id),\n\n documentIds: () => [\n ...new Set(transactions.flatMap((txn) => txn.effects.map((effect) => effect.documentId))),\n ],\n\n transactions: (ids, visible) =>\n transactions\n .filter((txn) => txn.effects.some((effect) => ids.includes(effect.documentId)))\n .map((txn) => ({ ...txn, effects: txn.effects.filter((e) => visible(e.documentId)) }))\n .map((txn) => ({\n id: txn.id,\n timestamp: txn.timestamp,\n ...(txn.author !== undefined ? { author: txn.author } : {}),\n mutations: txn.effects.map((effect) => {\n if (effect.after === undefined) return { delete: { id: effect.documentId } };\n const after = structuredClone(effect.after);\n return effect.existed ? { createOrReplace: after } : { create: after };\n }),\n documentIDs: txn.effects.map((effect) => effect.documentId),\n })),\n\n events: (ids) =>\n Object.fromEntries(\n ids.map((id) => [\n id,\n transactions\n .flatMap((txn) =>\n txn.effects\n .filter((effect) => effect.documentId === id && effect.after !== undefined)\n .map(\n (effect): HistoryEvent => ({\n type: effect.existed ? \"updateLiveDocument\" : \"createLiveDocument\",\n id: txn.id,\n documentId: id,\n revisionId: (effect.after as SanityDocument)._rev,\n timestamp: txn.timestamp,\n ...(txn.author !== undefined ? { author: txn.author } : {}),\n }),\n ),\n )\n .toReversed(),\n ]),\n ),\n };\n}\n\n/** Split `documents/<ids>` style id lists: comma-separated, URI-encoded. */\nexport function historyIds(list: string): string[] {\n return list.split(\",\").map((id) => decodeURIComponent(id));\n}\n","/**\n * The platform endpoints `client.request()` serves beyond identity: the\n * Access API (`/access/<type>/<id>/…`, routed through `ACCESS_CAPABILITIES`),\n * the project role catalog (`/projects/<id>/roles`, gated by the same table's\n * project row), the project's features (`/projects/<id>/features`), the\n * caller's organization attributes, resource lookup (`GET <data\n * base>`), ACL (`<data base>/acl`) and the History API (`<data\n * base>/history/…`, or `/data/history/<dataset>/…` on a plain dataset\n * handle). Routing goes through the capability table: a capability the\n * resource refuses throws the platform's own answer.\n */\n\nimport type { SanityDocument } from \"@sanity/types\";\n\nimport type {\n CurrentUser,\n Grant,\n ResourceAccess,\n TestClientIdentity,\n UserAttribute,\n UserAttributeValue,\n} from \"./access-control.ts\";\nimport { fakeError } from \"./errors.ts\";\nimport { historyIds, type DocumentHistory } from \"./history.ts\";\nimport {\n accessCapabilitiesOf,\n accessKey,\n ACCESS_CAPABILITIES,\n capabilitiesOf,\n holdsResource,\n invalidAccessType,\n isProjectMember,\n matchResourcePath,\n noPermission,\n notFound,\n projectMembershipRequired,\n serveCapability,\n type AccessRegistry,\n type AccessResource,\n type AccessResourceType,\n type Capability,\n type PlatformRequest,\n type ResourceCapabilities,\n type TestClientResource,\n} from \"./resources.ts\";\n\n/** What the handle answers for its own resource: the actor's grants, and the history it may read. */\nexport interface OwnResource {\n grants: Grant[];\n documents: () => readonly SanityDocument[];\n history: DocumentHistory;\n /** The actor's read gate; history serves only documents it lets through. */\n canRead: (document: SanityDocument) => Promise<boolean>;\n /** Throws the Content Lake's refusal of the actor's token; the History API is the Content Lake's. */\n admitToLake: (request: PlatformRequest) => void;\n}\n\n/** Everything {@link servePlatformRequest} needs from the owning handle. */\nexport interface PlatformApiContext {\n /** The handle's projectId, for `/data/history/<dataset>` paths. */\n projectId: string;\n /** The organization every resource of the family belongs to, if configured. */\n organizationId: string | undefined;\n /** The features each project has, keyed by projectId. */\n projectFeatures: Record<string, string[]>;\n /**\n * The handle's own resource, or `undefined` for any other: a token's\n * grants and history answer for the resource the handle addresses.\n */\n own: (resource: TestClientResource) => OwnResource | undefined;\n /** The acting identity's account-global user record (its `sanityUserId`). */\n actorUser: () => CurrentUser;\n /** The acting identity's Access API entries, keyed `\"<access type>:<id>\"`. */\n actorAccess: () => Record<string, ResourceAccess> | undefined;\n /** The project the acting identity's token belongs to, if it is project-scoped. */\n actorProjectId: () => string | undefined;\n /** The handle's members (registered identities, plus its inline `accessControl` actor), for member and role listings. */\n identities: () => TestClientIdentity[];\n /** Every resource that exists: seeded or written to (a handle pointed at one does not create it). */\n resources: () => TestClientResource[];\n}\n\n/** `{ value }` when the platform answers the path, `undefined` when the fake has no handler for it. */\ntype Served = { value: unknown } | undefined;\n\nconst ACCESS_PATH = /^\\/access\\/([^/]+)\\/([^/]+)(\\/.*)?$/;\nconst ACCESS_USER_PATH = /^\\/users\\/([^/]+)$/;\nconst ACCESS_ROLE_PATH = /^\\/roles\\/([^/]+)$/;\nconst PROJECT_ROLES_PATH = /^\\/projects\\/([^/]+)\\/roles$/;\nconst PROJECT_FEATURES_PATH = /^\\/projects\\/([^/]+)\\/features$/;\nconst MY_ATTRIBUTES_PATH = /^\\/organizations\\/([^/]+)\\/users\\/me\\/attributes$/;\nconst DATA_HISTORY_PATH = /^\\/data\\/history\\/([^/]+)(\\/.*)$/;\n\n/** An Access API listing page; the fake serves everything on one page. */\nfunction page<T>(data: T[]): { data: T[]; nextCursor: null } {\n return { data, nextCursor: null };\n}\n\n/** A users listing page, which also counts the users. */\nfunction usersPage<T>(data: T[]): { data: T[]; nextCursor: null; totalCount: number } {\n return { ...page(data), totalCount: data.length };\n}\n\n/**\n * The platform's profile fields the fake does not model: every user signs in\n * with `sanity` and was created at the epoch. `email` is required too, and is\n * empty where the identity seeds none.\n */\nconst UNMODELLED_PROFILE = { provider: \"sanity\", createdAt: new Date(0).toISOString() } as const;\n\n/** Serve `request` if its path is a platform endpoint the table knows; `uri` is the URI as the caller passed it. */\nexport async function servePlatformRequest(\n ctx: PlatformApiContext,\n uri: string,\n request: PlatformRequest,\n): Promise<Served> {\n const { path } = request;\n const queryStart = uri.indexOf(\"?\");\n const query = new URLSearchParams(queryStart === -1 ? \"\" : uri.slice(queryStart + 1));\n\n const myAttributes = path.match(MY_ATTRIBUTES_PATH);\n if (myAttributes !== null) {\n const organizationId = decodeURIComponent(myAttributes[1] as string);\n return serveMyAttributes({ ctx, request, organizationId, query });\n }\n\n const projectRoles = path.match(PROJECT_ROLES_PATH);\n if (projectRoles !== null) {\n const projectId = decodeURIComponent(projectRoles[1] as string);\n return serveProjectRoles({ ctx, request, projectId });\n }\n\n const projectFeatures = path.match(PROJECT_FEATURES_PATH);\n if (projectFeatures !== null) {\n const projectId = decodeURIComponent(projectFeatures[1] as string);\n if (!isProjectMember(ctx.actorProjectId(), projectId)) {\n throw projectMembershipRequired(request);\n }\n return { value: featuresOf(ctx, projectId) };\n }\n\n const access = path.match(ACCESS_PATH);\n if (access !== null) {\n const [, type, id, rest] = access as [string, string, string, string | undefined];\n return serveAccess({ ctx, request, type, id: decodeURIComponent(id), rest: rest ?? \"\", query });\n }\n\n const dataHistory = path.match(DATA_HISTORY_PATH);\n if (dataHistory !== null) {\n const [, dataset, rest] = dataHistory as [string, string, string];\n const resource: TestClientResource = { type: \"dataset\", id: `${ctx.projectId}.${dataset}` };\n return serveHistory({ ctx, request, resource, rest, query });\n }\n\n const matched = matchResourcePath(path);\n if (matched === undefined) return undefined;\n const { resource, rest } = matched;\n const row = capabilitiesOf(resource.type);\n if (rest === \"\") return row.lookupAtBase ? serveLookup(ctx, resource) : undefined;\n if (rest === \"/acl\") {\n return serveCapability(row.acl, request, () => {\n refuseAcl({ ctx, request, resource, row });\n const own = ctx.own(resource);\n return own === undefined ? undefined : { value: own.grants };\n });\n }\n if (rest.startsWith(\"/history/\")) {\n return serveHistory({ ctx, request, resource, rest: rest.slice(\"/history\".length), query });\n }\n return undefined;\n}\n\n/**\n * Throw the platform's refusal of `GET <path>` when it is a resource's ACL\n * the actor may not read: a route the resource does not mount, an unknown id,\n * or a missing read permission. An explicit `aclPath` alias runs it too.\n */\nexport function refuseAclRequest(ctx: PlatformApiContext, request: PlatformRequest): void {\n const matched = matchResourcePath(request.path);\n if (matched?.rest !== \"/acl\") return;\n const row = capabilitiesOf(matched.resource.type);\n serveCapability(row.acl, request, () =>\n refuseAcl({ ctx, request, resource: matched.resource, row }),\n );\n}\n\n/**\n * Throw the platform's refusal of `<base>/acl` on a resource it serves: an id\n * the registry does not hold, then an actor without the read permission.\n */\nfunction refuseAcl(args: {\n ctx: PlatformApiContext;\n request: PlatformRequest;\n resource: TestClientResource;\n row: ResourceCapabilities;\n}): void {\n const { ctx, request, resource, row } = args;\n if (row.aclRefusals === undefined) return;\n const type = row.access.type;\n const id = row.access.id(resource.id);\n const registry: AccessRegistry = { organizationId: ctx.organizationId, resources: ctx.resources };\n if (!holdsResource(type)(registry, id)) throw row.aclRefusals.unknown(request);\n const { read } = row.aclRefusals;\n if (read === undefined) return;\n const permissions = ctx.actorAccess()?.[accessKey(type, id)]?.permissions ?? [];\n if (!permissions.includes(read.permission)) throw read.refusal(request);\n}\n\n/**\n * `GET /projects/<id>/features`: the project's feature names, a bare array,\n * sorted and once each as the project API answers them. Only a project\n * member may read them (see `isProjectMember`).\n */\nfunction featuresOf(ctx: PlatformApiContext, projectId: string): string[] {\n const features = Object.hasOwn(ctx.projectFeatures, projectId)\n ? (ctx.projectFeatures[projectId] as string[])\n : [];\n return [...new Set(features)].toSorted();\n}\n\n/** `GET /media-libraries/<id>`, `/canvases/<id>`, `/dashboards/<id>`: the resource record. */\nfunction serveLookup(ctx: PlatformApiContext, resource: TestClientResource): Served {\n if (ctx.organizationId === undefined) {\n throw fakeError(\n `request(): GET ${capabilitiesOf(resource.type).dataBase(resource.id)} answers the ` +\n \"resource's organization, but no organization is configured. Pass \" +\n \"createTestClient({ organizationId }).\",\n );\n }\n return { value: { organizationId: ctx.organizationId, id: resource.id, status: \"active\" } };\n}\n\nfunction serveHistory(args: {\n ctx: PlatformApiContext;\n request: PlatformRequest;\n resource: TestClientResource;\n rest: string;\n query: URLSearchParams;\n}): Promise<Served> | undefined {\n const { ctx, request, resource, rest, query } = args;\n return serveCapability(capabilitiesOf(resource.type).history, request, () => {\n const own = ctx.own(resource);\n if (own === undefined) return undefined;\n own.admitToLake(request);\n return readHistory({ own, request, rest, query });\n });\n}\n\n/** Answer a History API path from the handle's own history, hiding what the actor cannot read. */\nasync function readHistory(args: {\n own: OwnResource;\n request: PlatformRequest;\n rest: string;\n query: URLSearchParams;\n}): Promise<Served> {\n const { own, request, rest, query } = args;\n // The read grant filters the document as it is now or, once deleted, as it last was.\n const current = new Map(own.documents().map((doc) => [doc._id, doc]));\n const hidden = new Set<string>();\n for (const id of new Set([...current.keys(), ...own.history.documentIds()])) {\n const image = current.get(id) ?? own.history.lastImage(id);\n if (image !== undefined && !(await own.canRead(image))) hidden.add(id);\n }\n const visible = (id: string) => !hidden.has(id);\n const readable = (list: string): string[] => historyIds(list).filter(visible);\n\n const documents = rest.match(/^\\/documents\\/([^/]+)$/);\n if (documents !== null) {\n const revision = query.get(\"revision\");\n if (revision === null) {\n throw fakeError(\n `request(): ${request.path} is served for \\`?revision=<rev>\\` reads only ` +\n \"(time-based history reads are not simulated).\",\n );\n }\n const ids = readable(documents[1] as string);\n return {\n value: { documents: own.history.documentsAt({ ids, revision, current: own.documents() }) },\n };\n }\n const transactions = rest.match(/^\\/transactions\\/([^/]+)$/);\n if (transactions !== null) {\n // NDJSON, one transaction per line, as the History API streams it.\n const records = own.history.transactions(readable(transactions[1] as string), visible);\n return { value: records.map((record) => JSON.stringify(record)).join(\"\\n\") };\n }\n const events = rest.match(/^\\/events\\/documents\\/([^/]+)$/);\n if (events !== null) {\n return {\n value: {\n events: own.history.events(readable(events[1] as string)),\n nextCursor: \"\",\n },\n };\n }\n return undefined;\n}\n\n/** One membership of an Access API user row: the roles held on one resource. */\ninterface AccessMembership {\n resourceType: AccessResourceType;\n resourceId: string;\n roleNames: string[];\n}\n\n/** An Access API user row: the account-global id, a profile, and its memberships. */\ninterface AccessUserRecord {\n sanityUserId: string;\n profile: {\n id: string;\n displayName: string;\n email: string;\n provider: string;\n createdAt: string;\n };\n memberships: AccessMembership[];\n}\n\n/** One `user-permissions/me` row. */\ninterface PermissionRecord {\n type: string;\n resourceType: AccessResourceType;\n resourceId: string;\n action: string;\n}\n\n/** Serve the capability when the actor holds `permission` (if the row names one), else 403. */\nfunction serveGated<T>(args: {\n capability: Capability;\n permission: string | undefined;\n held: readonly string[];\n request: PlatformRequest;\n serve: () => T;\n}): T {\n const { capability, permission, held, request, serve } = args;\n return serveCapability(capability, request, () => {\n if (permission !== undefined && !held.includes(permission)) throw noPermission(request);\n return serve();\n });\n}\n\n/**\n * `GET /projects/<id>/roles`: the project role catalog, a bare array of\n * `{ name, title, description?, projectId, appliesToUsers, appliesToRobots }`,\n * gated like the Access API's project roles. The fake does not model role\n * definitions, so the platform's `isCustom` and `grants` are not served.\n */\nfunction serveProjectRoles(args: {\n ctx: PlatformApiContext;\n request: PlatformRequest;\n projectId: string;\n}): Served {\n const { ctx, request, projectId } = args;\n const row = ACCESS_CAPABILITIES.project;\n return serveGated({\n capability: row.roles,\n permission: row.rolesPermission,\n held: ctx.actorAccess()?.[accessKey(\"project\", projectId)]?.permissions ?? [],\n request,\n serve: () => ({\n value: rolesOn({\n resource: { type: \"project\", id: projectId },\n identities: ctx.identities(),\n }).map((role) => ({\n name: role.name,\n title: role.title,\n ...(role.description !== undefined ? { description: role.description } : {}),\n projectId,\n appliesToUsers: role.appliesToUsers,\n appliesToRobots: role.appliesToRobots,\n })),\n }),\n });\n}\n\nfunction serveAccess(args: {\n ctx: PlatformApiContext;\n request: PlatformRequest;\n type: string;\n id: string;\n rest: string;\n query: URLSearchParams;\n}): Served {\n const { ctx, request, type, id, rest, query } = args;\n const row = accessCapabilitiesOf(type);\n if (row === undefined) throw invalidAccessType(request, type);\n const resource: AccessResource = { type: type as AccessResourceType, id };\n const registry: AccessRegistry = {\n organizationId: ctx.organizationId,\n resources: ctx.resources,\n };\n const permissions = ctx.actorAccess()?.[accessKey(resource.type, id)]?.permissions ?? [];\n // `check` answers `false` even for an id the platform does not hold.\n if (rest === \"/user-permissions/me/check\") {\n const requested = query.getAll(\"permissions\").flatMap((value) => value.split(\",\"));\n return {\n value: { data: Object.fromEntries(requested.map((p) => [p, permissions.includes(p)])) },\n };\n }\n if (row.unknown !== undefined && !row.unknown.holds(registry, id)) {\n throw row.unknown.refusal(request);\n }\n const gated = <T>(capability: Capability, permission: string | undefined, serve: () => T): T =>\n serveGated({ capability, permission, held: permissions, request, serve });\n const users = <T>(capability: Capability, serve: () => T): T =>\n gated(capability, row.usersPermission, serve);\n const roles = () => rolesOn({ resource, identities: ctx.identities() });\n\n const user = rest.match(ACCESS_USER_PATH);\n if (user !== null) {\n return users(row.user, () => {\n const sanityUserId = decodeURIComponent(user[1] as string);\n const record = usersOn({ resource, registry, identities: ctx.identities() }).find(\n (candidate) => candidate.sanityUserId === sanityUserId,\n );\n if (record === undefined) throw notFound(\"User\")(request);\n return { value: record };\n });\n }\n\n const role = rest.match(ACCESS_ROLE_PATH);\n if (role !== null) {\n return gated(row.roles, row.rolesPermission, () => {\n const name = decodeURIComponent(role[1] as string);\n const record = roles().find((candidate) => candidate.name === name);\n if (record === undefined) throw notFound(\"Role\")(request);\n return { value: record };\n });\n }\n\n switch (rest) {\n case \"/roles\":\n return gated(row.roles, row.rolesPermission, () => ({ value: page(roles()) }));\n case \"/users\":\n return users(row.users, () => ({\n value: usersPage(usersOn({ resource, registry, identities: ctx.identities() })),\n }));\n case \"/user-permissions/me\":\n return {\n value: page(\n permissions.map((permission): PermissionRecord => {\n const dot = permission.lastIndexOf(\".\");\n return {\n type: permission.slice(0, dot),\n resourceType: resource.type,\n resourceId: id,\n action: permission.slice(dot + 1),\n };\n }),\n ),\n };\n default:\n return undefined;\n }\n}\n\n/** An Access API role row: the seeded role, on its resource. */\ninterface AccessRoleRecord {\n name: string;\n title: string;\n description?: string;\n resourceType: AccessResourceType;\n resourceId: string;\n appliesToUsers: boolean;\n appliesToRobots: boolean;\n permissions: { type: string; action: string; name?: string; params?: Record<string, unknown> }[];\n}\n\n/**\n * The platform's role fields the fake does not model: every role applies to\n * users and robots. `title` and `permissions` are required too: the title is\n * the role's name where the seed gives none, and the permissions are empty.\n */\nconst UNMODELLED_ROLE = { appliesToUsers: true, appliesToRobots: true } as const;\n\n/** Every role a member holds on the resource, once each, in canonical order. */\nfunction rolesOn(args: {\n resource: AccessResource;\n identities: TestClientIdentity[];\n}): AccessRoleRecord[] {\n const { resource } = args;\n const byName = new Map<string, AccessRoleRecord>();\n for (const identity of args.identities) {\n const roles = ACCESS_CAPABILITIES[resource.type].memberRoles({ identity, id: resource.id });\n for (const role of roles ?? []) {\n byName.set(role.name, {\n name: role.name,\n title: role.title ?? role.name,\n ...(role.description !== undefined ? { description: role.description } : {}),\n resourceType: resource.type,\n resourceId: resource.id,\n ...UNMODELLED_ROLE,\n permissions: [],\n });\n }\n }\n return [...byName.values()].toSorted((a, b) => a.name.localeCompare(b.name));\n}\n\n/** The identity's membership on `resource`, or `undefined` when it holds no role there. */\nfunction membershipOn(\n identity: TestClientIdentity,\n resource: AccessResource,\n): AccessMembership | undefined {\n const roles = ACCESS_CAPABILITIES[resource.type].memberRoles({ identity, id: resource.id });\n if (roles === undefined) return undefined;\n return {\n resourceType: resource.type,\n resourceId: resource.id,\n roleNames: roles.map((role) => role.name).toSorted(),\n };\n}\n\n/**\n * One row per member of the resource, keyed by account-global id.\n * A member holds a role on the resource itself; its row also lists its\n * memberships on every resource the resource owns.\n */\nfunction usersOn(args: {\n resource: AccessResource;\n registry: AccessRegistry;\n identities: TestClientIdentity[];\n}): AccessUserRecord[] {\n const { resource, registry, identities } = args;\n const owned = ACCESS_CAPABILITIES[resource.type].owns?.(registry) ?? [];\n const bySanityUserId = new Map<string, AccessUserRecord>();\n for (const identity of identities) {\n const own = membershipOn(identity, resource);\n if (own === undefined) continue;\n const user = identity.globalUser ?? identity.user;\n const record: AccessUserRecord = {\n sanityUserId: user.id,\n profile: {\n id: user.id,\n displayName: user.name ?? user.id,\n email: user.email ?? \"\",\n ...UNMODELLED_PROFILE,\n },\n memberships: [\n own,\n // A user is part of a resource only through a role on it.\n ...owned.flatMap((child) => {\n const membership = membershipOn(identity, child);\n return membership === undefined || membership.roleNames.length === 0 ? [] : [membership];\n }),\n ],\n };\n const existing = bySanityUserId.get(record.sanityUserId);\n if (existing !== undefined && JSON.stringify(existing) !== JSON.stringify(record)) {\n throw fakeError(\n `request(): two identities (registered, or the handle's inline accessControl) resolve to ` +\n `user \"${record.sanityUserId}\" on ${resource.type} \"${resource.id}\" with different ` +\n \"records. Fix the identities so duplicate ids carry identical records.\",\n );\n }\n bySanityUserId.set(record.sanityUserId, record);\n }\n return [...bySanityUserId.values()];\n}\n\n/** The page size \"Get My Attributes\" answers when the request names none. */\nconst DEFAULT_ATTRIBUTES_LIMIT = 50;\n\n/** One served attribute row: the seeded values plus the one in effect. */\ninterface UserAttributeRecord extends UserAttribute {\n activeSource: \"saml\" | \"sanity\";\n activeValue: UserAttributeValue;\n}\n\n/**\n * `GET /organizations/<id>/users/me/attributes`: the caller's own\n * attributes in the organization, ordered by key, one page of `limit` after\n * the `cursor` key. No permission is needed; a caller outside the\n * organization is not found there.\n */\nfunction serveMyAttributes(args: {\n ctx: PlatformApiContext;\n request: PlatformRequest;\n organizationId: string;\n query: URLSearchParams;\n}): Served {\n const { ctx, request, organizationId, query } = args;\n const organization = ACCESS_CAPABILITIES.organization;\n const registry: AccessRegistry = { organizationId: ctx.organizationId, resources: ctx.resources };\n if (organization.unknown !== undefined && !organization.unknown.holds(registry, organizationId)) {\n throw organization.unknown.refusal(request);\n }\n // Organization membership is a role in the actor's organization entry.\n const entry = ctx.actorAccess()?.[accessKey(\"organization\", organizationId)];\n if (entry?.roles === undefined) throw notFound(\"User\")(request);\n const limit = attributesLimit(query);\n const cursor = query.get(\"cursor\");\n const attributes = (entry.attributes ?? [])\n .map(attributeRecord)\n .toSorted((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))\n .filter((attribute) => cursor === null || attribute.key > cursor)\n .slice(0, limit);\n return { value: { sanityUserId: ctx.actorUser().id, organizationId, attributes } };\n}\n\nfunction attributesLimit(query: URLSearchParams): number {\n const raw = query.get(\"limit\");\n if (raw === null) return DEFAULT_ATTRIBUTES_LIMIT;\n const limit = Number(raw);\n if (!Number.isInteger(limit) || limit < 1) {\n throw fakeError(\n `request(): users/me/attributes got limit=${JSON.stringify(raw)}; the fake serves positive ` +\n \"integer limits only (the platform's answer to others is not simulated).\",\n );\n }\n return limit;\n}\n\n/** Sanity values take precedence over SAML ones, as the platform resolves them. */\nfunction attributeRecord(attribute: UserAttribute): UserAttributeRecord {\n const { key, type, values } = attribute;\n const activeSource = values.sanity !== undefined ? \"sanity\" : \"saml\";\n const activeValue = values[activeSource];\n if (activeValue === undefined) {\n throw fakeError(\n `request(): user attribute \"${key}\" is seeded with no value; give it a \\`saml\\` or ` +\n \"`sanity` value.\",\n );\n }\n return { type, key, values: { ...values }, activeSource, activeValue };\n}\n","/**\n * Optional access-control simulation for the in-memory test client.\n *\n * When configured, the TestClient does two things a real `@sanity/client`\n * does over the wire but the bare in-memory client otherwise skips:\n *\n * 1. **Exposes `client.request({ url })`** that serves:\n * - `GET /users/me` → the configured `CurrentUser` — the project-scoped\n * record on a project-hosted handle, `globalUser` on a\n * `useProjectHostname: false` handle (Sanity's global API host,\n * set at creation or via `withConfig`)\n * - `GET <aclPath>` → the configured `Grant[]`\n * - `GET /projects/<projectId>` → the project's members (ids, roles, and robot flags)\n * - `GET /projects/<projectId>/users/<ids>` → project-user records\n * carrying `id` + `sanityUserId`, the bridge between the project\n * and global identity namespaces\n * - `GET /projects/<projectId>/roles` → the project role catalog\n * - `GET /projects/<projectId>/features` → the project's feature names\n * This is what the engine reads from to resolve `WorkflowAccess`\n * when no override is passed.\n *\n * 2. **Gates writes through the configured grants.** Every mutation\n * (`create`, `createOrReplace`, `createIfNotExists`, `delete`,\n * `patch`, and per-op inside `transaction`) checks whether the\n * configured grants allow the relevant `DocumentValuePermission`\n * on the target document. On denial the client throws a\n * `WriteAccessDeniedError` shaped like Sanity's 403 — the same\n * failure mode a production write would hit, surfaced in tests.\n *\n * When `accessControl` is omitted, the client behaves as before:\n * `request` is undefined, writes are unconditional, and the engine\n * falls through to whatever override the caller (typically the\n * bench's `ALL_ACCESS`) provides.\n *\n * A filter grant is evaluated by running its GROQ filter on a singleton\n * dataset containing the doc being checked — same shape as the engine's\n * `permissions.matchesFilter`, lifted from canvas. A path grant matches\n * the doc's `_id` element by element (see `./token-grants.ts`).\n */\n\nimport { ClientError } from \"@sanity/client\";\nimport type { SanityDocumentLike } from \"@sanity/types\";\nimport { evaluate, parse } from \"groq-js\";\n\nimport { fakeError } from \"./errors.ts\";\nimport { pathIncludes } from \"./token-grants.ts\";\nimport { refuseAclRequest, servePlatformRequest, type PlatformApiContext } from \"./platform-api.ts\";\nimport {\n DEFAULT_WRITE,\n isProjectMember,\n transactionRefused,\n type PlatformRequest,\n} from \"./resources.ts\";\n\nexport type DocumentValuePermission = \"create\" | \"manage\" | \"read\" | \"update\";\n\nexport interface CurrentUser {\n id: string;\n name?: string;\n email?: string;\n /** @deprecated singular `role` on legacy responses */\n role?: string;\n roles?: { name: string; title?: string; description?: string }[];\n}\n\n/**\n * One grant: `permissions` on the documents a GROQ `filter` matches, as an\n * ACL serves them, or on the ids a `path()` glob matches, as a token\n * carries them (the token's `{ s, p }`). The Content Lake holds a grant as\n * one or the other and evaluates a path grant as `_id in path(\"<path>\")`\n * (gradient `pkg/security/models.go` `Grant`, `FilterExpression`). A token\n * grant's permissions are whatever the token names; the Content Lake\n * refuses the token when one is not a permission it knows, or when the\n * path does not parse (see `./token-grants.ts`).\n */\nexport type Grant =\n | { filter: string; permissions: DocumentValuePermission[] }\n | { path: string; permissions: string[] };\n\n/**\n * What the Access API (`/access/<type>/<id>/…`) answers about one actor on\n * one resource: the roles it holds there (served by `…/users` and collected\n * into `…/roles`) and the permissions `…/user-permissions/me` and\n * `…/user-permissions/me/check` report, as `\"<type>.<action>\"` names such\n * as `\"sanity.media-library.read\"`.\n */\nexport interface ResourceAccess {\n roles?: NonNullable<CurrentUser[\"roles\"]>;\n permissions?: string[];\n /**\n * The user's attributes in the organization, as an administrator or SAML\n * sync set them; read on `\"organization:<id>\"` entries only. Served by\n * `GET /organizations/<id>/users/me/attributes`.\n */\n attributes?: UserAttribute[];\n}\n\n/** A value a user attribute can hold. */\nexport type UserAttributeValue = string | number | boolean | string[] | number[] | boolean[];\n\n/**\n * One user attribute: its key, its type, and its value from each source.\n * The served row adds `activeSource` and `activeValue`: the Sanity value\n * when there is one, else the SAML value.\n */\nexport interface UserAttribute {\n key: string;\n type: string;\n values: { saml?: UserAttributeValue; sanity?: UserAttributeValue };\n}\n\nexport interface TestClientAccessControl {\n /** What `client.request({ url: \"/users/me\" })` returns on a project-hosted handle. */\n currentUser: CurrentUser;\n /**\n * What `/users/me` returns on a global-hosted handle\n * (`useProjectHostname: false`, at creation or via `withConfig`).\n * Sanity serves the\n * account-global user there (`g*` id, no roles) while the project host\n * serves the project-scoped record (`p*` id, project roles). Omit to\n * serve `currentUser` from both — the single-id collapse that is also\n * exactly how robot tokens behave in production.\n */\n globalUser?: CurrentUser;\n /** What `client.request({ url: aclPath })` returns. */\n grants: Grant[];\n /**\n * URL path the engine's access resolver hits to fetch grants.\n * Defaults to the handle's resource ACL: `/projects/<projectId>/datasets/<dataset>/acl`,\n * or `<resource base>/acl` on a `resource`-configured handle.\n */\n aclPath?: string;\n /**\n * Access API entries, keyed `\"<access type>:<id>\"` — `\"organization:<id>\"`,\n * `\"project:<projectId>\"`, `\"media-library:<id>\"`, `\"canvas:<id>\"`, `\"dashboard:<id>\"`. See {@link ResourceAccess}.\n */\n access?: Record<string, ResourceAccess>;\n}\n\n/**\n * One entry in the token→identity registry (`createTestClient({ identities })`).\n * A handle bound to the entry's token behaves exactly like one configured\n * with `accessControl: { currentUser: projectUserOf(identity, projectId),\n * globalUser, grants }` — `/users/me` serves the record for the handle's\n * project and host, the default ACL path serves `grants`, and reads/writes\n * are gated through them.\n */\nexport interface TestClientIdentity {\n /**\n * The project-scoped user record: what `/users/me` returns on a\n * project-hosted handle, and the id `identity()` resolves to in grant\n * filters and mutation guards. Per-project variants go in\n * `usersByProject`; this is the record for any project without one.\n */\n user: CurrentUser;\n /**\n * The account-global user record, served by `/users/me` on a\n * global-hosted handle (`useProjectHostname: false`, at creation or\n * via `withConfig`).\n * The same person carries a different id per project (`p*`) and one\n * global id (`g*`); registering both lets tests model that split.\n * Omit for single-id principals (robot tokens resolve to one `p-*` id\n * on every host).\n */\n globalUser?: CurrentUser;\n /**\n * Per-project user records, keyed by projectId — the same token\n * resolves to a different project-scoped id in each project, which is\n * what these model. A handle's project falls back to `user` when its\n * projectId has no entry.\n */\n usersByProject?: Record<string, CurrentUser>;\n /** Grants served at the ACL path and enforced on reads/writes. */\n grants: Grant[];\n /**\n * Access API entries, keyed `\"<access type>:<id>\"` (`\"organization:<id>\"`,\n * `\"media-library:<id>\"`, `\"canvas:<id>\"`, `\"dashboard:<id>\"`,\n * `\"project:<projectId>\"`). On a non-project resource, an entry with\n * `roles` makes the identity a member there. Project membership needs no\n * entry: see `projectId`.\n */\n access?: Record<string, ResourceAccess>;\n /**\n * The project a project-scoped token belongs to, as a robot token belongs\n * to the project that issued it. The identity is a member of that project\n * only, and its token is refused on any other project's API host. Omit\n * for a token the fake treats as a member of every project, accepted on\n * every host.\n */\n projectId?: string;\n}\n\n/**\n * A row served by the project-users directory endpoint\n * (`/projects/<projectId>/users/<id>`), mirroring Sanity's project user\n * retrieval API: the project-scoped `id` alongside its account-global\n * `sanityUserId` — the one sanctioned bridge between the two namespaces.\n */\nexport interface ProjectUserRecord {\n id: string;\n sanityUserId: string;\n displayName: string;\n email?: string;\n}\n\n/**\n * The Content Lake's 403 when a configured-grants check rejects a write: a\n * `ClientError` built from the response gradient sends, an\n * `insufficientPermissionsError` wrapped in a `mutationError`\n * (`pkg/security/errors.go` `InsufficientPermissionsError`). It also carries\n * the doc id, the attempted permission, and the count of matching grants\n * (for parity with the engine's `permission-denied` disabled-reason), which\n * the platform's answer does not name.\n */\nexport class WriteAccessDeniedError extends ClientError {\n readonly documentId: string;\n readonly permission: DocumentValuePermission;\n readonly matchingGrants: number;\n\n constructor(args: {\n documentId: string;\n permission: DocumentValuePermission;\n matchingGrants: number;\n /** The write the Content Lake refused; defaults to a `/data/mutate` request. */\n request?: PlatformRequest;\n }) {\n super(\n transactionRefused(args.request ?? DEFAULT_WRITE, {\n description: `Insufficient permissions; permission \"${args.permission}\" required`,\n permission: args.permission,\n type: \"insufficientPermissionsError\",\n }),\n );\n this.name = \"WriteAccessDeniedError\";\n this.documentId = args.documentId;\n this.permission = args.permission;\n this.matchingGrants = args.matchingGrants;\n }\n}\n\n/**\n * Predicate variant of `evaluateWriteAccess` — same logic but only\n * returns the boolean. Used by read-gating paths\n * (`fetch`/`getDocument`/`getDocuments`) where the documented Sanity\n * behavior is \"invisible doc → 404\", not \"throw 403\". So callers\n * can filter the dataset before GROQ runs and return `null` from\n * single-doc reads, instead of leaking a denial through the read\n * surface.\n */\nexport async function canAccess(args: {\n document: SanityDocumentLike & { _id?: string; _type?: string };\n grants: Grant[];\n permission: DocumentValuePermission;\n userId: string;\n}): Promise<boolean> {\n const result = await evaluateWriteAccess(args);\n return result.allowed;\n}\n\n// Parsed-filter cache. The GROQ AST is cheap to reuse; only the\n// per-document evaluation needs to re-run.\nconst parsedFilters = new Map<string, ReturnType<typeof parse>>();\n\nasync function matchesFilter(args: {\n document: SanityDocumentLike & { _id?: string; _type?: string };\n filter: string;\n userId: string;\n}): Promise<boolean> {\n const { document, filter, userId } = args;\n if (!parsedFilters.has(filter)) {\n parsedFilters.set(filter, parse(`*[${filter}]`));\n }\n const ast = parsedFilters.get(filter)!;\n const result = await evaluate(ast, {\n dataset: [document as Record<string, unknown>],\n identity: userId,\n });\n const data = await result.get();\n return Array.isArray(data) && data.length === 1;\n}\n\n/** Whether `grant` covers `document`: its path glob includes the `_id`, or its filter matches. */\nasync function grantMatches(args: {\n document: SanityDocumentLike & { _id?: string; _type?: string };\n grant: Grant;\n userId: string;\n}): Promise<boolean> {\n const { document, grant, userId } = args;\n if (\"path\" in grant) {\n return typeof document._id === \"string\" && pathIncludes(grant.path, document._id);\n }\n return matchesFilter({ document, filter: grant.filter, userId });\n}\n\n/**\n * Resolve whether `grants` allow `permission` on `document` for the\n * given user. Same logic the engine uses in `permissions.grantsPermissionOn`\n * (most-permissive wins; multiple grants compose).\n *\n * Returns `{ allowed: boolean, matchingGrants: number }` so callers\n * can build the structured error without re-counting.\n */\nexport async function evaluateWriteAccess(args: {\n document: SanityDocumentLike & { _id?: string; _type?: string };\n grants: Grant[];\n permission: DocumentValuePermission;\n userId: string;\n}): Promise<{ allowed: boolean; matchingGrants: number }> {\n const { document, grants, permission, userId } = args;\n let matching = 0;\n for (const grant of grants) {\n if (await grantMatches({ document, grant, userId })) {\n matching++;\n if (grant.permissions.includes(permission)) {\n return { allowed: true, matchingGrants: matching };\n }\n }\n }\n return { allowed: false, matchingGrants: matching };\n}\n\n/**\n * Throw a `WriteAccessDeniedError` if `grants` don't permit the\n * supplied `permission` on `document`. No-op when allowed. Use this\n * from every mutation entrypoint that wants to simulate Sanity's\n * write boundary.\n */\nexport async function enforceWriteAccess(args: {\n document: SanityDocumentLike & { _id?: string; _type?: string };\n grants: Grant[];\n permission: DocumentValuePermission;\n userId: string;\n request: PlatformRequest;\n}): Promise<void> {\n const { document, grants, permission, userId, request } = args;\n const { allowed, matchingGrants } = await evaluateWriteAccess({\n document,\n grants,\n permission,\n userId,\n });\n if (allowed) return;\n if (typeof document._id !== \"string\") {\n throw new WriteAccessDeniedError({\n documentId: \"(unknown)\",\n permission,\n matchingGrants,\n request,\n });\n }\n throw new WriteAccessDeniedError({\n documentId: document._id,\n permission,\n matchingGrants,\n request,\n });\n}\n\n/**\n * Default ACL path the engine pulls grants from. Mirrors Sanity's\n * project/dataset ACL endpoint. Callers can override via\n * `accessControl.aclPath`.\n */\nexport function defaultAclPath(projectId: string, dataset: string): string {\n return `/projects/${projectId}/datasets/${dataset}/acl`;\n}\n\n/** Everything {@link makeRequestImpl} needs from the owning handle. */\nexport interface RequestImplContext {\n /**\n * Active access-control resolver. A getter, not a value — token-bound\n * handles resolve their identity from the shared registry on every\n * call, so a re-registered identity (`addIdentity` upsert) takes\n * effect on already-minted siblings.\n */\n getAccess: () => TestClientAccessControl;\n /** The request `@sanity/client` sends for `request(opts)` on the handle, full URL included. */\n requestFor: (opts: { url?: string; uri?: string; tag?: string | null }) => PlatformRequest;\n projectId: string;\n /**\n * Whether this handle addresses the project host (default) or the\n * global API host (`useProjectHostname: false`, at creation or via\n * `withConfig`).\n * Decides which user record `/users/me` serves.\n */\n useProjectHostname: boolean;\n /**\n * The handle's members (registered identities, plus its inline\n * `accessControl` actor), for the project endpoints. A getter so\n * `addIdentity` upserts are visible.\n */\n listIdentities: () => TestClientIdentity[];\n /** See {@link CreateTestClientOptions.organizationId}; added to `/projects/<projectId>`. */\n organizationId: string | undefined;\n /** History, ACL, resource lookup and Access API routing; see `./platform-api.ts`. */\n platform: PlatformApiContext;\n}\n\n/** The project-scoped record a registry identity resolves to in `projectId`. */\nexport function projectUserOf(identity: TestClientIdentity, projectId: string): CurrentUser {\n return identity.usersByProject?.[projectId] ?? identity.user;\n}\n\n/**\n * Deduplicate tokens that resolve to one project user. Callers supply served\n * records in canonical form so stringify equality represents semantic\n * equality; divergent records fail instead of producing a last-wins answer.\n */\nfunction consistentProjectRecords<RecordType extends { id: string }>(args: {\n projectId: string;\n records: readonly RecordType[];\n}): Map<string, RecordType> {\n const byId = new Map<string, RecordType>();\n for (const record of args.records) {\n const existing = byId.get(record.id);\n if (existing !== undefined && JSON.stringify(existing) !== JSON.stringify(record)) {\n throw fakeError(\n `request(): two identities (registered, or the handle's inline accessControl) resolve to ` +\n `project user \"${record.id}\" in project ` +\n `\"${args.projectId}\" with different records. The project records cannot be served ` +\n `unambiguously — fix the identities so duplicate ids carry identical records.`,\n );\n }\n byId.set(record.id, record);\n }\n return byId;\n}\n\n/** Normalize role key and array order for stable serving and duplicate comparison. */\nfunction canonicalProjectRoles(roles: NonNullable<CurrentUser[\"roles\"]>) {\n return roles\n .map((role) => ({\n name: role.name,\n ...(role.title !== undefined ? { title: role.title } : {}),\n ...(role.description !== undefined ? { description: role.description } : {}),\n }))\n .toSorted((left, right) => {\n const leftKey = JSON.stringify(left);\n const rightKey = JSON.stringify(right);\n if (leftKey < rightKey) return -1;\n if (leftKey > rightKey) return 1;\n return 0;\n });\n}\n\n/**\n * Serve `/projects/<projectId>/users/<ids>` from the identity registry —\n * the fake's mirror of Sanity's project user retrieval API, the bridge\n * between project-scoped ids and `sanityUserId`. Single id → one record;\n * comma-separated ids → array. Unknown single id throws with\n * `statusCode: 404`, like production; unknown ids in a list are omitted.\n */\nfunction serveProjectUsers(args: {\n requestedIds: string;\n pathProjectId: string;\n identities: TestClientIdentity[];\n}): ProjectUserRecord | ProjectUserRecord[] {\n const { requestedIds, pathProjectId, identities } = args;\n const toRecord = (identity: TestClientIdentity): ProjectUserRecord => {\n const projectUser = projectUserOf(identity, pathProjectId);\n return {\n id: projectUser.id,\n sanityUserId: identity.globalUser?.id ?? projectUser.id,\n displayName: projectUser.name ?? projectUser.id,\n ...(projectUser.email !== undefined ? { email: projectUser.email } : {}),\n };\n };\n const byId = consistentProjectRecords({\n projectId: pathProjectId,\n records: identities\n .filter((identity) => isProjectMember(identity.projectId, pathProjectId))\n .map(toRecord),\n });\n const ids = requestedIds.split(\",\");\n const [firstId] = ids;\n if (ids.length === 1 && firstId !== undefined) {\n const record = byId.get(firstId);\n if (record === undefined) {\n throw Object.assign(\n fakeError(\n `request(): no registered identity resolves to project user \"${firstId}\" in project ` +\n `\"${pathProjectId}\". Register it via createTestClient({ identities }) or addIdentity().`,\n ),\n { statusCode: 404 },\n );\n }\n return record;\n }\n return ids.flatMap((id) => {\n const record = byId.get(id);\n return record === undefined ? [] : [record];\n });\n}\n\nconst PROJECT_USERS_PATH = /^\\/projects\\/([^/]+)\\/users\\/([^/]+)$/;\n\nconst PROJECT_PATH = /^\\/projects\\/([^/]+)$/;\n\n/** One row of the `/projects/<projectId>` members response. */\nexport interface ProjectMemberRecord {\n id: string;\n isRobot?: boolean;\n /** Project-scoped roles in canonical order, independent of registration order. */\n roles?: NonNullable<CurrentUser[\"roles\"]>;\n}\n\n/**\n * Serve `/projects/<projectId>` from the identity registry — the members\n * half of Sanity's project API, which member-list joins read\n * (`members[].id` carries the project-scoped principal id and `roles` carries\n * its project roles; robot principals are flagged `isRobot`, matching their\n * production `p-…` form). One row per distinct resolved project user, however\n * many tokens map to that person.\n */\nfunction serveProject(args: {\n pathProjectId: string;\n identities: TestClientIdentity[];\n organizationId: string | undefined;\n}): {\n organizationId?: string;\n members: ProjectMemberRecord[];\n} {\n const members = args.identities.filter((identity) =>\n isProjectMember(identity.projectId, args.pathProjectId),\n );\n const records = members.map((identity) => {\n const projectUser = projectUserOf(identity, args.pathProjectId);\n return {\n id: projectUser.id,\n ...(projectUser.id.startsWith(\"p-\") ? { isRobot: true } : {}),\n ...(projectUser.roles !== undefined\n ? { roles: canonicalProjectRoles(projectUser.roles) }\n : {}),\n } satisfies ProjectMemberRecord;\n });\n const byId = consistentProjectRecords({ projectId: args.pathProjectId, records });\n return {\n ...(args.organizationId !== undefined ? { organizationId: args.organizationId } : {}),\n members: [...byId.values()],\n };\n}\n\n/**\n * Internal helper: turn the active access-control config into the bound\n * `request` function the client exposes.\n */\nexport function makeRequestImpl(\n ctx: RequestImplContext,\n): <T>(opts: {\n url?: string;\n uri?: string;\n signal?: AbortSignal;\n tag?: string | null;\n}) => Promise<T> {\n const { getAccess, requestFor, useProjectHostname, listIdentities, organizationId, platform } =\n ctx;\n return async <T>(opts: {\n url?: string;\n uri?: string;\n signal?: AbortSignal;\n tag?: string | null;\n }): Promise<T> => {\n const accessControl = getAccess();\n // `@sanity/client` distinguishes `uri` (project-scoped, e.g.\n // `/users/me`) from `url` (raw). Treat both identically for the mock —\n // host selection rides the handle's `useProjectHostname`, as in\n // production, not the option name.\n const path = opts.uri ?? opts.url;\n if (path === \"/users/me\") {\n // The global host serves the account-global user; the project host\n // serves the project-scoped record. Absent `globalUser` both serve\n // the same record — the single-id shape robot tokens have in\n // production.\n const user = useProjectHostname\n ? accessControl.currentUser\n : (accessControl.globalUser ?? accessControl.currentUser);\n return user as unknown as T;\n }\n // An explicit `aclPath` aliases the grants, but never past the platform's refusal of that ACL.\n if (path !== undefined && path === accessControl.aclPath) {\n refuseAclRequest(platform, requestFor(opts));\n return accessControl.grants as unknown as T;\n }\n const projectUsers = path?.match(PROJECT_USERS_PATH);\n const [, pathProjectId, requestedIds] = projectUsers ?? [];\n if (pathProjectId !== undefined && requestedIds !== undefined) {\n return serveProjectUsers({\n requestedIds,\n pathProjectId,\n identities: listIdentities(),\n }) as unknown as T;\n }\n const project = path?.match(PROJECT_PATH);\n const [, membersProjectId] = project ?? [];\n if (membersProjectId !== undefined) {\n return serveProject({\n pathProjectId: membersProjectId,\n identities: listIdentities(),\n organizationId,\n }) as unknown as T;\n }\n const served =\n path === undefined ? undefined : await servePlatformRequest(platform, path, requestFor(opts));\n if (served !== undefined) return served.value as T;\n throw fakeError(\n `request(): no handler for path \"${path}\". With access control configured, the fake serves ` +\n `\"/users/me\", the handle's ACL (\"<data base>/acl\"), \"/projects/<projectId>\" (members), ` +\n `\"/projects/<projectId>/users/<ids>\", \"/projects/<projectId>/roles\", \"/projects/<projectId>/features\", the History API (client.getDataUrl(\"history\", …)), ` +\n `resource lookup (\"/media-libraries/<id>\", \"/canvases/<id>\", \"/dashboards/<id>\") and the ` +\n `Access API (\"/access/<type>/<id>/roles[/<name>] | users[/<id>] | user-permissions/me[/check]\") ` +\n `and the caller's attributes (\"/organizations/<id>/users/me/attributes\"). For any ` +\n `other endpoint, register a stub (client.stub(\"request\", fn)) or intercept the HTTP call ` +\n `with a tool like MSW.`,\n );\n };\n}\n","import { ClientError } from \"@sanity/client\";\nimport type { SanityDocument, SanityDocumentLike } from \"@sanity/types\";\n\nimport { DEFAULT_WRITE, transactionRefused, type PlatformRequest } from \"./resources.ts\";\n\n/** The Content Lake mutation actions that a {@link MutationGuard} can match. */\nexport type MutationGuardAction = \"create\" | \"update\" | \"delete\";\n\nexport interface MutationGuardDenial {\n guardID: string;\n name?: string;\n}\n\n/** The `response` of a {@link GuardDeniedError}: the Content Lake's 403 for a guarded write. */\nexport interface GuardDeniedResponse {\n body: {\n error: {\n description: string;\n items: [{ error: GuardDeniedDetails; index: 0 }];\n type: \"mutationError\";\n };\n };\n url: string;\n method: string;\n headers: Record<string, string>;\n statusCode: 403;\n statusMessage: \"Forbidden\";\n}\n\n/** The `guardDeniedError` gradient writes for a guard's denial (`proto.GuardDeniedError`). */\nexport interface GuardDeniedDetails {\n description: string;\n denials: MutationGuardDenial[];\n type: \"guardDeniedError\";\n}\n\n/**\n * The Content Lake's 403 when a mutation guard denies a write: a\n * `ClientError` built from the response gradient sends, a\n * `guardDeniedError` wrapped in a `mutationError`. `denials` and `type` lift\n * the guard's answer to the top for callers that match on them.\n */\nexport class GuardDeniedError extends ClientError {\n readonly type = \"guardDeniedError\";\n readonly denials: MutationGuardDenial[];\n declare readonly statusCode: 403;\n declare readonly response: GuardDeniedResponse;\n\n constructor(denials: readonly MutationGuardDenial[], request: PlatformRequest = DEFAULT_WRITE) {\n const unique = uniqueDenials(denials);\n super(\n transactionRefused(request, {\n description: guardDeniedDescription(unique),\n denials: unique,\n type: \"guardDeniedError\",\n }),\n );\n this.name = \"GuardDeniedError\";\n this.denials = unique;\n }\n}\n\n/** gradient's `guard.DenyError.Error()` (`pkg/documents/guard/errors.go`). */\nfunction guardDeniedDescription(denials: readonly MutationGuardDenial[]): string {\n const named = denials.map(({ guardID, name }) => (name ? `${guardID} (${name})` : guardID));\n return `mutation denied by ${denials.length} guard(s): ${named.join(\" \")}`;\n}\n\nfunction uniqueDenials(denials: readonly MutationGuardDenial[]): MutationGuardDenial[] {\n const seen = new Set<string>();\n return denials.filter((denial) => {\n if (seen.has(denial.guardID)) return false;\n seen.add(denial.guardID);\n return true;\n });\n}\n\n/** The before/final-after image of one net document change in a commit. */\nexport interface MutationGuardContext {\n action: MutationGuardAction;\n /** Original live store image. Treat it as read-only; it is not a copy. */\n before: Readonly<SanityDocumentLike & { _id: string; _type: string }> | null;\n /** Final live store image. Treat it as read-only; it is not a copy. */\n after: Readonly<SanityDocumentLike & { _id: string; _type: string }> | null;\n /** Acting project identity, or `undefined` on an identity-free client. */\n identity: string | undefined;\n}\n\n/**\n * Evaluates one net document change and returns every denying guard. Returning\n * `void` allows observe-only hooks. A thrown value is a callback failure and\n * propagates unchanged; only returned entries become {@link GuardDeniedError}.\n */\nexport type MutationGuard = (\n context: MutationGuardContext,\n) => readonly MutationGuardDenial[] | void | Promise<readonly MutationGuardDenial[] | void>;\n\nexport interface MutationImages {\n before: SanityDocument | undefined;\n after: SanityDocument | undefined;\n}\n\nexport type MutationGuardImageContext = Omit<MutationGuardContext, \"identity\">;\n\n/** Derive the single lake action represented by a document's net commit image. */\nexport function mutationContext(images: MutationImages): MutationGuardImageContext | undefined {\n const { before, after } = images;\n // TestStore preserves object identity until a write replaces the document.\n // Equal references therefore mean an ID was remembered but never changed.\n if (before === after) return undefined;\n if (before === undefined) return { action: \"create\", before: null, after: after ?? null };\n if (after === undefined) return { action: \"delete\", before, after: null };\n return { action: \"update\", before, after };\n}\n\n/** Derive guard contexts for an ordered collection of net document images. */\nexport function mutationContexts(images: Iterable<MutationImages>): MutationGuardImageContext[] {\n return [...images].flatMap((image) => {\n const context = mutationContext(image);\n return context === undefined ? [] : [context];\n });\n}\n\n/** Evaluate every net context before rejecting once with all returned denials. */\nexport async function enforceMutationGuards(\n guard: MutationGuard | undefined,\n contexts: readonly MutationGuardContext[],\n request: PlatformRequest,\n): Promise<void> {\n if (guard === undefined) return;\n const denials: MutationGuardDenial[] = [];\n for (const context of contexts) {\n const denied = await guard(context);\n if (denied !== undefined) denials.push(...denied);\n }\n if (denials.length > 0) throw new GuardDeniedError(denials, request);\n}\n","import type { SanityDocument } from \"@sanity/types\";\nimport { fakeError } from \"./errors.ts\";\n\n/** Human-readable type of a value, for \"expected an array but got …\" errors. */\nfunction describeType(value: unknown): string {\n if (value === null) return \"null\";\n if (Array.isArray(value)) return \"an array\";\n return `a ${typeof value}`;\n}\n\n/**\n * Patch operations the test client supports.\n *\n * Subset of `@sanity/client`'s PatchOperations. Each entry is one\n * mutation step applied in order at commit time.\n */\nexport type PatchOp =\n | { kind: \"set\"; props: Record<string, unknown> }\n | { kind: \"setIfMissing\"; props: Record<string, unknown> }\n | { kind: \"unset\"; paths: string[] }\n | { kind: \"inc\"; props: Record<string, number> }\n | { kind: \"dec\"; props: Record<string, number> }\n | {\n kind: \"insert\";\n position: \"before\" | \"after\" | \"replace\";\n anchor: string;\n items: unknown[];\n };\n\nexport interface PatchHandle {\n set: (props: Record<string, unknown>) => PatchHandle;\n setIfMissing: (props: Record<string, unknown>) => PatchHandle;\n unset: (paths: string[]) => PatchHandle;\n inc: (props: Record<string, number>) => PatchHandle;\n dec: (props: Record<string, number>) => PatchHandle;\n insert: (\n position: \"before\" | \"after\" | \"replace\",\n anchor: string,\n items: unknown[],\n ) => PatchHandle;\n ifRevisionId: (rev: string) => PatchHandle;\n commit: (options?: PatchCommitOptions) => Promise<SanityDocument>;\n /** Test-only introspection — not on real Patch */\n toJSON: () => { id: string; ops: PatchOp[]; ifRev?: string };\n}\n\n/** Options for `PatchHandle.commit()`. Only `tag` is meaningful to the fake. */\nexport interface PatchCommitOptions {\n /** Request tag, recorded on the mutation log entry (`null` = explicit suppression). */\n tag?: string | null;\n /** Accepted for signature parity; not simulated. */\n visibility?: \"sync\" | \"async\" | \"deferred\";\n}\n\nexport interface PatchExecutor {\n apply: (\n documentId: string,\n ops: PatchOp[],\n ifRev: string | undefined,\n options: PatchCommitOptions | undefined,\n ) => Promise<SanityDocument>;\n}\n\nexport function createPatch(documentId: string, executor: PatchExecutor): PatchHandle {\n const ops: PatchOp[] = [];\n let ifRev: string | undefined;\n\n const handle: PatchHandle = {\n set: (props) => {\n ops.push({ kind: \"set\", props });\n return handle;\n },\n setIfMissing: (props) => {\n ops.push({ kind: \"setIfMissing\", props });\n return handle;\n },\n unset: (paths) => {\n ops.push({ kind: \"unset\", paths });\n return handle;\n },\n inc: (props) => {\n ops.push({ kind: \"inc\", props });\n return handle;\n },\n dec: (props) => {\n ops.push({ kind: \"dec\", props });\n return handle;\n },\n insert: (position, anchor, items) => {\n ops.push({ kind: \"insert\", position, anchor, items });\n return handle;\n },\n ifRevisionId: (rev) => {\n ifRev = rev;\n return handle;\n },\n commit: (options) => executor.apply(documentId, ops, ifRev, options),\n toJSON: () => ({\n id: documentId,\n ops: [...ops],\n ...(ifRev !== undefined ? { ifRev } : {}),\n }),\n };\n\n return handle;\n}\n\n/**\n * Apply an ordered list of patch ops to a document, returning the new doc.\n *\n * The path semantics are deliberately a small subset:\n *\n * - **Dotted paths** (`a.b.c`) for nested object fields.\n * - **Array index** (`a.b[0]`) for positional access into arrays.\n * - **Keyed lookup** (`a.b[_key==\"x\"]`) for finding an array entry by `_key`.\n *\n * Anything more exotic (recursive `..`, GROQ filter expressions, multi-match)\n * is intentionally out of scope and will throw — the workflow engine only\n * needs the three forms above.\n */\nexport function applyPatchOps(doc: SanityDocument, ops: PatchOp[]): SanityDocument {\n let next: SanityDocument = structuredClone(doc);\n for (const op of ops) {\n next = applyOne(next, op);\n }\n return next;\n}\n\nfunction applyOne(doc: SanityDocument, op: PatchOp): SanityDocument {\n switch (op.kind) {\n case \"set\":\n for (const [path, value] of Object.entries(op.props)) {\n setAtPath(doc, path, value);\n }\n return doc;\n case \"setIfMissing\":\n for (const [path, value] of Object.entries(op.props)) {\n if (getAtPath(doc, path) === undefined) {\n setAtPath(doc, path, value);\n }\n }\n return doc;\n case \"unset\":\n for (const path of op.paths) {\n unsetAtPath(doc, path);\n }\n return doc;\n case \"inc\":\n for (const [path, delta] of Object.entries(op.props)) {\n const current = getAtPath(doc, path);\n const base = typeof current === \"number\" ? current : 0;\n setAtPath(doc, path, base + delta);\n }\n return doc;\n case \"dec\":\n for (const [path, delta] of Object.entries(op.props)) {\n const current = getAtPath(doc, path);\n const base = typeof current === \"number\" ? current : 0;\n setAtPath(doc, path, base - delta);\n }\n return doc;\n case \"insert\": {\n insertIntoArray(doc, op.position, op.anchor, op.items);\n return doc;\n }\n }\n}\n\ninterface PathSegment {\n key: string;\n // Optional array selector. Either a numeric index or a key=value match.\n selector?: { kind: \"index\"; index: number } | { kind: \"keyed\"; field: string; value: string };\n}\n\nfunction parsePath(path: string): PathSegment[] {\n // Examples:\n // \"currentStageId\"\n // \"taskStatus[0].status\"\n // \"taskStatus[_key==\\\"approve\\\"].status\"\n // \"context[key==\\\"releaseId\\\"].value\"\n const segments: PathSegment[] = [];\n for (const raw of path.split(\".\")) {\n const match = /^([A-Za-z_$][\\w$]*)(?:\\[(.+)\\])?$/.exec(raw);\n if (!match) {\n throw fakeError(\n `Unsupported patch path segment \"${raw}\" (in path \"${path}\"). This fake supports dotted ` +\n `field access (\"a.b.c\"), array index (\"items[0]\"), and keyed lookup (\\`items[_key==\"x\"]\\`). ` +\n `Recursive descent (\"..\") and GROQ filter expressions are not supported.`,\n );\n }\n const key = match[1] as string;\n const selectorRaw = match[2];\n if (selectorRaw === undefined) {\n segments.push({ key });\n continue;\n }\n const indexMatch = /^(\\d+)$/.exec(selectorRaw);\n if (indexMatch) {\n segments.push({ key, selector: { kind: \"index\", index: Number(indexMatch[1]) } });\n continue;\n }\n const keyedMatch = /^([A-Za-z_$][\\w$]*)\\s*==\\s*\"([^\"]*)\"$/.exec(selectorRaw);\n if (keyedMatch) {\n segments.push({\n key,\n selector: { kind: \"keyed\", field: keyedMatch[1] as string, value: keyedMatch[2] as string },\n });\n continue;\n }\n throw fakeError(\n `Unsupported array selector \"[${selectorRaw}]\" in patch path \"${path}\". Supported selectors ` +\n `are a numeric index (e.g. \"[0]\") and a single keyed match (e.g. \\`[_key==\"abc\"]\\`). ` +\n `Multi-key matches and arbitrary GROQ filters are not supported.`,\n );\n }\n return segments;\n}\n\nfunction resolveSelector(array: unknown[], selector: NonNullable<PathSegment[\"selector\"]>): number {\n if (selector.kind === \"index\") return selector.index;\n const idx = array.findIndex(\n (entry) =>\n typeof entry === \"object\" &&\n entry !== null &&\n (entry as Record<string, unknown>)[selector.field] === selector.value,\n );\n if (idx === -1) {\n throw fakeError(\n `Patch path matched no array entry where ${selector.field}==\"${selector.value}\". The array has ` +\n `${array.length} item(s); check the key exists (and that you're patching the right document).`,\n );\n }\n return idx;\n}\n\nfunction getAtPath(doc: SanityDocument, path: string): unknown {\n const segments = parsePath(path);\n let cursor: unknown = doc;\n for (const seg of segments) {\n if (cursor === undefined || cursor === null) return undefined;\n cursor = (cursor as Record<string, unknown>)[seg.key];\n if (seg.selector !== undefined) {\n if (!Array.isArray(cursor)) return undefined;\n const idx = resolveSelector(cursor, seg.selector);\n cursor = cursor[idx];\n }\n }\n return cursor;\n}\n\nfunction setAtPath(doc: SanityDocument, path: string, value: unknown): void {\n const segments = parsePath(path);\n if (segments.length === 0)\n throw fakeError(`Empty patch path — a set/setIfMissing op needs a target field.`);\n let cursor: Record<string, unknown> = doc as unknown as Record<string, unknown>;\n for (let i = 0; i < segments.length - 1; i++) {\n const seg = segments[i] as PathSegment;\n let next = cursor[seg.key];\n if (next === undefined) {\n next = seg.selector?.kind === \"index\" || seg.selector?.kind === \"keyed\" ? [] : {};\n cursor[seg.key] = next;\n }\n if (seg.selector !== undefined) {\n if (!Array.isArray(next)) {\n throw fakeError(\n `Patch path \"${path}\" applies an array selector to field \"${seg.key}\", but that field ` +\n `holds ${describeType(next)}, not an array.`,\n );\n }\n const idx = resolveSelector(next, seg.selector);\n cursor = next[idx] as Record<string, unknown>;\n } else {\n cursor = next as Record<string, unknown>;\n }\n }\n const last = segments[segments.length - 1] as PathSegment;\n if (last.selector !== undefined) {\n const arr = cursor[last.key];\n if (!Array.isArray(arr)) {\n throw fakeError(\n `Patch path \"${path}\" applies an array selector to field \"${last.key}\", but that field ` +\n `holds ${describeType(arr)}, not an array.`,\n );\n }\n const idx = resolveSelector(arr, last.selector);\n arr[idx] = value;\n } else {\n cursor[last.key] = value;\n }\n}\n\nfunction unsetAtPath(doc: SanityDocument, path: string): void {\n const segments = parsePath(path);\n if (segments.length === 0) return;\n let cursor: Record<string, unknown> = doc as unknown as Record<string, unknown>;\n for (let i = 0; i < segments.length - 1; i++) {\n const seg = segments[i] as PathSegment;\n let next = cursor[seg.key];\n if (next === undefined || next === null) return;\n if (seg.selector !== undefined) {\n if (!Array.isArray(next)) return;\n const idx = resolveSelector(next, seg.selector);\n next = next[idx];\n }\n cursor = next as Record<string, unknown>;\n }\n const last = segments[segments.length - 1] as PathSegment;\n if (last.selector !== undefined) {\n const arr = cursor[last.key];\n if (!Array.isArray(arr)) return;\n const idx = resolveSelector(arr, last.selector);\n arr.splice(idx, 1);\n } else {\n delete cursor[last.key];\n }\n}\n\nfunction insertIntoArray(\n doc: SanityDocument,\n position: \"before\" | \"after\" | \"replace\",\n anchor: string,\n items: unknown[],\n): void {\n // anchor is a path that must terminate at an array entry (selector required).\n // For \"replace\", we replace the entry; for \"before\"/\"after\", we insert\n // adjacent to it.\n const segments = parsePath(anchor);\n if (segments.length === 0)\n throw fakeError(`Empty insert anchor — insert() needs an array path to anchor against.`);\n const last = segments[segments.length - 1] as PathSegment;\n if (last.selector === undefined) {\n throw fakeError(\n `Insert anchor \"${anchor}\" must end with an array selector (an index like \"[0]\" or a keyed ` +\n `match like \\`[_key==\"x\"]\\`) so the fake knows where in the array to insert.`,\n );\n }\n\n let cursor: Record<string, unknown> = doc as unknown as Record<string, unknown>;\n for (let i = 0; i < segments.length - 1; i++) {\n const seg = segments[i] as PathSegment;\n const next = cursor[seg.key];\n if (next === undefined || next === null) {\n throw fakeError(\n `Insert anchor \"${anchor}\" traverses field \"${seg.key}\", which is missing on the document. ` +\n `setIfMissing the parent first, or anchor against an existing array.`,\n );\n }\n if (seg.selector !== undefined) {\n if (!Array.isArray(next)) {\n throw fakeError(\n `Insert anchor \"${anchor}\" applies an array selector to field \"${seg.key}\", but it holds ` +\n `${describeType(next)}, not an array.`,\n );\n }\n cursor = next[resolveSelector(next, seg.selector)] as Record<string, unknown>;\n } else {\n cursor = next as Record<string, unknown>;\n }\n }\n const arr = cursor[last.key];\n if (!Array.isArray(arr)) {\n throw fakeError(\n `Insert anchor \"${anchor}\" targets field \"${last.key}\", but it holds ${describeType(arr)}, ` +\n `not an array.`,\n );\n }\n const idx = resolveSelector(arr, last.selector);\n if (position === \"replace\") {\n arr.splice(idx, 1, ...items);\n } else if (position === \"before\") {\n arr.splice(idx, 0, ...items);\n } else {\n arr.splice(idx + 1, 0, ...items);\n }\n}\n","/**\n * Perspective layering — read-side filtering that mirrors Sanity's\n * `perspective` query/client option.\n *\n * The store always holds raw documents. Each fetch builds a *view* of\n * the store filtered by the active perspective before passing the\n * dataset to `groq-js`.\n *\n * Reference: https://www.sanity.io/docs/content-lake/perspectives\n */\n\nimport type { SanityDocument } from \"@sanity/types\";\n\n/**\n * `raw`, `published`, `drafts`, a release stack, or a single release name\n * (`\"rSpring\"`) — which the Content Lake reads as a one-release stack.\n */\nexport type Perspective = \"raw\" | \"published\" | \"drafts\" | (string & {}) | string[];\n\nconst DRAFTS_PREFIX = \"drafts.\";\nconst VERSIONS_PREFIX = \"versions.\";\nconst SYSTEM_PREFIX = \"_.\";\nconst RELEASE_DOC_PREFIX = \"_.releases.\";\n\n/** Strip `drafts.` / `versions.<name>.` prefix to recover the published id. */\nexport function publishedIdOf(id: string): string {\n if (id.startsWith(DRAFTS_PREFIX)) return id.slice(DRAFTS_PREFIX.length);\n if (id.startsWith(VERSIONS_PREFIX)) {\n // versions.<name>.<id> — split off the prefix + name\n const rest = id.slice(VERSIONS_PREFIX.length);\n const dot = rest.indexOf(\".\");\n if (dot === -1) return rest;\n return rest.slice(dot + 1);\n }\n return id;\n}\n\n/** Extract the release name from a `versions.<name>.<id>` id, else undefined. */\nexport function releaseNameOf(id: string): string | undefined {\n if (!id.startsWith(VERSIONS_PREFIX)) return undefined;\n const rest = id.slice(VERSIONS_PREFIX.length);\n const dot = rest.indexOf(\".\");\n if (dot === -1) return undefined;\n return rest.slice(0, dot);\n}\n\nexport const isDraftId = (id: string): boolean => id.startsWith(DRAFTS_PREFIX);\nexport const isVersionId = (id: string): boolean => id.startsWith(VERSIONS_PREFIX);\nexport const isSystemId = (id: string): boolean => id.startsWith(SYSTEM_PREFIX);\nexport const isReleaseDocId = (id: string): boolean => id.startsWith(RELEASE_DOC_PREFIX);\n\n/**\n * Apply a perspective to a raw set of documents and return the view\n * groq-js should evaluate against.\n *\n * For non-raw perspectives, each output document for a non-system id\n * gets `_id` projected to its published id and `_originalId` carrying\n * the source id (matching the wire shape of the real Content Lake).\n */\nexport function applyPerspective(\n docs: readonly SanityDocument[],\n requested: Perspective,\n): SanityDocument[] {\n const perspective = normalizePerspective(requested);\n if (perspective === \"raw\") return [...docs];\n\n if (perspective === \"published\") {\n return docs.filter((d) => !isDraftId(d._id) && !isVersionId(d._id) && !isSystemId(d._id));\n }\n\n if (perspective === \"drafts\") {\n return mergeStack(docs, [], { includeDrafts: true, includeReleaseDocs: false });\n }\n\n // string[] — release stack with optional \"drafts\" entry.\n const stack: string[] = [];\n let includeDrafts = false;\n for (const entry of perspective) {\n if (entry === \"drafts\") {\n includeDrafts = true;\n } else {\n stack.push(entry);\n }\n }\n return mergeStack(docs, stack, { includeDrafts, includeReleaseDocs: true });\n}\n\n/**\n * The perspective as the Content Lake receives it: `previewDrafts` is the\n * deprecated name of `drafts`, and any other string is a comma-joined\n * stack (`\"rA\"`, `\"drafts,rA\"`), the form `@sanity/client` sends.\n */\nfunction normalizePerspective(perspective: Perspective): \"raw\" | \"published\" | \"drafts\" | string[] {\n if (Array.isArray(perspective)) return perspective;\n switch (perspective) {\n case \"raw\":\n return \"raw\";\n case \"published\":\n return \"published\";\n case \"drafts\":\n case \"previewDrafts\":\n return \"drafts\";\n default:\n return perspective.split(\",\");\n }\n}\n\ninterface MergeOptions {\n includeDrafts: boolean;\n includeReleaseDocs: boolean;\n}\n\n/**\n * Walk the raw docs once, group by published id, then for each group\n * pick the highest-priority source according to `releaseStack` and\n * `includeDrafts`. Project the chosen doc to the published id.\n */\nfunction mergeStack(\n docs: readonly SanityDocument[],\n releaseStack: readonly string[],\n options: MergeOptions,\n): SanityDocument[] {\n // Group docs by published id. Track which sources exist.\n type Group = {\n published?: SanityDocument;\n draft?: SanityDocument;\n versions: Map<string, SanityDocument>;\n };\n const groups = new Map<string, Group>();\n const systemReleases: SanityDocument[] = [];\n\n for (const doc of docs) {\n if (isReleaseDocId(doc._id)) {\n if (options.includeReleaseDocs) systemReleases.push(doc);\n continue;\n }\n if (isSystemId(doc._id)) continue; // other system docs are hidden\n const pubId = publishedIdOf(doc._id);\n let group = groups.get(pubId);\n if (group === undefined) {\n group = { versions: new Map() };\n groups.set(pubId, group);\n }\n if (isDraftId(doc._id)) {\n group.draft = doc;\n } else if (isVersionId(doc._id)) {\n const releaseName = releaseNameOf(doc._id);\n if (releaseName !== undefined) group.versions.set(releaseName, doc);\n } else {\n group.published = doc;\n }\n }\n\n const out: SanityDocument[] = [];\n for (const [pubId, group] of groups) {\n // Walk the release stack first, then drafts (if enabled, in its\n // slot — here we model \"drafts after the stack\" matching the\n // typical `[r1, r2, \"drafts\"]` shape), then fall through to\n // published.\n let chosen: SanityDocument | undefined;\n for (const releaseName of releaseStack) {\n const v = group.versions.get(releaseName);\n if (v !== undefined) {\n // version.unpublish stub: hide the doc entirely\n if (isUnpublishStub(v)) {\n chosen = undefined;\n break;\n }\n chosen = v;\n break;\n }\n }\n if (chosen === undefined && options.includeDrafts && group.draft !== undefined) {\n chosen = group.draft;\n }\n if (chosen === undefined && group.published !== undefined) {\n // skip if any release in the stack carried an unpublish stub\n const blocked = releaseStack.some((name) => {\n const v = group.versions.get(name);\n return v !== undefined && isUnpublishStub(v);\n });\n if (!blocked) chosen = group.published;\n }\n if (chosen !== undefined) {\n out.push(projectToPublishedId(chosen, pubId));\n }\n }\n\n return [...out, ...systemReleases];\n}\n\nfunction isUnpublishStub(doc: SanityDocument): boolean {\n const sys = (doc as { _system?: { delete?: boolean } })._system;\n return sys?.delete === true;\n}\n\nfunction projectToPublishedId(doc: SanityDocument, pubId: string): SanityDocument {\n if (doc._id === pubId) return doc;\n return { ...doc, _id: pubId, _originalId: doc._id } as SanityDocument;\n}\n\n/**\n * `getDocument(id)` resolution under a perspective. Returns the chosen\n * source's *raw* doc (not projected), so the caller can decide whether\n * to expose `_originalId` etc. For perspective=\"raw\", this is just\n * `docs.find(d => d._id === id)`.\n */\nexport function resolveDocument(\n docs: readonly SanityDocument[],\n id: string,\n perspective: Perspective,\n): SanityDocument | undefined {\n if (perspective === \"raw\") {\n return docs.find((d) => d._id === id);\n }\n // Match the rules of applyPerspective: resolve to the same chosen\n // doc but key by the requested id (which the caller treated as a\n // published id under non-raw perspectives).\n const view = applyPerspective(docs, perspective);\n return view.find((d) => d._id === id);\n}\n","import type { SanityDocument, SanityDocumentLike } from \"@sanity/types\";\nimport { fakeError } from \"./errors.ts\";\n\n/**\n * In-memory document store backing a TestClient. Closure-scoped — created\n * fresh per test, no global state.\n *\n * The shape mirrors what `groq-js` evaluates against: an array of\n * documents. Internally we keep a Map by `_id` for O(1) lookup, and\n * project to an array on each query.\n */\nexport interface TestStore {\n /** Replace the entire dataset with the given documents. Existing docs are dropped. */\n seed: (documents: SanityDocumentLike[]) => void;\n /** Add documents without dropping existing ones. Conflicts (same _id) overwrite. */\n add: (documents: SanityDocumentLike[]) => void;\n /** Remove all documents. */\n reset: () => void;\n /** All documents as an array, in insertion order. */\n all: () => SanityDocument[];\n /** Get one document by _id, or undefined. */\n get: (id: string) => SanityDocument | undefined;\n /** Insert a fully-formed document by _id. Replaces if it already exists. */\n put: (doc: SanityDocument) => void;\n /** Remove a document by _id. Returns the removed doc or undefined. */\n remove: (id: string) => SanityDocument | undefined;\n /** Snapshot of all docs, useful for assertions. */\n snapshot: () => readonly SanityDocument[];\n}\n\n/**\n * Create a fresh, isolated store. The returned object closes over a private\n * Map; two stores created from two calls do not see each other.\n */\nexport function createTestStore(initial?: SanityDocumentLike[]): TestStore {\n const docs = new Map<string, SanityDocument>();\n\n const promote = (doc: SanityDocumentLike): SanityDocument => {\n if (typeof doc._id !== \"string\" || doc._id.length === 0) {\n throw fakeError(\n `Cannot seed/store a document without a non-empty string \\`_id\\` (got ${JSON.stringify(doc._id)}). ` +\n `This fake never auto-generates ids — give every document an explicit _id. Document: ${JSON.stringify(doc)}`,\n );\n }\n if (typeof doc._type !== \"string\" || doc._type.length === 0) {\n throw fakeError(\n `Document \"${doc._id}\" is missing a non-empty string \\`_type\\` (got ${JSON.stringify(doc._type)}). ` +\n `Every Sanity document needs a _type.`,\n );\n }\n const now = new Date().toISOString();\n return {\n ...doc,\n _id: doc._id,\n _type: doc._type,\n _rev: doc._rev ?? cryptoRandomKey(),\n _createdAt: doc._createdAt ?? now,\n _updatedAt: doc._updatedAt ?? now,\n } as SanityDocument;\n };\n\n const seed = (documents: SanityDocumentLike[]) => {\n docs.clear();\n for (const d of documents) {\n const promoted = promote(d);\n docs.set(promoted._id, promoted);\n }\n };\n\n if (initial) seed(initial);\n\n return {\n seed,\n add: (documents) => {\n for (const d of documents) {\n const promoted = promote(d);\n docs.set(promoted._id, promoted);\n }\n },\n reset: () => docs.clear(),\n all: () => [...docs.values()],\n get: (id) => docs.get(id),\n put: (doc) => {\n docs.set(doc._id, doc);\n },\n remove: (id) => {\n const doc = docs.get(id);\n if (doc) docs.delete(id);\n return doc;\n },\n snapshot: () => [...docs.values()],\n };\n}\n\n/**\n * Generate a stable-ish random key without pulling in a dependency.\n * Use cases: synthesising `_rev` for newly-stored documents.\n */\nexport function cryptoRandomKey(length = 12): string {\n const bytes = new Uint8Array(length);\n // globalThis.crypto is available in Node 22+ and all modern browsers.\n globalThis.crypto.getRandomValues(bytes);\n return [...bytes]\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\")\n .slice(0, length);\n}\n","/**\n * Action handlers for `client.action()` — Content Releases, versions, and\n * document publish/unpublish — plus the `client.releases.*` and\n * `client.createVersion` / `client.unpublishVersion` helpers.\n *\n * Mirrors enough of the real `@sanity/client` action surface for tests\n * to exercise release-aware workflows. Transient states\n * (`scheduling`/`publishing`/`unscheduling`) are skipped — releases\n * transition synchronously between settled states.\n */\n\nimport type { SanityDocument, SanityDocumentLike } from \"@sanity/types\";\n\nimport { fakeError } from \"./errors.ts\";\nimport { PlatformHttpError, type PlatformRequest } from \"./resources.ts\";\nimport { mutationContexts, type MutationGuardImageContext } from \"./mutation-guard.ts\";\nimport { applyPatchOps, type PatchOp } from \"./patch.ts\";\nimport { publishedIdOf } from \"./perspective.ts\";\nimport type { TestStore } from \"./store.ts\";\nimport { cryptoRandomKey } from \"./store.ts\";\n\n/**\n * Document body accepted by `version.create` / `createVersion`. The id\n * is derived from `releaseId + publishedId` so callers may omit `_id`\n * here. `version.replace` requires `_id` since it identifies the doc\n * to replace.\n */\nexport interface DocumentBody {\n _id?: string;\n _type: string;\n [field: string]: unknown;\n}\n\n/** A version copied from a base: `_system.base` names the base and the revision copied. */\ntype VersionFromBase = SanityDocumentLike & {\n _system: { base: { id: string; rev: string | undefined } };\n};\n\nexport type ReleaseState = \"active\" | \"scheduled\" | \"published\" | \"archived\";\n\nexport type ReleaseType = \"asap\" | \"scheduled\" | \"undecided\";\n\nexport interface ReleaseMetadata {\n title?: string;\n description?: string;\n releaseType?: ReleaseType;\n intendedPublishAt?: string;\n}\n\nexport interface ReleaseDocument extends SanityDocument {\n _type: \"system.release\";\n name: string;\n state: ReleaseState;\n metadata: ReleaseMetadata;\n publishAt?: string;\n}\n\nexport type ReleaseAction =\n | {\n actionType: \"sanity.action.release.create\";\n releaseId: string;\n metadata?: Partial<ReleaseMetadata>;\n }\n | {\n actionType: \"sanity.action.release.edit\";\n releaseId: string;\n patch: { set?: Record<string, unknown>; unset?: string[] };\n }\n | { actionType: \"sanity.action.release.schedule\"; releaseId: string; publishAt: string }\n | { actionType: \"sanity.action.release.unschedule\"; releaseId: string }\n | { actionType: \"sanity.action.release.publish\"; releaseId: string }\n | { actionType: \"sanity.action.release.archive\"; releaseId: string }\n | { actionType: \"sanity.action.release.unarchive\"; releaseId: string }\n | { actionType: \"sanity.action.release.delete\"; releaseId: string };\n\nexport type VersionAction =\n | {\n actionType: \"sanity.action.document.version.create\";\n publishedId: string;\n document: DocumentBody;\n releaseId?: string; // inferred from document._id when omitted\n }\n | {\n /** Copy the document at `baseId` into `versionId`, as `@sanity/client` sends `createVersion({ baseId })`. */\n actionType: \"sanity.action.document.version.create\";\n publishedId: string;\n baseId: string;\n versionId: string;\n ifBaseRevisionId?: string;\n }\n | {\n actionType: \"sanity.action.document.version.replace\";\n document: SanityDocumentLike;\n }\n | {\n actionType: \"sanity.action.document.version.discard\";\n versionId: string;\n }\n | {\n actionType: \"sanity.action.document.version.unpublish\";\n versionId: string;\n publishedId: string;\n };\n\n/**\n * Draft↔published document actions — `sanity.action.document.publish`\n * promotes the draft onto the published id (and deletes the draft);\n * `sanity.action.document.unpublish` retracts the published document into a\n * draft (keeping an existing draft untouched) and deletes the published one.\n */\nexport type DocumentAction =\n | {\n actionType: \"sanity.action.document.publish\";\n draftId: string;\n publishedId: string;\n ifDraftRevisionId?: string;\n ifPublishedRevisionId?: string;\n }\n | {\n actionType: \"sanity.action.document.unpublish\";\n draftId: string;\n publishedId: string;\n };\n\nexport type Action = ReleaseAction | VersionAction | DocumentAction;\n\n/**\n * Client-layer interception for document actions. `gate` checks access on\n * each document a publish/unpublish or `version.create` writes, given its net\n * image across the batch: before (`undefined` when created) and after\n * (`undefined` when deleted). A rejection rolls back the whole batch. `guard` receives\n * the batch's net draft and published mutations after staging; a rejection\n * rolls back the whole batch. `request` is the actions request the batch was\n * sent on, which a refused precondition's error names.\n */\nexport interface DocumentActionHooks {\n request?: PlatformRequest;\n gate?: (\n prev: SanityDocument | undefined,\n next: SanityDocument | undefined,\n ) => void | Promise<void>;\n guard?: (contexts: readonly MutationGuardImageContext[]) => void | Promise<void>;\n}\n\nexport interface ActionResult {\n transactionId: string;\n}\n\nconst RELEASE_DOC_PREFIX = \"_.releases.\";\nconst VERSIONS_PREFIX = \"versions.\";\n\nconst releaseDocId = (releaseId: string): string => `${RELEASE_DOC_PREFIX}${releaseId}`;\nconst versionDocId = (releaseId: string, publishedId: string): string =>\n `${VERSIONS_PREFIX}${releaseId}.${publishedId}`;\n\n/** Parse `versions.<release>.<id>` → `{ releaseId, publishedId }`. Throws otherwise. */\nfunction parseVersionId(versionId: string): { releaseId: string; publishedId: string } {\n if (!versionId.startsWith(VERSIONS_PREFIX)) {\n throw fakeError(\n `\"${versionId}\" is not a version document id. Version ids look like ` +\n `\"versions.<releaseId>.<publishedId>\" (e.g. \"versions.summer-drop.product-1\").`,\n );\n }\n const rest = versionId.slice(VERSIONS_PREFIX.length);\n const dot = rest.indexOf(\".\");\n if (dot === -1) {\n throw fakeError(\n `\"${versionId}\" is not a version document id. Version ids look like ` +\n `\"versions.<releaseId>.<publishedId>\" (e.g. \"versions.summer-drop.product-1\").`,\n );\n }\n return { releaseId: rest.slice(0, dot), publishedId: rest.slice(dot + 1) };\n}\n\nfunction getRelease(store: TestStore, releaseId: string): ReleaseDocument {\n const doc = store.get(releaseDocId(releaseId)) as ReleaseDocument | undefined;\n if (doc === undefined) {\n throw fakeError(\n `Release \"${releaseId}\" not found. Create it first with ` +\n `client.releases.create({ releaseId: \"${releaseId}\" }) (or a ` +\n `\"sanity.action.release.create\" action) before editing, scheduling, or adding versions to it.`,\n );\n }\n return doc;\n}\n\nfunction maybeRelease(store: TestStore, releaseId: string): ReleaseDocument | undefined {\n return store.get(releaseDocId(releaseId)) as ReleaseDocument | undefined;\n}\n\nfunction nowIso(): string {\n return new Date().toISOString();\n}\n\nfunction promote(doc: SanityDocumentLike): SanityDocument {\n const now = nowIso();\n return {\n ...doc,\n _id: doc._id as string,\n _type: doc._type as string,\n _rev: cryptoRandomKey(),\n _createdAt: doc._createdAt ?? now,\n _updatedAt: now,\n } as SanityDocument;\n}\n\nfunction bumpRev(doc: SanityDocument, patch: Partial<SanityDocument>): SanityDocument {\n return {\n ...doc,\n ...patch,\n _rev: cryptoRandomKey(),\n _updatedAt: nowIso(),\n } as SanityDocument;\n}\n\nfunction requireActive(release: ReleaseDocument): void {\n if (release.state !== \"active\") {\n throw fakeError(\n `Release \"${release.name}\" is \"${release.state}\", but only \"active\" releases accept version ` +\n `writes (create / replace / discard / unpublish). Unschedule or unarchive it back to ` +\n `\"active\" first, or target a different release.`,\n );\n }\n}\n\n/**\n * Apply a single action to the store. Returns a synthetic transactionId.\n * Throws on any invariant violation — callers wrap multi-action arrays\n * with a snapshot/rollback to keep atomicity. {@link applyActions} checks\n * access and evaluates guard contexts after staging the complete batch.\n */\nexport async function applyAction(\n store: TestStore,\n action: Action,\n request: PlatformRequest = ACTIONS_REQUEST,\n): Promise<ActionResult> {\n switch (action.actionType) {\n case \"sanity.action.release.create\": {\n if (maybeRelease(store, action.releaseId) !== undefined) {\n throw fakeError(\n `Release \"${action.releaseId}\" already exists. release.create expects a new releaseId; ` +\n `use release.edit to change its metadata, or pick a different releaseId.`,\n );\n }\n const now = nowIso();\n const metadata: ReleaseMetadata = {\n releaseType: \"undecided\",\n ...action.metadata,\n };\n const doc: ReleaseDocument = {\n _id: releaseDocId(action.releaseId),\n _type: \"system.release\",\n _rev: cryptoRandomKey(),\n _createdAt: now,\n _updatedAt: now,\n name: action.releaseId,\n state: \"active\",\n metadata,\n };\n store.put(doc);\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.edit\": {\n const release = getRelease(store, action.releaseId);\n const patchOps: PatchOp[] = [];\n if (action.patch.set !== undefined) {\n patchOps.push({ kind: \"set\", props: action.patch.set });\n }\n if (action.patch.unset !== undefined) {\n patchOps.push({ kind: \"unset\", paths: action.patch.unset });\n }\n const patched = applyPatchOps(release, patchOps);\n store.put(bumpRev(patched, {}));\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.schedule\": {\n const release = getRelease(store, action.releaseId);\n if (release.state !== \"active\") {\n throw fakeError(\n `Cannot schedule release \"${action.releaseId}\": it is \"${release.state}\", and only ` +\n `\"active\" releases can be scheduled.`,\n );\n }\n store.put(\n bumpRev(release, {\n state: \"scheduled\",\n publishAt: action.publishAt,\n } as Partial<SanityDocument>),\n );\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.unschedule\": {\n const release = getRelease(store, action.releaseId);\n if (release.state !== \"scheduled\") {\n throw fakeError(\n `Cannot unschedule release \"${action.releaseId}\": it is \"${release.state}\", and only ` +\n `\"scheduled\" releases can be unscheduled.`,\n );\n }\n const next: ReleaseDocument = { ...release, state: \"active\" };\n delete (next as { publishAt?: string }).publishAt;\n store.put(bumpRev(next, {}));\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.publish\": {\n const release = getRelease(store, action.releaseId);\n if (release.state === \"published\") {\n // Idempotent.\n return { transactionId: cryptoRandomKey() };\n }\n if (release.state !== \"active\" && release.state !== \"scheduled\") {\n throw fakeError(\n `Cannot publish release \"${action.releaseId}\": it is \"${release.state}\", and only ` +\n `\"active\" or \"scheduled\" releases can be published.`,\n );\n }\n // Materialise versions onto published ids, then drop the versions.\n const prefix = `${VERSIONS_PREFIX}${action.releaseId}.`;\n const sources = store.all().filter((d) => d._id.startsWith(prefix));\n for (const v of sources) {\n const pubId = v._id.slice(prefix.length);\n const isUnpublishStub = (v as { _system?: { delete?: boolean } })._system?.delete === true;\n if (isUnpublishStub) {\n store.remove(pubId);\n } else {\n const body: SanityDocumentLike = {\n ...(v as SanityDocumentLike),\n _id: pubId,\n };\n // Strip transient fields we projected through the version.\n delete (body as { _originalId?: string })._originalId;\n delete (body as { _system?: unknown })._system;\n const promoted = promote(body);\n // Preserve _createdAt of the existing published doc if present.\n const existing = store.get(pubId);\n if (existing !== undefined) {\n promoted._createdAt = existing._createdAt;\n }\n store.put(promoted);\n }\n store.remove(v._id);\n }\n store.put(bumpRev(release, { state: \"published\" } as Partial<SanityDocument>));\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.archive\": {\n const release = getRelease(store, action.releaseId);\n if (release.state !== \"active\" && release.state !== \"scheduled\") {\n throw fakeError(\n `Cannot archive release \"${action.releaseId}\": it is \"${release.state}\", and only ` +\n `\"active\" or \"scheduled\" releases can be archived.`,\n );\n }\n store.put(bumpRev(release, { state: \"archived\" } as Partial<SanityDocument>));\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.unarchive\": {\n const release = getRelease(store, action.releaseId);\n if (release.state !== \"archived\") {\n throw fakeError(\n `Cannot unarchive release \"${action.releaseId}\": it is \"${release.state}\", and only ` +\n `\"archived\" releases can be unarchived.`,\n );\n }\n store.put(bumpRev(release, { state: \"active\" } as Partial<SanityDocument>));\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.delete\": {\n const release = getRelease(store, action.releaseId);\n if (release.state !== \"archived\" && release.state !== \"published\") {\n throw fakeError(\n `Cannot delete release \"${action.releaseId}\": it is \"${release.state}\", and only ` +\n `\"archived\" or \"published\" releases can be deleted. Archive it first ` +\n `(sanity.action.release.archive) or publish it.`,\n );\n }\n // Clean up any leftover version docs (archived releases keep them around).\n const prefix = `${VERSIONS_PREFIX}${action.releaseId}.`;\n for (const v of store.all()) {\n if (v._id.startsWith(prefix)) store.remove(v._id);\n }\n store.remove(release._id);\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.document.version.create\": {\n if (\"baseId\" in action) return createVersionFromBase(store, action, request);\n const releaseId =\n action.releaseId ??\n (action.document._id !== undefined\n ? parseVersionId(action.document._id as string).releaseId\n : undefined);\n if (releaseId === undefined) {\n throw fakeError(\n `version.create needs a release to attach to: pass \\`releaseId\\`, or a \\`document._id\\` ` +\n `shaped \"versions.<releaseId>.<publishedId>\" so the release can be inferred.`,\n );\n }\n const release = getRelease(store, releaseId);\n requireActive(release);\n const vid = versionDocId(releaseId, action.publishedId);\n const doc: SanityDocumentLike = {\n ...action.document,\n _id: vid,\n };\n const existing = store.get(vid);\n const promoted = promote(doc);\n if (existing !== undefined) {\n promoted._createdAt = existing._createdAt;\n }\n store.put(promoted);\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.document.version.replace\": {\n const id = action.document._id;\n if (typeof id !== \"string\") {\n throw fakeError(\n `version.replace needs \\`document._id\\` (a version id like ` +\n `\"versions.<releaseId>.<publishedId>\") to identify which version document to replace.`,\n );\n }\n const { releaseId } = parseVersionId(id);\n const release = getRelease(store, releaseId);\n requireActive(release);\n const existing = store.get(id);\n const promoted = promote(action.document);\n if (existing !== undefined) {\n promoted._createdAt = existing._createdAt;\n }\n store.put(promoted);\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.document.version.discard\": {\n const { releaseId } = parseVersionId(action.versionId);\n const release = getRelease(store, releaseId);\n requireActive(release);\n store.remove(action.versionId);\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.document.version.unpublish\": {\n const { releaseId } = parseVersionId(action.versionId);\n const release = getRelease(store, releaseId);\n requireActive(release);\n const doc: SanityDocumentLike = {\n _id: action.versionId,\n _type: \"stub\",\n _system: { delete: true },\n } as SanityDocumentLike;\n store.put(promote(doc));\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.document.publish\": {\n requireDraftOf(action);\n const draft = store.get(action.draftId);\n if (draft === undefined) {\n throw fakeError(\n `Cannot publish \"${action.publishedId}\": draft \"${action.draftId}\" not found. ` +\n `document.publish promotes an existing draft onto the published id — create the ` +\n `draft first (e.g. client.createOrReplace({ _id: \"${action.draftId}\", ... })).`,\n );\n }\n const published = store.get(action.publishedId);\n requireRevisionMatch(request, action.draftId, draft._rev, action.ifDraftRevisionId);\n requireRevisionMatch(\n request,\n action.publishedId,\n published?._rev,\n action.ifPublishedRevisionId,\n );\n const promoted = promote({ ...draft, _id: action.publishedId });\n if (published !== undefined) {\n promoted._createdAt = published._createdAt;\n }\n store.put(promoted);\n store.remove(action.draftId);\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.document.unpublish\": {\n requireDraftOf(action);\n const published = store.get(action.publishedId);\n if (published === undefined) {\n throw fakeError(\n `Cannot unpublish \"${action.publishedId}\": no published document with that _id exists. ` +\n `document.unpublish retracts a live published document into \"${action.draftId}\".`,\n );\n }\n // The lake keeps an existing draft untouched; only a missing one is\n // materialised from the published content.\n if (store.get(action.draftId) === undefined) {\n const draft = promote({ ...published, _id: action.draftId });\n draft._createdAt = published._createdAt;\n store.put(draft);\n }\n store.remove(action.publishedId);\n return { transactionId: cryptoRandomKey() };\n }\n }\n}\n\n/** Throw unless the action's `draftId` is the draft of its `publishedId`. */\nfunction requireDraftOf(action: {\n actionType: string;\n draftId: string;\n publishedId: string;\n}): void {\n if (publishedIdOf(action.draftId) === action.publishedId && action.draftId !== action.publishedId)\n return;\n throw fakeError(\n `${action.actionType}: draftId \"${action.draftId}\" is not the draft of publishedId ` +\n `\"${action.publishedId}\" — expected draftId \"drafts.${action.publishedId}\".`,\n );\n}\n\n/**\n * `version.create` with `baseId`: the platform copies the base document's\n * content, `_type` included, into `versionId`, which names a version of\n * `publishedId`, and records the base in `_system.base`. Only the write on\n * the version is access-checked: gradient `version_create.go` preloads the\n * base read-only, and the transaction store checks permissions on written\n * documents alone.\n */\nasync function createVersionFromBase(\n store: TestStore,\n action: Extract<VersionAction, { baseId: string }>,\n request: PlatformRequest,\n): Promise<ActionResult> {\n const { releaseId, publishedId } = parseVersionId(action.versionId);\n if (publishedId !== action.publishedId) {\n throw fakeError(\n `version.create: versionId \"${action.versionId}\" is not a version of publishedId ` +\n `\"${action.publishedId}\" (expected \"${versionDocId(releaseId, action.publishedId)}\").`,\n );\n }\n requireActive(getRelease(store, releaseId));\n const base = store.get(action.baseId);\n if (base === undefined) {\n throw fakeError(\n `Cannot create version \"${action.versionId}\": base document \"${action.baseId}\" not found. ` +\n `version.create with \\`baseId\\` copies an existing document; pass \\`document\\` instead ` +\n `when there is nothing to copy.`,\n );\n }\n requireRevisionMatch(request, action.baseId, base._rev, action.ifBaseRevisionId);\n // A new revision and timestamps: the version is a new document unless it replaces one.\n const existing = store.get(action.versionId);\n const copy: VersionFromBase = {\n ...base,\n _id: action.versionId,\n _createdAt: existing?._createdAt ?? nowIso(),\n _system: { base: { id: base._id, rev: base._rev } },\n };\n const promoted = promote(copy);\n store.put(promoted);\n return { transactionId: cryptoRandomKey() };\n}\n\n/** The request an action error names when its caller gives none. */\nconst ACTIONS_REQUEST: PlatformRequest = {\n method: \"POST\",\n path: \"/data/actions\",\n url: \"/data/actions\",\n};\n\n/**\n * Enforce an `ifDraftRevisionId` / `ifPublishedRevisionId` / `ifBaseRevisionId`\n * precondition when given. A mismatch is the Content Lake's 409\n * `documentRevisionIDDoesNotMatchError`, and a missing document its 404\n * `documentNotFoundError` (gradient `pkg/documents/actions/publish.go`\n * `publishExecute`, `pkg/documents/errors.go`, `pkg/documents/mutation/patch.go`).\n */\nfunction requireRevisionMatch(\n request: PlatformRequest,\n documentId: string,\n actualRev: string | undefined,\n expectedRev: string | undefined,\n): void {\n if (expectedRev === undefined || actualRev === expectedRev) return;\n if (actualRev === undefined) {\n throw new PlatformHttpError({\n ...request,\n statusCode: 404,\n statusMessage: \"Not Found\",\n body: {\n error: {\n description: `The document with the ID ${JSON.stringify(documentId)} was not found`,\n id: documentId,\n type: \"documentNotFoundError\",\n },\n },\n });\n }\n throw new PlatformHttpError({\n ...request,\n statusCode: 409,\n statusMessage: \"Conflict\",\n body: {\n error: {\n currentRevisionID: actualRev,\n description:\n `Document ${JSON.stringify(documentId)} has unexpected revision ID ` +\n `(${JSON.stringify(actualRev)}), expected ${JSON.stringify(expectedRev)}`,\n expectedRevisionID: expectedRev,\n type: \"documentRevisionIDDoesNotMatchError\",\n },\n },\n });\n}\n\n/**\n * Apply an action or a list of actions atomically. On any failure all\n * mutations within the batch are rolled back to the pre-batch snapshot.\n * `hooks` intercepts document publish/unpublish actions\n * ({@link DocumentActionHooks}) and evaluates their net changes as one batch.\n */\nexport async function applyActions(\n store: TestStore,\n actions: Action | Action[],\n hooks?: DocumentActionHooks,\n): Promise<ActionResult> {\n const list = Array.isArray(actions) ? actions : [actions];\n const snapshot = new Map<string, SanityDocument | undefined>();\n const guardedIds = new Set<string>();\n // Documents whose net change `hooks.gate` checks, in check order.\n const gatedIds = new Set<string>();\n const remember = (id: string) => {\n if (!snapshot.has(id)) snapshot.set(id, store.get(id));\n };\n\n try {\n let last: ActionResult = { transactionId: cryptoRandomKey() };\n for (const a of list) {\n // Pre-record everything we might touch so rollback is precise.\n switch (a.actionType) {\n case \"sanity.action.release.create\":\n case \"sanity.action.release.edit\":\n case \"sanity.action.release.schedule\":\n case \"sanity.action.release.unschedule\":\n case \"sanity.action.release.archive\":\n case \"sanity.action.release.unarchive\":\n remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);\n break;\n case \"sanity.action.release.delete\": {\n remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);\n // Deleting a release also removes the version documents it keeps.\n const prefix = `${VERSIONS_PREFIX}${a.releaseId}.`;\n for (const d of store.all()) {\n if (d._id.startsWith(prefix)) remember(d._id);\n }\n break;\n }\n case \"sanity.action.release.publish\": {\n remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);\n const prefix = `${VERSIONS_PREFIX}${a.releaseId}.`;\n for (const d of store.all()) {\n if (d._id.startsWith(prefix)) {\n remember(d._id);\n remember(d._id.slice(prefix.length));\n }\n }\n break;\n }\n case \"sanity.action.document.version.create\": {\n if (\"baseId\" in a) {\n remember(a.versionId);\n gatedIds.add(a.versionId);\n break;\n }\n // version.create derives its id from releaseId + publishedId,\n // so we don't rely on document._id being set.\n const releaseId =\n a.releaseId ??\n (typeof a.document._id === \"string\"\n ? parseVersionId(a.document._id).releaseId\n : undefined);\n if (releaseId !== undefined) {\n remember(versionDocId(releaseId, a.publishedId));\n gatedIds.add(versionDocId(releaseId, a.publishedId));\n }\n break;\n }\n case \"sanity.action.document.version.replace\":\n if (typeof a.document._id === \"string\") remember(a.document._id);\n break;\n case \"sanity.action.document.version.discard\":\n case \"sanity.action.document.version.unpublish\":\n remember(a.versionId);\n break;\n case \"sanity.action.document.publish\":\n remember(a.draftId);\n remember(a.publishedId);\n // Preserve the lake-facing order: published write, then draft delete.\n guardedIds.add(a.publishedId);\n guardedIds.add(a.draftId);\n gatedIds.add(a.publishedId);\n gatedIds.add(a.draftId);\n break;\n case \"sanity.action.document.unpublish\":\n remember(a.draftId);\n remember(a.publishedId);\n // Preserve the lake-facing order: draft create, then published delete.\n guardedIds.add(a.draftId);\n guardedIds.add(a.publishedId);\n gatedIds.add(a.publishedId);\n gatedIds.add(a.draftId);\n break;\n }\n last = await applyAction(store, a, hooks?.request);\n }\n // Like the Content Lake, check each document's net change once the whole\n // batch is staged: a draft unpublished and published again is never written.\n for (const id of gatedIds) {\n const before = snapshot.get(id);\n const after = store.get(id);\n if (before !== after) await hooks?.gate?.(before, after);\n }\n const contexts = mutationContexts(\n [...guardedIds].map((id) => ({ before: snapshot.get(id), after: store.get(id) })),\n );\n await hooks?.guard?.(contexts);\n return last;\n } catch (err) {\n for (const [id, doc] of snapshot) {\n if (doc === undefined) {\n store.remove(id);\n } else {\n store.put(doc);\n }\n }\n throw err;\n }\n}\n","import type { SanityDocument, SanityDocumentLike } from \"@sanity/types\";\nimport type { PatchHandle, PatchOp } from \"./patch.ts\";\n\n/**\n * A transaction collects mutations and applies them atomically at commit\n * time. Mutations are applied in declaration order; if any one fails, the\n * whole transaction throws and no writes land in the store.\n *\n * Subset of `@sanity/client`'s Transaction. Same shape, narrower surface.\n */\nexport type TxnMutation =\n | { kind: \"create\"; doc: SanityDocumentLike }\n | { kind: \"createOrReplace\"; doc: SanityDocumentLike }\n | { kind: \"createIfNotExists\"; doc: SanityDocumentLike }\n | { kind: \"delete\"; id: string }\n | { kind: \"patch\"; documentId: string; ops: PatchOp[]; ifRev?: string };\n\nexport interface TransactionHandle {\n create: (doc: SanityDocumentLike) => TransactionHandle;\n createOrReplace: (doc: SanityDocumentLike) => TransactionHandle;\n createIfNotExists: (doc: SanityDocumentLike) => TransactionHandle;\n delete: (id: string) => TransactionHandle;\n /** Accept either a Patch builder (drained at commit) or an inline doc id + ops. */\n patch: (\n documentIdOrPatch: string | PatchHandle,\n ops?: PatchOp[],\n ifRev?: string,\n ) => TransactionHandle;\n commit: (options?: TxnCommitOptions) => Promise<TxnCommitResult>;\n /** Test-only introspection — not on real Transaction. */\n toJSON: () => TxnMutation[];\n}\n\nexport interface TxnCommitOptions {\n /**\n * Shape the resolved value (mirrors `@sanity/client`):\n * - default (both unset) → {@link MultipleMutationResult}\n * - `returnDocuments: true` → the resulting document(s)\n * - `returnFirst: true` with `returnDocuments` → a single document\n * - `returnDocuments: false` → an id-shaped mutation result\n */\n returnDocuments?: boolean;\n returnFirst?: boolean;\n /** Override the synthetic transaction id (mirrors `client.transaction(undefined, { transactionId })`). */\n transactionId?: string;\n /**\n * Request tag, recorded on the mutation log entry for assertions.\n * `null` mirrors `@sanity/client`'s explicit-suppression form and is\n * recorded as-is. No other behavior is simulated.\n */\n tag?: string | null;\n}\n\n/** The operation recorded per affected document, matching `@sanity/client`. */\nexport type MutationOperation = \"create\" | \"update\" | \"delete\" | \"none\";\n\n/** Result of a mutation when `returnFirst` resolves true and ids are requested. */\nexport interface SingleMutationResult {\n transactionId: string;\n documentId: string;\n results: { id: string; operation: MutationOperation }[];\n}\n\n/** Result of a mutation when ids are requested for all affected documents. */\nexport interface MultipleMutationResult {\n transactionId: string;\n documentIds: string[];\n results: { id: string; operation: MutationOperation }[];\n}\n\nexport type TxnCommitResult =\n | MultipleMutationResult\n | SingleMutationResult\n | SanityDocument\n | SanityDocument[];\n\nexport interface TxnExecutor {\n apply: (mutations: TxnMutation[], options?: TxnCommitOptions) => Promise<TxnCommitResult>;\n}\n\nexport function createTransaction(executor: TxnExecutor): TransactionHandle {\n const mutations: TxnMutation[] = [];\n\n const handle: TransactionHandle = {\n create: (doc) => {\n mutations.push({ kind: \"create\", doc });\n return handle;\n },\n createOrReplace: (doc) => {\n mutations.push({ kind: \"createOrReplace\", doc });\n return handle;\n },\n createIfNotExists: (doc) => {\n mutations.push({ kind: \"createIfNotExists\", doc });\n return handle;\n },\n delete: (id) => {\n mutations.push({ kind: \"delete\", id });\n return handle;\n },\n patch: (documentIdOrPatch, ops, ifRev) => {\n if (typeof documentIdOrPatch === \"string\") {\n mutations.push({\n kind: \"patch\",\n documentId: documentIdOrPatch,\n ops: ops ?? [],\n ...(ifRev !== undefined ? { ifRev } : {}),\n });\n } else {\n const json = documentIdOrPatch.toJSON();\n mutations.push({\n kind: \"patch\",\n documentId: json.id,\n ops: json.ops,\n ...(json.ifRev !== undefined ? { ifRev: json.ifRev } : {}),\n });\n }\n return handle;\n },\n commit: (options) => executor.apply(mutations, options),\n toJSON: () => [...mutations],\n };\n\n return handle;\n}\n","/**\n * The URL `@sanity/client` requests, built the way it builds it, so that a\n * fake error's `response.url` is the URL the real client's error carries\n * (`url: res.url || reqUrl`, @sanity/client 8.16.0 `src/http/errors.ts:47-54`).\n *\n * - Base: `<protocol>://<projectId>.<host>/v<apiVersion>` on the project\n * host, `<apiHost>/v<apiVersion>` otherwise; the CDN base swaps in\n * `apicdn.sanity.io` for the default API host (`src/config.ts:258-267`).\n * - Path: `<base>/<uri without its leading slash>` (`src/data/dataMethods.ts`\n * `_getUrl`).\n * - Query: the request's `query` object appended after the URL's own query\n * string with `&`, or after `?`, skipping `undefined` values (get-it 9\n * `buildFetchArgs`, the transport of @sanity/client 8).\n */\n\nimport { fakeError } from \"./errors.ts\";\n\n/** `@sanity/client`'s default `apiHost`. */\nexport const DEFAULT_API_HOST = \"https://api.sanity.io\";\n\n/** `@sanity/client`'s default `apiVersion`, which it warns about. */\nexport const DEFAULT_API_VERSION = \"1\";\n\nconst DEFAULT_CDN_HOST = \"apicdn.sanity.io\";\n\n/**\n * Encoded GROQ query strings this long or longer are sent as a `POST` body\n * instead of in the URL (`getQuerySizeLimit`).\n */\nexport const QUERY_SIZE_LIMIT = 11264;\n\n/** A request's `query` object, in the key order `@sanity/client` builds it. */\nexport type QueryEntries = readonly (readonly [string, string | number | boolean | undefined])[];\n\n/**\n * `requestTagPrefix` as `@sanity/client` keeps it (8.17.0 `src/config.ts:239-244`):\n * unset when empty, else with its trailing dots dropped, so a prefix of only\n * dots stays `\"\"`. An empty prefix adds nothing to a tag\n * (`src/data/dataMethods.ts:1043-1046`).\n */\nexport function normalizeTagPrefix(prefix: string | undefined): string | undefined {\n return prefix ? prefix.replace(/\\.+$/, \"\") : undefined;\n}\n\n/**\n * `apiVersion` as `@sanity/client` keeps it: without a leading `v`, and\n * `1`, `X` or a `YYYY-MM-DD` date, else the error `createClient` throws.\n */\nexport function normalizeApiVersion(apiVersion: string): string {\n const version = `${apiVersion}`.replace(/^v/, \"\");\n if (version === \"1\" || version === \"X\") return version;\n const date = new Date(version);\n if (!/^\\d{4}-\\d{2}-\\d{2}$/.test(version) || !(date.getTime() > 0)) {\n throw fakeError(\n `Invalid API version string, expected \\`1\\` or date in format \\`YYYY-MM-DD\\` (got ${JSON.stringify(apiVersion)}), ` +\n \"the same rule @sanity/client applies to `apiVersion`.\",\n );\n }\n return version;\n}\n\n/** The API base URLs of a handle, as `@sanity/client`'s `config()` sets `url` and `cdnUrl`. */\nexport function apiBaseUrls(args: {\n apiHost: string;\n apiVersion: string;\n /** The project whose host the handle addresses, or `undefined` for the global host. */\n projectId: string | undefined;\n}): { url: string; cdnUrl: string } {\n const { apiHost, apiVersion, projectId } = args;\n if (projectId === undefined) {\n const url = `${apiHost}/v${apiVersion}`;\n return { url, cdnUrl: url };\n }\n const [protocol, host] = apiHost.split(\"://\", 2) as [string, string];\n const cdnHost = apiHost === DEFAULT_API_HOST ? DEFAULT_CDN_HOST : host;\n return {\n url: `${protocol}://${projectId}.${host}/v${apiVersion}`,\n cdnUrl: `${protocol}://${projectId}.${cdnHost}/v${apiVersion}`,\n };\n}\n\n/** `<base>/<uri>`, with `query` merged into the URL as the transport merges it: before a fragment. */\nexport function requestUrl(base: string, uri: string, query: QueryEntries = []): string {\n const url = `${base}/${uri.replace(/^\\//, \"\")}`;\n const params = new URLSearchParams();\n for (const [key, value] of query) {\n if (value !== undefined) params.append(key, `${value}`);\n }\n const search = params.toString();\n if (search === \"\") return url;\n const fragmentStart = url.indexOf(\"#\");\n const beforeFragment = fragmentStart === -1 ? url : url.slice(0, fragmentStart);\n const fragment = fragmentStart === -1 ? \"\" : url.slice(fragmentStart);\n return `${beforeFragment}${beforeFragment.includes(\"?\") ? \"&\" : \"?\"}${search}${fragment}`;\n}\n\n/**\n * The `?query=…&$param=…` string a GROQ query travels in, as\n * `encodeQueryString` builds it for `fetch()`.\n */\nexport function encodeGroqQuery(query: string, params: Record<string, unknown> = {}): string {\n const search = new URLSearchParams();\n search.append(\"query\", query);\n for (const [key, value] of Object.entries(params)) {\n if (value !== undefined) search.append(`$${key}`, JSON.stringify(value));\n }\n return `?${search}`;\n}\n","/**\n * Graceful-degradation layer for the parts of `@sanity/client` this fake\n * doesn't implement.\n *\n * The client object is a fixed surface. Without a guard, reaching for a\n * member the fake doesn't have (`client.listen`, `client.assets.upload`,\n * a typo) yields a bare `TypeError: x is not a function` — no hint that\n * it's an intentional gap in a *fake*, no pointer to a workaround. The\n * `Proxy` built here turns every such access into an actionable error,\n * and lets tests fill gaps via a stub registry.\n *\n * Resolution order for a property read on the wrapped client:\n * 1. Real implemented member → passed straight through.\n * 2. Thenable / inspection probes (`then`, symbols, …) → pass through\n * so `await client` and `console.log(client)` stay well-behaved.\n * 3. User-registered stub (`client.stub(name, fn)`) → returned.\n * 4. Known-but-unimplemented `@sanity/client` member → a guard that\n * throws a curated, workaround-bearing error on call or deeper access.\n * 5. Anything else (typo / unknown) → a guard that throws a\n * \"no such method\" error listing the supported surface.\n */\n\nconst ISSUES_URL = \"https://github.com/sanity-labs/client-fake-for-test/issues\";\n\n/** Members this fake actually implements — surfaced in error messages. */\nexport const SUPPORTED_SURFACE = [\n \"fetch\",\n \"getDocument\",\n \"getDocuments\",\n \"create\",\n \"createOrReplace\",\n \"createIfNotExists\",\n \"delete\",\n \"patch\",\n \"transaction\",\n \"mutate\",\n \"action\",\n \"releases\",\n \"createVersion\",\n \"unpublishVersion\",\n \"request (only with accessControl or a bound token)\",\n \"getDataUrl\",\n \"config\",\n \"withConfig\",\n] as const;\n\ninterface UnsupportedEntry {\n /** What the real member is. */\n what: string;\n /** What to do instead. */\n workaround: string;\n}\n\n/**\n * Curated map of real `@sanity/client` members the fake deliberately omits.\n * Keyed by the top-level property name; nested access (e.g.\n * `assets.upload`) is reported with the full dotted path.\n */\nexport const UNSUPPORTED_MEMBERS: Record<string, UnsupportedEntry> = {\n listen: {\n what: \"real-time listener (returns an RxJS Observable of mutation events)\",\n workaround:\n \"Test your subscription logic in isolation, or register a fake: \" +\n `client.stub(\"listen\", () => myFakeObservable).`,\n },\n observable: {\n what: \"the RxJS-flavoured mirror of the client API\",\n workaround:\n \"Use the Promise-based methods and wrap them with `from()` if you need an Observable.\",\n },\n clone: {\n what: \"clone the client with the same config\",\n workaround: \"Use `withConfig({})` — it returns a sibling handle into the same store.\",\n },\n getUrl: {\n what: \"build an absolute API URL for a path\",\n workaround: \"Hard-code the expected URL in your test, or stub it.\",\n },\n assets: {\n what: \"asset upload / metadata namespace\",\n workaround: \"Seed asset documents into the store directly with `seed()` / `add()`.\",\n },\n agent: {\n what: \"Agent Actions namespace (generate / transform / …)\",\n workaround: `Stub the call site: client.stub(\"agent\", myFakeAgent).`,\n },\n projects: {\n what: \"project-management namespace\",\n workaround: \"Out of scope for a data-plane fake — stub the call site.\",\n },\n users: {\n what: \"user-management namespace\",\n workaround: \"Out of scope for a data-plane fake — stub the call site.\",\n },\n live: {\n what: \"Live Content API namespace\",\n workaround: \"Out of scope — stub the call site or test the consumer separately.\",\n },\n mediaLibrary: {\n what: \"Media Library namespace\",\n workaround: \"Out of scope — seed documents directly or stub the call site.\",\n },\n request: {\n what: \"generic HTTP request hook — only wired up when the handle has an acting identity\",\n workaround:\n \"Create the client with `accessControl`, or bind a registered token \" +\n \"(`identities` + `token` / `withConfig({ token })`), to enable request() (it serves \" +\n \"/users/me, the ACL path, /projects/<projectId>, /projects/<projectId>/users/<ids>, \" +\n \"/projects/<projectId>/features, the History API, resource lookup and the Access API). \" +\n \"Or register a stub.\",\n },\n dryRun: {\n what: \"not a method — did you mean the `{ dryRun }` mutation option?\",\n workaround: \"Pass `{ dryRun: true }` as the second arg to create / mutate / delete.\",\n },\n};\n\n/**\n * Property names read by promise-resolution, serialization, and test\n * frameworks while probing an unknown object. These must resolve to\n * `undefined` (not a throwing guard) so `await client`,\n * `JSON.stringify(client)`, and matcher diffs stay well-behaved. Members\n * inherited from `Object.prototype` (`toString`, `valueOf`, …) already\n * pass through via the `in` check and don't need listing here.\n */\nconst PROBE_NAMES = new Set([\n \"then\",\n \"catch\",\n \"finally\",\n \"toJSON\",\n \"asymmetricMatch\",\n \"$$typeof\",\n \"nodeType\",\n \"_isMockFunction\",\n]);\n\n/** Build the multi-line error body shared by both guard flavours. */\nfunction guardMessage(path: string, detail: string, workaround: string): string {\n return [\n `[client-fake-for-test] \\`client.${path}\\` ${detail}`,\n \"\",\n \"This is an in-memory fake of @sanity/client. It implements:\",\n ` ${SUPPORTED_SURFACE.join(\", \")}.`,\n \"\",\n `→ ${workaround}`,\n \"\",\n `Register a stub to fill any gap: client.stub(${JSON.stringify(path.split(\".\")[0])}, handler)`,\n `If this should be supported, open an issue: ${ISSUES_URL}`,\n ].join(\"\\n\");\n}\n\n/**\n * A callable, infinitely-chainable guard. Reading any property returns a\n * deeper guard (so `client.assets.upload` reports the full path), and\n * calling at any depth throws the actionable error. `then` / symbols\n * resolve to `undefined` so `await` and inspection don't trip the guard.\n */\nfunction makeGuard(path: string, message: string): unknown {\n const thrower = () => {\n throw new Error(message);\n };\n return new Proxy(thrower, {\n apply: () => thrower(),\n get: (_t, prop) => {\n if (typeof prop === \"symbol\") return undefined;\n if (PROBE_NAMES.has(prop)) return undefined;\n // Re-derive the message for the deeper path so the error names the\n // exact member that was reached for.\n const childPath = `${path}.${String(prop)}`;\n const top = path.split(\".\")[0] as string;\n const entry = UNSUPPORTED_MEMBERS[top];\n const detail = entry ? `is not implemented (${entry.what}).` : \"is not implemented.\";\n const workaround = entry?.workaround ?? \"Register a stub or use a different test strategy.\";\n return makeGuard(childPath, guardMessage(childPath, detail, workaround));\n },\n });\n}\n\n/** Outcome of a recorded call, reported once a (possibly async) result is known. */\nexport type CallSettler = (outcome: { result?: unknown; error?: unknown }) => void;\n\nexport interface GuardConfig {\n /**\n * Resolve a registered stub for an unimplemented member, or `undefined`.\n * Checked only for members the client doesn't already implement, so\n * stubs fill gaps without shadowing core behaviour.\n */\n getStub: (name: string) => unknown;\n /**\n * Called synchronously when a recorded method is invoked, with the\n * method name and its arguments. Returns a settler the wrapper invokes\n * once the result (or rejection) is known. Omit to disable recording.\n */\n onCall?: (method: string, args: unknown[]) => CallSettler;\n /**\n * Top-level member names whose calls are recorded via `onCall`. A\n * recorded member that resolves to a namespace object (e.g. `releases`)\n * has each of its methods recorded as `name.method`.\n */\n recorded?: Set<string>;\n}\n\n/** Wrap a function so each call is reported to `onCall` (args now, outcome on settle). */\nfunction wrapRecorded(\n method: string,\n fn: (...args: unknown[]) => unknown,\n onCall: NonNullable<GuardConfig[\"onCall\"]>,\n): (...args: unknown[]) => unknown {\n return (...args: unknown[]) => {\n const settle = onCall(method, args);\n try {\n const out = fn(...args);\n if (\n out !== null &&\n typeof out === \"object\" &&\n typeof (out as PromiseLike<unknown>).then === \"function\"\n ) {\n return (out as Promise<unknown>).then(\n (result) => {\n settle({ result });\n return result;\n },\n (error: unknown) => {\n settle({ error });\n throw error;\n },\n );\n }\n settle({ result: out });\n return out;\n } catch (error) {\n settle({ error });\n throw error;\n }\n };\n}\n\n/** Wrap a namespace object so its method calls record as `prefix.method`. */\nfunction wrapNamespace(\n ns: Record<string, unknown>,\n prefix: string,\n onCall: NonNullable<GuardConfig[\"onCall\"]>,\n): Record<string, unknown> {\n return new Proxy(ns, {\n get(target, prop, receiver) {\n const value = Reflect.get(target, prop, receiver);\n if (typeof prop === \"string\" && typeof value === \"function\") {\n return wrapRecorded(`${prefix}.${prop}`, value as (...a: unknown[]) => unknown, onCall);\n }\n return value;\n },\n });\n}\n\n/**\n * Wrap a client object so unimplemented members fail informatively,\n * registered stubs are served, and recorded methods report their calls.\n *\n * @param client - the fully-built client object (plain literal).\n * @param config - stub resolver plus optional call-recording hooks.\n */\nexport function wrapWithGuards<T extends object>(client: T, config: GuardConfig): T {\n const { getStub, onCall, recorded } = config;\n return new Proxy(client, {\n get(target, prop, receiver) {\n if (typeof prop === \"symbol\") return Reflect.get(target, prop, receiver);\n if (prop in target) {\n const value = Reflect.get(target, prop, receiver);\n if (onCall !== undefined && recorded?.has(prop) === true) {\n if (typeof value === \"function\") {\n return wrapRecorded(prop, value as (...a: unknown[]) => unknown, onCall);\n }\n if (value !== null && typeof value === \"object\") {\n return wrapNamespace(value as Record<string, unknown>, prop, onCall);\n }\n }\n return value;\n }\n // Promise / serialization / framework probes on a non-thenable object.\n if (PROBE_NAMES.has(prop)) return undefined;\n\n const stub = getStub(prop);\n if (stub !== undefined) return stub;\n\n const entry = UNSUPPORTED_MEMBERS[prop];\n if (entry !== undefined) {\n return makeGuard(\n prop,\n guardMessage(prop, `is not implemented (${entry.what}).`, entry.workaround),\n );\n }\n return makeGuard(\n prop,\n guardMessage(\n prop,\n \"is not a method on this fake (unknown member or unimplemented @sanity/client API).\",\n \"Check for a typo, register a stub, or use a different test strategy.\",\n ),\n );\n },\n });\n}\n","import type { SanityDocument, SanityDocumentLike } from \"@sanity/types\";\nimport { evaluate, parse } from \"groq-js\";\n\nimport {\n canAccess,\n enforceWriteAccess,\n makeRequestImpl,\n projectUserOf,\n type DocumentValuePermission,\n type TestClientAccessControl,\n type TestClientIdentity,\n} from \"./access-control.ts\";\nimport {\n applyActions,\n type Action,\n type ActionResult,\n type DocumentBody,\n type ReleaseDocument,\n type ReleaseMetadata,\n} from \"./actions.ts\";\nimport { fakeError } from \"./errors.ts\";\nimport { createDocumentHistory, type DocumentHistory } from \"./history.ts\";\nimport {\n enforceMutationGuards,\n mutationContexts,\n type MutationGuard,\n type MutationGuardContext,\n type MutationGuardImageContext,\n} from \"./mutation-guard.ts\";\nimport { applyPatchOps, createPatch, type PatchHandle, type PatchOp } from \"./patch.ts\";\nimport { applyPerspective, isSystemId, resolveDocument, type Perspective } from \"./perspective.ts\";\nimport {\n capabilitiesOf,\n dataUrl,\n DEFAULT_WRITE,\n datasetParts,\n sessionHostMismatch,\n sessionNotFound,\n validateResource,\n type PlatformRequest,\n type TestClientResource,\n} from \"./resources.ts\";\nimport { createTestStore, cryptoRandomKey, type TestStore } from \"./store.ts\";\nimport {\n createTransaction,\n type MultipleMutationResult,\n type MutationOperation,\n type SingleMutationResult,\n type TransactionHandle,\n type TxnCommitOptions,\n type TxnCommitResult,\n type TxnMutation,\n} from \"./transaction.ts\";\nimport {\n apiBaseUrls,\n DEFAULT_API_HOST,\n DEFAULT_API_VERSION,\n encodeGroqQuery,\n normalizeApiVersion,\n normalizeTagPrefix,\n QUERY_SIZE_LIMIT,\n requestUrl,\n type QueryEntries,\n} from \"./request-url.ts\";\nimport { refusedToken } from \"./token-grants.ts\";\nimport { wrapWithGuards, type CallSettler } from \"./unsupported.ts\";\n\n/**\n * Data-plane subset of `@sanity/client`'s surface, with multi-dataset support.\n *\n * One `createTestClient()` call creates a registry of stores, one per\n * resource: a `(projectId, dataset)` pair, or a Media Library, Canvas or\n * dashboard named by `resource: { type, id }` exactly as `@sanity/client`\n * names it. The returned client points at one of them.\n * `withConfig({ projectId, dataset })` or `withConfig({ resource })` returns\n * a new handle into the same registry but pointing at a different store —\n * useful for workflows where definitions live in one dataset and instances\n * live in another.\n *\n * Two separate `createTestClient()` calls have separate registries, so\n * tests stay isolated from each other.\n *\n * Methods not on the real client:\n *\n * - `seed`, `add`, `reset`, `snapshot`, `store` — bulk-load helpers,\n * scoped to the current dataset.\n * - `simulateConcurrentEdit()` — force the next N revision-guarded writes\n * to a document to 409, for optimistic-locking retry tests.\n * - `datasets()` / `resources()` — list the stores known in this registry.\n *\n * Methods deliberately omitted:\n *\n * - Project / agent / asset / live / listen APIs.\n */\nexport interface FetchOptions {\n /** Override the client's configured perspective for this read. */\n perspective?: Perspective;\n /**\n * When `false`, resolve to a {@link RawQueryResponse} (`{ query, ms,\n * result }`) instead of the bare result — mirrors `@sanity/client`.\n * Defaults to `true`.\n */\n filterResponse?: boolean;\n /**\n * With `filterResponse: false`, set to `false` to omit `query` from the\n * raw response. Ignored when `filterResponse` is not `false`.\n */\n returnQuery?: boolean;\n /**\n * Request tag, recorded on the {@link CallRecord} for assertions.\n * `null` mirrors `@sanity/client`'s explicit-suppression form and is\n * recorded as-is. No other behavior is simulated.\n */\n tag?: string | null;\n /** Accepted for signature parity; not simulated. */\n signal?: AbortSignal;\n}\n\n/**\n * Shape returned by `fetch` when `filterResponse: false`. Matches\n * `@sanity/client`'s `RawQueryResponse`. `query` is omitted when the\n * caller also passes `returnQuery: false`.\n */\nexport interface RawQueryResponse<R> {\n query?: string;\n ms: number;\n result: R;\n}\n\n/**\n * A single mutation in the raw array form accepted by `mutate()` — the\n * same shape `@sanity/client` accepts. The inline `patch` form mirrors\n * the real client's keyed patch object.\n */\nexport type FakeMutation =\n | { create: SanityDocumentLike }\n | { createOrReplace: SanityDocumentLike }\n | { createIfNotExists: SanityDocumentLike }\n | { delete: { id: string } }\n | {\n patch: {\n id: string;\n ifRevisionID?: string;\n set?: Record<string, unknown>;\n setIfMissing?: Record<string, unknown>;\n unset?: string[];\n inc?: Record<string, number>;\n dec?: Record<string, number>;\n insert?: {\n before?: string;\n after?: string;\n replace?: string;\n items: unknown[];\n };\n };\n };\n\n/** Append-only record of one committed transaction (a single mutation is a 1-entry transaction). */\nexport interface MutationLogEntry {\n transactionId: string;\n projectId: string;\n dataset: string;\n /** The resource the transaction targeted, on a `resource`-configured handle. */\n resource?: TestClientResource;\n mutations: TxnMutation[];\n /** Request tag the commit carried, when one was passed (`null` = explicit suppression). */\n tag?: string | null;\n}\n\n/**\n * One recorded invocation of a client method — the spy layer behind\n * `client.calls()` / `client.callCount()`. `result` holds the resolved\n * value (awaited, for async methods); `error` holds the thrown/rejected\n * value. Builder-returning methods (`patch`, `transaction`) record the\n * builder as `result`; their eventual writes show up in the mutation log.\n */\nexport interface CallRecord {\n /** Method name; namespace methods are dotted, e.g. `releases.create`. */\n method: string;\n /** Arguments as passed (stored by reference, like `vi.fn`). */\n args: unknown[];\n /** Resolved/returned value, set once the call settles successfully. */\n result?: unknown;\n /** Thrown or rejected value, set if the call failed. */\n error?: unknown;\n /** Request tag from the call's options, when one was passed (`null` = explicit suppression). */\n tag?: string | null;\n projectId: string;\n dataset: string;\n /** The resource the call targeted, on a `resource`-configured handle. */\n resource?: TestClientResource;\n /**\n * The token of the handle that made the call, after `withConfig`\n * overrides; absent on a handle without one.\n */\n token?: string;\n /** The `apiVersion` of the handle that made the call, as its `config()` reports it. */\n apiVersion?: string;\n}\n\n/**\n * Where each recorded method carries its options bag, for {@link tagFromArgs}.\n * Positional (not \"last object arg\") because `fetch` params may legitimately\n * contain a `tag` key that is a GROQ param, not a request tag. Methods whose\n * tag only arrives at commit time (`patch`, `transaction`) surface it on the\n * {@link MutationLogEntry} instead.\n */\nconst OPTIONS_ARG_POSITION: Record<string, number> = {\n fetch: 2,\n getDocument: 1,\n getDocuments: 1,\n create: 1,\n createOrReplace: 1,\n createIfNotExists: 1,\n delete: 1,\n mutate: 1,\n action: 1,\n request: 0,\n};\n\n/** The request tag a call's options carried — `undefined` when absent. */\nfunction tagFromArgs(method: string, args: unknown[]): string | null | undefined {\n const position = OPTIONS_ARG_POSITION[method];\n if (position === undefined) return undefined;\n const options = args[position];\n if (typeof options !== \"object\" || options === null || !(\"tag\" in options)) return undefined;\n return (options as { tag?: string | null }).tag;\n}\n\n/** Client methods whose calls are recorded for `calls()` / `callCount()`. */\nconst RECORDED_METHODS = new Set([\n \"fetch\",\n \"getDocument\",\n \"getDocuments\",\n \"create\",\n \"createOrReplace\",\n \"createIfNotExists\",\n \"delete\",\n \"patch\",\n \"transaction\",\n \"mutate\",\n \"action\",\n \"releases\",\n \"createVersion\",\n \"unpublishVersion\",\n \"request\",\n]);\n\nexport interface TestClient {\n // --- query ---\n fetch: <T = unknown>(\n query: string,\n params?: Record<string, unknown>,\n options?: FetchOptions,\n ) => Promise<T>;\n getDocument: <T = SanityDocument>(id: string, options?: FetchOptions) => Promise<T | undefined>;\n getDocuments: <T = SanityDocument>(\n ids: string[],\n options?: FetchOptions,\n ) => Promise<(T | null)[]>;\n /**\n * Sanity-style HTTP request hook. Present iff the handle has an\n * acting identity — created with `accessControl`, or bound to a\n * registered token (`identities` + `token`). The engine's access\n * resolver pokes `/users/me` and the configured ACL path through\n * this — without it the engine throws on any verb that needs an\n * actor. Also serves the resource's history, ACL, lookup and Access API\n * endpoints, or the platform's refusal where the resource has none.\n */\n request?: <T>(opts: {\n url?: string;\n uri?: string;\n signal?: AbortSignal;\n tag?: string | null;\n }) => Promise<T>;\n\n /**\n * `@sanity/client`'s `getDataUrl(operation, path)`: `<resource\n * base>/<operation>/<path>` on a `resource`-configured handle,\n * `/data/<operation>/<dataset>/<path>` otherwise. Pass the result to\n * `request()` to reach e.g. the History API.\n */\n getDataUrl: (operation: string, path?: string) => string;\n\n // --- mutate ---\n create: <T extends SanityDocumentLike>(\n doc: T,\n options?: MutationOptions,\n ) => Promise<T & SystemFields>;\n createOrReplace: <T extends SanityDocumentLike>(\n doc: T,\n options?: MutationOptions,\n ) => Promise<T & SystemFields>;\n createIfNotExists: <T extends SanityDocumentLike>(\n doc: T,\n options?: MutationOptions,\n ) => Promise<T & SystemFields>;\n delete: (id: string, options?: MutationOptions) => Promise<{ _id: string } | null>;\n patch: (documentId: string) => PatchHandle;\n transaction: () => TransactionHandle;\n /**\n * Apply a batch of mutations in the raw array form (or a `patch()` /\n * `transaction()` builder) — mirrors `@sanity/client`'s `mutate()`.\n * Atomic: any failure rolls the whole batch back. The resolved shape\n * follows `options.returnDocuments` / `returnFirst` exactly like the\n * real client (default → {@link MultipleMutationResult}).\n */\n mutate: (\n operations: FakeMutation[] | PatchHandle | TransactionHandle,\n options?: TxnCommitOptions,\n ) => Promise<TxnCommitResult>;\n\n // --- actions / releases ---\n /**\n * Dispatch one or more release / version / document actions against the\n * store. Returns a synthetic `transactionId`. Multi-action arrays are\n * applied atomically — any failure rolls back the batch. The document\n * publish/unpublish actions run through the access gate and expose their\n * net draft/published mutations to `mutationGuard`; release/version actions\n * apply unhooked.\n */\n action: (action: Action | Action[], options?: ActionOptions) => Promise<ActionResult>;\n /** Helpers mirroring `@sanity/client`'s `client.releases.*` surface. */\n releases: ReleasesHandle;\n /**\n * Helper for `sanity.action.document.version.create`: writes `document`, or,\n * given `baseId` (as `@sanity/client` takes it), copies that document into\n * `versions.<releaseId>.<publishedId>`.\n */\n createVersion: (\n params:\n | { releaseId: string; publishedId: string; document: DocumentBody }\n | { releaseId: string; publishedId: string; baseId: string; ifBaseRevisionId?: string },\n ) => Promise<ActionResult>;\n /** Helper for `sanity.action.document.version.unpublish`. */\n unpublishVersion: (params: { releaseId: string; publishedId: string }) => Promise<ActionResult>;\n\n // --- config ---\n /**\n * Current `(projectId, dataset)` or `resource`, active perspective, and which API host\n * the handle models (`useProjectHostname`). `token` is present iff the\n * handle is bound to a registered identity.\n */\n config: () => {\n /**\n * Absent on a handle created global-hosted (`useProjectHostname: false`)\n * without a `projectId`, as in `@sanity/client`.\n */\n projectId?: string;\n dataset: string;\n /** Present iff the handle is `resource`-configured. */\n resource?: TestClientResource;\n perspective: Perspective;\n requestTagPrefix?: string;\n token?: string;\n useProjectHostname: boolean;\n /** The API host, `https://api.sanity.io` unless configured. */\n apiHost: string;\n /** The API version, without a leading `v`; `\"1\"` unless configured, as in `@sanity/client`. */\n apiVersion: string;\n /** Whether GET reads of the data API go to the API CDN; `true` unless configured `false`. */\n useCdn: boolean;\n };\n /**\n * Return a new client handle pointing at a different `(projectId, dataset)`\n * within the SAME backing registry. Either field can be omitted to keep\n * the current value. Two clients produced by `withConfig` see each\n * other's writes if they point at the same `(projectId, dataset)`.\n */\n withConfig: (\n config: Partial<{\n projectId: string;\n dataset: string;\n /**\n * Point the sibling at a resource, named as `@sanity/client`'s\n * `resource` config names it. While set, it decides the store (over\n * `projectId` / `dataset`), as in `@sanity/client`. Pass `undefined`\n * to drop it and address `(projectId, dataset)` again.\n */\n resource: TestClientResource | undefined;\n perspective: Perspective;\n /** Prefix composed with request tags and used alone for untagged requests; `undefined` unsets it. */\n requestTagPrefix: string | undefined;\n /**\n * Model the global API host (`false`) instead of the project host\n * (default `true`). A global-hosted sibling's `/users/me` serves the\n * identity's account-global record (`globalUser`, falling back to the\n * project record) — mirroring `@sanity/client`, where a non-project\n * config routes requests to `api.sanity.io`.\n */\n useProjectHostname: boolean;\n /** See {@link CreateTestClientOptions.apiHost}. */\n apiHost: string;\n /** See {@link CreateTestClientOptions.apiVersion}. */\n apiVersion: string;\n /** See {@link CreateTestClientOptions.useCdn}. */\n useCdn: boolean;\n /**\n * Rebind the acting identity (and its grants) on a sibling that shares\n * the same store — the test analogue of a token-scoped client. Lets two\n * actors write the same dataset, e.g. for `identity()`-based guards.\n * Mutually exclusive with `token`.\n */\n accessControl: TestClientAccessControl;\n /**\n * Bind the sibling to a token registered in the family's identity\n * registry (`createTestClient({ identities })` / `addIdentity`): its\n * `/users/me` serves that identity's record for the sibling's project\n * and host, its ACL path serves that identity's grants, and\n * reads/writes are gated through them — the token determines\n * identity, exactly like production. Unknown tokens throw. Mutually\n * exclusive with `accessControl`.\n */\n token: string;\n }>,\n ) => TestClient;\n\n // --- test helpers (not on real SanityClient) ---\n /** Replace the current dataset with the given documents. */\n seed: (documents: SanityDocumentLike[]) => void;\n /** Add documents to the current dataset without dropping existing ones. */\n add: (documents: SanityDocumentLike[]) => void;\n /** Drop all documents in the current dataset. */\n reset: () => void;\n /** Read-only snapshot of all documents in the current dataset. */\n snapshot: () => readonly SanityDocument[];\n /** Direct store access for the current dataset. */\n store: () => TestStore;\n /**\n * Simulate `count` concurrent edits to `id` in the current dataset: the\n * next `count` revision-guarded mutations (those passing `ifRevisionId`)\n * to that document throw a 409-shaped conflict, as if another client\n * committed between the caller's read and write. Mutations that don't\n * pass `ifRevisionId` are unaffected.\n *\n * Reach for this when the code under test reads a document's `_rev` and\n * commits with `ifRevisionId` internally — so you can't stage the race\n * by mutating the document first (the code would just re-read the new\n * revision and its commit would match). The counter is consumed one per\n * guarded commit, so `count` lets a test drive a recover-after-N-losses\n * path or exhaust a retry budget, deterministically and without relying\n * on promise interleaving.\n *\n * Note: this forces the conflict *outcome* — it short-circuits the\n * revision comparison rather than changing the stored `_rev`. The code\n * under test sees the identical 409 error a real lost race produces; the\n * stored revision is left untouched.\n */\n simulateConcurrentEdit: (id: string, count?: number) => void;\n /** List `(projectId, dataset)` pairs known in this registry. */\n datasets: () => { projectId: string; dataset: string }[];\n /** List every resource known in this registry — datasets as `{ type: \"dataset\", id: \"<projectId>.<dataset>\" }`. */\n resources: () => TestClientResource[];\n /**\n * Register a handler for a `@sanity/client` member the fake doesn't\n * implement (e.g. `listen`, `assets`). Accessing that member on the\n * client then returns the handler instead of throwing the\n * \"not implemented\" guard error. Shared across sibling `withConfig`\n * handles. Stubs fill gaps only — they don't shadow implemented methods.\n */\n stub: (name: string, handler: unknown) => void;\n /** The currently-registered stubs, keyed by member name. */\n stubs: () => Record<string, unknown>;\n /**\n * Register (or upsert) a token→identity entry in the family's shared\n * registry. Siblings already bound to `token` resolve lazily, so a\n * re-registration takes effect on them immediately. Registering an\n * identity does not bind it — pass `token` at `createTestClient` or\n * `withConfig({ token })` to act as it.\n */\n addIdentity: (token: string, identity: TestClientIdentity) => void;\n /** Snapshot of the registered token→identity entries. */\n identities: () => Record<string, TestClientIdentity>;\n /**\n * Revoke a registered token, as when it expires or is deleted: it stays\n * registered and still binds, but from the next request on every Content\n * Lake and platform request made with it (on any handle) rejects with the\n * platform's 401 `Session not found`, and nothing is written. Revocation\n * is permanent for the family; `addIdentity` does not restore it. Unknown\n * tokens throw.\n */\n revokeIdentity: (token: string) => void;\n /**\n * Every mutation committed through this client's registry, flattened\n * and in order — `create` / `createOrReplace` / `createIfNotExists` /\n * `delete` / `patch`, whether issued directly, via `transaction()`, or\n * via `mutate()`. The spy hook for \"assert exactly what writes my code\n * submitted\".\n */\n mutations: () => TxnMutation[];\n /**\n * Every committed transaction, in order. A direct mutation is recorded\n * as a one-mutation transaction. Each entry carries its synthetic\n * `transactionId` and the `(projectId, dataset)` it targeted.\n */\n transactions: () => MutationLogEntry[];\n /** Clear the recorded mutation / transaction log. */\n clearLog: () => void;\n /**\n * Every recorded method call, in order — the spy layer for \"was `fetch`\n * called, how many times, and with what params\". Records the\n * `@sanity/client`-surface methods (`fetch`, `getDocument`, `create`,\n * `patch`, `mutate`, `releases.*`, …), not the test-only helpers.\n * Pass a method name to filter (e.g. `calls(\"fetch\")`); namespace\n * methods are dotted (`calls(\"releases.create\")`). Shared across\n * `withConfig` siblings.\n */\n calls: (method?: string) => CallRecord[];\n /** Number of recorded calls, optionally filtered by method name. */\n callCount: (method?: string) => number;\n /** Clear the recorded call log. */\n clearCalls: () => void;\n}\n\nexport interface ReleasesHandle {\n create: (params: {\n releaseId: string;\n metadata?: Partial<ReleaseMetadata>;\n }) => Promise<ActionResult & { releaseId: string; metadata: ReleaseMetadata }>;\n edit: (params: {\n releaseId: string;\n patch: { set?: Record<string, unknown>; unset?: string[] };\n }) => Promise<ActionResult>;\n get: (params: { releaseId: string }) => Promise<ReleaseDocument | undefined>;\n fetchDocuments: (params: { releaseId: string }) => Promise<SanityDocument[]>;\n schedule: (params: { releaseId: string; publishAt: string }) => Promise<ActionResult>;\n unschedule: (params: { releaseId: string }) => Promise<ActionResult>;\n publish: (params: { releaseId: string }) => Promise<ActionResult>;\n archive: (params: { releaseId: string }) => Promise<ActionResult>;\n unarchive: (params: { releaseId: string }) => Promise<ActionResult>;\n delete: (params: { releaseId: string }) => Promise<ActionResult>;\n}\n\nexport interface MutationOptions {\n ifRevisionId?: string;\n /**\n * Shape the resolved value, mirroring `@sanity/client`:\n * - default → the mutated document\n * - `returnDocuments: false` → an id-shaped mutation result\n * ({@link SingleMutationResult} / {@link MultipleMutationResult})\n * - `returnFirst: false` with documents → an array\n *\n * The static return type reflects the default (the document); pass a\n * type argument or cast when requesting an id-shaped result.\n */\n returnDocuments?: boolean;\n returnFirst?: boolean;\n /** Override the synthetic transaction id used in the result and log. */\n transactionId?: string;\n /** When `true`, validate + shape the result but don't persist. */\n dryRun?: boolean;\n /** Accepted for signature parity; not simulated. */\n visibility?: \"sync\" | \"async\" | \"deferred\";\n /** Accepted for signature parity; not simulated. */\n autoGenerateArrayKeys?: boolean;\n /**\n * Request tag, recorded on the {@link CallRecord} and the\n * {@link MutationLogEntry} for assertions. `null` mirrors\n * `@sanity/client`'s explicit-suppression form and is recorded as-is.\n * No other behavior is simulated.\n */\n tag?: string | null;\n}\n\n/** Options for `action()`. Only `tag` is meaningful to the fake. */\nexport interface ActionOptions {\n /** Request tag, recorded on the {@link CallRecord} (`null` = explicit suppression). */\n tag?: string | null;\n}\n\nexport interface SystemFields {\n _rev: string;\n _createdAt: string;\n _updatedAt: string;\n}\n\nexport interface CreateTestClientOptions {\n /** Default project id. Defaults to `\"test\"`. */\n projectId?: string;\n /** Default dataset name. Defaults to `\"test\"`. */\n dataset?: string;\n /** Documents to seed into the default `(projectId, dataset)` store. */\n documents?: SanityDocumentLike[];\n /**\n * Optional: seed multiple datasets at once. Keys are `\"projectId:dataset\"`\n * pairs. Useful when workflows span multiple datasets.\n */\n datasets?: Record<string, SanityDocumentLike[]>;\n /**\n * Optional: seed any resource type. Keys are `\"<type>:<id>\"`, the\n * `resource` config's two fields: `\"media-library:ml1\"`, `\"canvas:c1\"`,\n * `\"dashboard:d1\"`, or `\"dataset:<projectId>.<dataset>\"` (the same store\n * as `datasets[\"<projectId>:<dataset>\"]`).\n */\n resources?: Record<string, SanityDocumentLike[]>;\n /**\n * The resource the root handle points at, named exactly as\n * `@sanity/client`'s `resource` config names it. While set, it decides\n * the store (over `projectId` / `dataset`), and each capability the\n * platform lacks on that resource type answers with the platform's\n * refusal. `withConfig({ resource })` mints siblings on other resources.\n */\n resource?: TestClientResource;\n /**\n * The organization every resource of this client belongs to — what\n * `GET /media-libraries/<id>`, `/canvases/<id>`, `/dashboards/<id>` and\n * `/projects/<projectId>` answer as `organizationId`.\n */\n organizationId?: string;\n /**\n * The features each project has, keyed by projectId: what\n * `GET /projects/<projectId>/features` answers, sorted. A project\n * without an entry has none and answers `[]`.\n */\n projectFeatures?: Record<string, string[]>;\n /**\n * Opt-in access-control simulation. When set, the client exposes\n * `request({ url })` that serves `/users/me` (host-aware), the\n * configured ACL path, `/projects/<projectId>` (members),\n * `/projects/<projectId>/users/<ids>`, `/projects/<projectId>/roles`,\n * `/projects/<projectId>/features` and the other platform routes,\n * AND every mutation is gated through the supplied `grants`\n * — writes that the grants don't permit throw a `WriteAccessDeniedError`\n * shaped like Sanity's 403. Lets tests demonstrate the full\n * \"engine soft-gate + lake hard-gate\" contract end-to-end without\n * a real HTTP round-trip.\n *\n * When omitted, `request` is undefined and all writes proceed\n * unconditionally — the legacy behavior.\n */\n accessControl?: TestClientAccessControl;\n /**\n * Token→identity registry, shared by the whole `withConfig` family:\n * multiple identities over one store, keyed by the token a handle is\n * bound to — the token determines identity, exactly like production.\n *\n * ```ts\n * const client = createTestClient({\n * token: \"t-admin\",\n * identities: {\n * \"t-admin\": { user: { id: \"admin\", roles: [{ name: \"administrator\" }] }, grants: WILDCARD },\n * \"t-bob\": { user: { id: \"bob\", roles: [{ name: \"editor\" }] }, grants: READ_ONLY },\n * },\n * });\n * const bob = client.withConfig({ token: \"t-bob\" });\n * // same store; bob's /users/me → bob; bob's ACL path → bob's grants\n * ```\n *\n * A token-bound handle behaves exactly like one configured with\n * `accessControl` derived from its identity (identity serving + grant\n * gating; the ACL path defaults per `(projectId, dataset)`). Register\n * more identities later with `addIdentity`.\n */\n identities?: Record<string, TestClientIdentity>;\n /**\n * Token the root handle is bound to — must name an entry in\n * `identities` (unknown tokens throw). Omit to leave the root handle\n * identity-free (a bare factory for token-bound siblings): its\n * `request` is undefined and its reads/writes are ungated. Mutually\n * exclusive with `accessControl`.\n */\n token?: string;\n /**\n * Pluggable write interceptor — runs once for each document's net change in\n * a commit. Document publish/unpublish actions expose their underlying\n * draft/published mutations; release/version actions remain unhooked. Return\n * denial descriptors to reject atomically with `GuardDeniedError`; a\n * thrown callback error propagates unchanged. The client owns no policy.\n * Inherited by `withConfig` siblings. See {@link MutationGuard}.\n */\n mutationGuard?: MutationGuard;\n /**\n * Default perspective applied to reads. Per-fetch `options.perspective`\n * overrides this. `withConfig({ perspective })` returns a sibling\n * handle with a different default. Defaults to `\"raw\"` so existing\n * tests stay green.\n */\n perspective?: Perspective;\n /** Prefix composed with request tags and used alone for untagged requests. */\n requestTagPrefix?: string;\n /**\n * Which API host the root handle models: the project host (default\n * `true`) or the global host (`false`) — same flag as the real\n * `createClient` config. A global-hosted handle's `/users/me` serves\n * the identity's account-global record. `withConfig({ useProjectHostname })`\n * mints siblings on the other host.\n */\n useProjectHostname?: boolean;\n /**\n * The API host, as the real `createClient` config names it (default\n * `https://api.sanity.io`). With {@link apiVersion}, `useCdn` and the\n * host flag it decides the URL a platform error carries as\n * `response.url`: the URL `@sanity/client` would have requested, e.g.\n * `https://p1.api.sanity.io/v2025-02-19/data/doc/production/x?tag=t`.\n */\n apiHost?: string;\n /** The API version (default `\"1\"`, as in `@sanity/client`): `\"YYYY-MM-DD\"`, `\"X\"` or `\"1\"`, `v` prefix optional. */\n apiVersion?: string;\n /**\n * Whether GET reads of the data API (queries, documents, history) go to\n * the API CDN, as in `@sanity/client` (default `true`). Only the URL a\n * platform error names depends on it; the fake serves the same data.\n */\n useCdn?: boolean;\n}\n\nexport type { Perspective } from \"./perspective.ts\";\nexport type { ResourceType, TestClientResource } from \"./resources.ts\";\nexport type {\n Action,\n ActionResult,\n DocumentAction,\n DocumentBody,\n ReleaseAction,\n ReleaseDocument,\n ReleaseMetadata,\n ReleaseState,\n ReleaseType,\n VersionAction,\n} from \"./actions.ts\";\n\nexport type {\n TestClientAccessControl,\n TestClientIdentity,\n DocumentValuePermission,\n} from \"./access-control.ts\";\nexport { WriteAccessDeniedError } from \"./access-control.ts\";\n\nconst DEFAULT_PROJECT_ID = \"test\";\nconst DEFAULT_DATASET = \"test\";\n\n/** One resource's store and the history of every write committed to it. */\ninterface RegistryEntry {\n resource: TestClientResource;\n store: TestStore;\n history: DocumentHistory;\n /**\n * Whether the resource exists for the platform: it was seeded or written\n * to. A handle pointed at it opens its store but does not create it.\n */\n exists: boolean;\n}\n\ntype Registry = Map<string, RegistryEntry>;\n\n/**\n * Shared backing context for one `createTestClient()` call and all the\n * `withConfig` siblings it spawns: the multi-dataset store registry, the\n * append-only mutation log, the stub registry, and the token→identity\n * registry. Sibling handles see each other's writes, log entries, stubs,\n * and registered identities.\n */\ninterface Backend {\n registry: Registry;\n /** See {@link CreateTestClientOptions.organizationId}. */\n organizationId: string | undefined;\n /** See {@link CreateTestClientOptions.projectFeatures}. */\n projectFeatures: Record<string, string[]>;\n log: MutationLogEntry[];\n stubs: Map<string, unknown>;\n calls: CallRecord[];\n /**\n * Pending simulated concurrent edits, keyed per (resource, document id).\n * Each entry is a count of upcoming revision-guarded mutations that\n * should 409 as if another client raced them. See\n * {@link TestClient.simulateConcurrentEdit}.\n */\n concurrentEdits: Map<string, number>;\n /**\n * Token→identity registry. Token-bound handles resolve their acting\n * identity from here on every access, so `addIdentity` upserts take\n * effect on already-minted siblings. See\n * {@link CreateTestClientOptions.identities}.\n */\n identities: Map<string, TestClientIdentity>;\n /** Registered tokens the platform no longer has a session for; see {@link TestClient.revokeIdentity}. */\n revoked: Set<string>;\n /**\n * The registry-wide commit lock: every write path queues on this chain.\n * The real Content Lake validates and applies a mutation atomically\n * server-side; the fake's write paths await user hooks while staging or\n * committing a write. Without serialization two in-flight guarded writes\n * can both validate against the same `_rev` and the loser silently clobbers\n * the winner.\n */\n writeChain: Promise<void>;\n}\n\n/**\n * Queue one write on the backend's commit lock. Corollary of the lock: a\n * gate / `mutationGuard` hook that issues another write through the same\n * registry deadlocks — hooks are read-only by contract.\n */\nfunction serializeWrite<T>(backend: Backend, run: () => Promise<T>): Promise<T> {\n const result = backend.writeChain.then(run);\n backend.writeChain = result.then(\n () => undefined,\n () => undefined,\n );\n return result;\n}\n\n/** Throw unless `token` names a registered identity — every token-binding site validates eagerly. */\nfunction requireIdentity(backend: Backend, token: string, site: string): void {\n if (backend.identities.has(token)) return;\n const known = [...backend.identities.keys()];\n throw fakeError(\n `${site}: unknown token \"${token}\" — no identity is registered for it. ` +\n (known.length > 0\n ? `Registered tokens: ${known.map((t) => `\"${t}\"`).join(\", \")}. `\n : \"No identities are registered. \") +\n \"Register identities via createTestClient({ identities }) or client.addIdentity(token, identity).\",\n );\n}\n\n/** Throw when a single config names both identity mechanisms — a handle acts as exactly one. */\nfunction rejectAmbiguousIdentity(\n site: string,\n config: { token?: string; accessControl?: TestClientAccessControl },\n): void {\n if (config.token !== undefined && config.accessControl !== undefined) {\n throw fakeError(\n `${site}: \\`token\\` and \\`accessControl\\` are mutually exclusive — a handle acts either as ` +\n \"a registered identity (token) or as an inline accessControl config, not both.\",\n );\n }\n}\n\nconst registryKey = (resource: TestClientResource) => `${resource.type}:${resource.id}`;\n\n/** The dataset resource a `(projectId, dataset)` pair names. */\nconst datasetResource = (projectId: string, dataset: string): TestClientResource => ({\n type: \"dataset\",\n id: `${projectId}.${dataset}`,\n});\n\n/** Split a `datasets` seed key, `\"<projectId>:<dataset>\"`. */\nconst parseDatasetKey = (key: string): { projectId: string; dataset: string } => {\n const [projectId, dataset] = key.split(\":\") as [string, string];\n return { projectId, dataset };\n};\n\n/** Split a `resources` seed key, `\"<type>:<id>\"`, and validate it like a `resource` config. */\nconst parseResourceKey = (key: string): TestClientResource => {\n const separator = key.indexOf(\":\");\n const resource = {\n type: key.slice(0, separator),\n id: key.slice(separator + 1),\n } as TestClientResource;\n if (separator <= 0 || resource.id.length === 0) {\n throw fakeError(\n `createTestClient(): \\`resources\\` keys are \"<type>:<id>\" (e.g. \"media-library:ml1\"); got \"${key}\".`,\n );\n }\n validateResource(resource);\n return resource;\n};\n\n/** The registry entry for `resource`, created empty (and not yet existing) on first use. */\nfunction ensureEntry(registry: Registry, resource: TestClientResource): RegistryEntry {\n const key = registryKey(resource);\n let entry = registry.get(key);\n if (entry === undefined) {\n entry = {\n resource,\n store: createTestStore(),\n history: createDocumentHistory(),\n exists: false,\n };\n registry.set(key, entry);\n }\n return entry;\n}\n\n/** Seed `documents` into `resource`'s store; a seeded resource exists. */\nfunction seedEntry(\n registry: Registry,\n resource: TestClientResource,\n documents: SanityDocumentLike[],\n): void {\n const entry = ensureEntry(registry, resource);\n entry.store.seed(documents);\n entry.exists = true;\n}\n\n/**\n * Create a fresh, isolated test client backed by a private multi-dataset\n * registry. `withConfig` returns a sibling handle into the same registry.\n *\n * ```\n * const client = createTestClient({\n * datasets: {\n * \"test:definitions\": [...],\n * \"test:instances\": [...],\n * },\n * });\n * const defs = client.withConfig({ dataset: \"definitions\" });\n * const inst = client.withConfig({ dataset: \"instances\" });\n * ```\n */\nexport function createTestClient(options: CreateTestClientOptions = {}): TestClient {\n const defaultProjectId = options.projectId ?? DEFAULT_PROJECT_ID;\n const defaultDataset = options.dataset ?? DEFAULT_DATASET;\n\n const backend: Backend = {\n registry: new Map(),\n organizationId: options.organizationId,\n projectFeatures: options.projectFeatures ?? {},\n log: [],\n stubs: new Map(),\n calls: [],\n concurrentEdits: new Map(),\n identities: new Map(Object.entries(options.identities ?? {})),\n revoked: new Set(),\n writeChain: Promise.resolve(),\n };\n const registry = backend.registry;\n\n rejectAmbiguousIdentity(\"createTestClient()\", options);\n if (options.token !== undefined) {\n requireIdentity(backend, options.token, \"createTestClient()\");\n }\n\n if (options.resource !== undefined) validateResource(options.resource);\n\n // Seed defaults. `documents` seeds the root handle's own store.\n if (options.documents) {\n const root = options.resource ?? datasetResource(defaultProjectId, defaultDataset);\n seedEntry(registry, root, options.documents);\n }\n if (options.datasets) {\n for (const [key, docs] of Object.entries(options.datasets)) {\n const { projectId, dataset } = parseDatasetKey(key);\n seedEntry(registry, datasetResource(projectId, dataset), docs);\n }\n }\n if (options.resources) {\n for (const [key, docs] of Object.entries(options.resources)) {\n seedEntry(registry, parseResourceKey(key), docs);\n }\n }\n\n return makeHandle({\n backend,\n projectId: defaultProjectId,\n dataset: defaultDataset,\n resource: options.resource,\n accessControl: options.accessControl,\n perspective: options.perspective ?? \"raw\",\n requestTagPrefix: normalizeTagPrefix(options.requestTagPrefix),\n mutationGuard: options.mutationGuard,\n token: options.token,\n useProjectHostname: options.useProjectHostname ?? true,\n hasProjectId: options.projectId !== undefined,\n apiHost: options.apiHost ?? DEFAULT_API_HOST,\n apiVersion: normalizeApiVersion(options.apiVersion ?? DEFAULT_API_VERSION),\n useCdn: options.useCdn !== false,\n configuredPerspective: options.perspective,\n });\n}\n\n/** Affected ids in order, de-duplicated — the `documentIds` of a result. */\nfunction dedupeIds(results: { id: string }[]): string[] {\n const seen = new Set<string>();\n const out: string[] = [];\n for (const r of results) {\n if (!seen.has(r.id)) {\n seen.add(r.id);\n out.push(r.id);\n }\n }\n return out;\n}\n\n/**\n * Resolve a mutation's return value the way `@sanity/client` does, from\n * already-resolved `returnDocuments` / `returnFirst` booleans (callers\n * apply their own defaults: document-first for create-family,\n * ids-for-all for transaction / mutate).\n */\nfunction formatMutationResult(\n documents: SanityDocument[],\n results: { id: string; operation: MutationOperation }[],\n transactionId: string,\n returnDocuments: boolean,\n returnFirst: boolean,\n): TxnCommitResult {\n if (returnDocuments) {\n return returnFirst ? (documents[0] as SanityDocument) : documents;\n }\n if (returnFirst) {\n return {\n transactionId,\n documentId: results[0]?.id as string,\n results,\n } satisfies SingleMutationResult;\n }\n return {\n transactionId,\n documentIds: dedupeIds(results),\n results,\n } satisfies MultipleMutationResult;\n}\n\n/** Convert one raw `mutate()` operation object into a {@link TxnMutation}. */\nfunction toSingleTxnMutation(op: FakeMutation): TxnMutation {\n if (\"create\" in op) return { kind: \"create\", doc: op.create };\n if (\"createOrReplace\" in op) return { kind: \"createOrReplace\", doc: op.createOrReplace };\n if (\"createIfNotExists\" in op) return { kind: \"createIfNotExists\", doc: op.createIfNotExists };\n if (\"delete\" in op) return { kind: \"delete\", id: op.delete.id };\n\n const p = op.patch;\n const ops: PatchOp[] = [];\n if (p.set !== undefined) ops.push({ kind: \"set\", props: p.set });\n if (p.setIfMissing !== undefined) ops.push({ kind: \"setIfMissing\", props: p.setIfMissing });\n if (p.unset !== undefined) ops.push({ kind: \"unset\", paths: p.unset });\n if (p.inc !== undefined) ops.push({ kind: \"inc\", props: p.inc });\n if (p.dec !== undefined) ops.push({ kind: \"dec\", props: p.dec });\n if (p.insert !== undefined) {\n const { before, after, replace, items } = p.insert;\n const position = before !== undefined ? \"before\" : after !== undefined ? \"after\" : \"replace\";\n const anchor = before ?? after ?? replace;\n if (anchor === undefined) {\n throw fakeError(\n \"mutate(): a `patch.insert` operation must name exactly one anchor — `before`, `after`, \" +\n 'or `replace` — pointing at an array path (e.g. \"tags[-1]\" or `tags[_key==\"x\"]`), ' +\n \"alongside `items`. None was set.\",\n );\n }\n ops.push({ kind: \"insert\", position, anchor, items });\n }\n return {\n kind: \"patch\",\n documentId: p.id,\n ops,\n ...(p.ifRevisionID !== undefined ? { ifRev: p.ifRevisionID } : {}),\n };\n}\n\n/** Normalise `mutate()` input (raw array, Patch, or Transaction) to mutations. */\nfunction toTxnMutations(\n operations: FakeMutation[] | PatchHandle | TransactionHandle,\n): TxnMutation[] {\n if (Array.isArray(operations)) return operations.map(toSingleTxnMutation);\n // Both builders expose toJSON(); a Transaction yields an array, a Patch\n // yields a single `{ id, ops, ifRev? }`.\n const json = operations.toJSON();\n if (Array.isArray(json)) return json;\n return [\n {\n kind: \"patch\",\n documentId: json.id,\n ops: json.ops,\n ...(json.ifRev !== undefined ? { ifRev: json.ifRev } : {}),\n },\n ];\n}\n\ninterface MakeHandleArgs {\n backend: Backend;\n projectId: string;\n dataset: string;\n /** The `resource` config, when set; it decides the store over `(projectId, dataset)`. */\n resource: TestClientResource | undefined;\n accessControl: TestClientAccessControl | undefined;\n perspective: Perspective;\n requestTagPrefix: string | undefined;\n mutationGuard: MutationGuard | undefined;\n token: string | undefined;\n /**\n * Which API host this handle models: the project host (true, default)\n * or the global host (`withConfig({ useProjectHostname: false })`).\n * Decides which user record `/users/me` serves — project-scoped vs\n * account-global — exactly like production host routing.\n */\n useProjectHostname: boolean;\n /**\n * Whether this handle or one it was minted from has a `projectId`:\n * configured, or the default standing in for the one `@sanity/client`\n * requires on the project host. A global-hosted client created without one\n * has none, so `config()` omits it; the handle still addresses the default\n * project's stores.\n */\n hasProjectId: boolean;\n apiHost: string;\n apiVersion: string;\n useCdn: boolean;\n /**\n * The perspective the handle's config names, if any. `perspective`\n * falls back to `\"raw\"`; `@sanity/client` sends a perspective only\n * when one is configured or passed.\n */\n configuredPerspective: Perspective | undefined;\n}\n\n/** `@sanity/client`'s mutation options that become query parameters of `/mutate`. */\ninterface MutationQueryOptions {\n dryRun?: boolean;\n returnDocuments?: boolean;\n visibility?: string;\n autoGenerateArrayKeys?: boolean;\n skipCrossDatasetReferenceValidation?: boolean;\n tag?: string | null;\n}\n\nfunction makeHandle(handleArgs: MakeHandleArgs): TestClient {\n const {\n backend,\n resource,\n accessControl,\n perspective,\n requestTagPrefix,\n mutationGuard,\n token,\n useProjectHostname,\n apiHost,\n apiVersion,\n useCdn,\n configuredPerspective,\n } = handleArgs;\n const { registry } = backend;\n // The \"test\" default stands in only for a project-hosted handle; a resource\n // handle is global-hosted whatever `useProjectHostname` says. A dataset\n // resource names its project; other resources name none, so their reported\n // default is not passed on to a sibling that drops the resource.\n const hasProjectId =\n handleArgs.hasProjectId ||\n (useProjectHostname && resource === undefined) ||\n resource?.type === \"dataset\";\n // A dataset resource names its own project and dataset, as in `@sanity/client`.\n const { projectId, dataset } =\n resource?.type === \"dataset\" ? datasetParts(resource.id) : handleArgs;\n /** The resource this handle reads and writes: its `resource` config, else its dataset. */\n const target = resource ?? datasetResource(projectId, dataset);\n\n /**\n * The handle's acting identity, if any. Token-bound handles resolve\n * from the shared registry on every access — so an `addIdentity`\n * upsert takes effect on already-minted siblings; `accessControl`\n * handles use their fixed config. Registry identities resolve their\n * project-scoped record for THIS handle's project (`usersByProject`\n * fallback `user`), so siblings in different projects see different\n * project-scoped ids for the same token — as in production.\n */\n const activeAccess = (): TestClientAccessControl | undefined => {\n if (token === undefined) return accessControl;\n const identity = backend.identities.get(token);\n if (identity === undefined) {\n // Bind-time validation makes this unreachable today; guard anyway\n // so a future identity-removal API fails loudly.\n throw fakeError(`no identity is registered for token \"${token}\" (was it removed?).`);\n }\n return {\n // Per-project records apply only where the resource lives in a project.\n currentUser:\n capabilitiesOf(target.type).identity === \"project\"\n ? projectUserOf(identity, projectId)\n : identity.user,\n ...(identity.globalUser !== undefined ? { globalUser: identity.globalUser } : {}),\n grants: identity.grants,\n ...(identity.access !== undefined ? { access: identity.access } : {}),\n };\n };\n\n /** Non-optional view for sites that only exist when an identity is bound. */\n const requireActiveAccess = (): TestClientAccessControl => {\n const access = activeAccess();\n if (access === undefined) {\n throw fakeError(\"this handle has no acting identity (no token or accessControl bound).\");\n }\n return access;\n };\n\n /** Whether the handle addresses its project's API host; `@sanity/client` sends a `resource` client's requests to the global one. */\n const projectHosted = useProjectHostname && resource === undefined;\n\n /**\n * Throw the platform's 401 when it has no session for the handle's token\n * (revoked), or the token is project-scoped to another project than the\n * API host the handle addresses.\n */\n const refuseSession = (request: PlatformRequest): void => {\n if (token === undefined) return;\n if (backend.revoked.has(token)) throw sessionNotFound(request);\n const tokenProjectId = backend.identities.get(token)?.projectId;\n if (projectHosted && tokenProjectId !== undefined && tokenProjectId !== projectId) {\n throw sessionHostMismatch(request);\n }\n };\n\n /** `request()` with the session check first: a refused token reaches no platform route. */\n const withSession =\n (request: NonNullable<TestClient[\"request\"]>): NonNullable<TestClient[\"request\"]> =>\n async (opts) => {\n refuseSession(rawRequest(opts));\n return request(opts);\n };\n\n /**\n * Throw the Content Lake's refusal when the acting identity's grants hold\n * one it cannot parse. The Content Lake parses the token on every request\n * it serves (queries, documents, mutations, actions, history), so each of\n * those runs this first; the project API, Access API and ACL do not read\n * a token's grants.\n */\n const admitToLake = (request: PlatformRequest): void => {\n refuseSession(request);\n const access = activeAccess();\n if (access === undefined) return;\n const refusal = refusedToken(request, access.grants);\n if (refusal !== undefined) throw refusal;\n };\n /** The handle's API base URLs, as `@sanity/client`'s `config()` sets `url` and `cdnUrl`. */\n const baseUrls = apiBaseUrls({\n apiHost,\n apiVersion,\n projectId: projectHosted ? projectId : undefined,\n });\n /** `@sanity/client`'s `getDataUrl(operation, path)` on this handle. */\n const dataPath = (operation: string, path?: string): string =>\n dataUrl({ resource, dataset, operation, path });\n /** Whether `@sanity/client` takes `uri` for the data API `operation` of this handle (`isQuery`, `isDoc`, …). */\n const addresses = (uri: string, operation: string): boolean =>\n (hasProjectId || resource !== undefined) && uri.startsWith(dataPath(operation));\n /** `isData`: a URI the API CDN serves, when `useCdn` is on. */\n const isDataUri = (uri: string): boolean =>\n uri.startsWith(\"/data/\") ||\n [\"query\", \"mutate\", \"doc\", \"listen\", \"history\"].some((operation) => addresses(uri, operation));\n\n /**\n * The request `@sanity/client` sends for `uri` on this handle, its URL\n * built as `_prepareRequest` builds it (@sanity/client 8.16.0\n * `src/data/dataMethods.ts:1020-1120`): the tag first, then on the query\n * endpoint the perspective and `returnQuery`, on the CDN base for a GET of\n * the data API unless the perspective needs fresh drafts.\n */\n const platformRequest = (args: {\n method: PlatformRequest[\"method\"];\n uri: string;\n tag?: string | null | undefined;\n /** The query parameters the calling method sets, in its order. */\n query?: QueryEntries;\n /** As the calling method sets it; otherwise a GET of the data API may use the CDN. */\n canUseCdn?: boolean;\n perspective?: Perspective | undefined;\n returnQuery?: boolean | undefined;\n }): PlatformRequest => {\n const { method, uri, tag } = args;\n const query = [...(args.query ?? [])];\n let cdn = useCdn && (args.canUseCdn ?? (method === \"GET\" && isDataUri(uri)));\n const effectiveTag =\n tag && requestTagPrefix ? `${requestTagPrefix}.${tag}` : tag || requestTagPrefix;\n if (effectiveTag && tag !== null) query.unshift([\"tag\", effectiveTag]);\n if (addresses(uri, \"query\")) {\n const perspectiveOption = args.perspective || configuredPerspective;\n if (perspectiveOption !== undefined) {\n const value = Array.isArray(perspectiveOption)\n ? perspectiveOption.join(\",\")\n : perspectiveOption;\n query.unshift([\"perspective\", value]);\n // Drafts cannot come from the CDN, so `@sanity/client` goes to the API for them.\n if (\n (Array.isArray(perspectiveOption) && perspectiveOption.length > 0) ||\n perspectiveOption === \"previewDrafts\" ||\n perspectiveOption === \"drafts\"\n ) {\n cdn = false;\n }\n }\n if (args.returnQuery === false) query.unshift([\"returnQuery\", \"false\"]);\n }\n const queryStart = uri.indexOf(\"?\");\n return {\n method,\n path: queryStart === -1 ? uri : uri.slice(0, queryStart),\n url: requestUrl(cdn ? baseUrls.cdnUrl : baseUrls.url, uri, query),\n };\n };\n\n /** `getDocument(s)`, `releases.get`: `GET <data>/doc/<ids>`. */\n const docRequest = (ids: string, tag: string | null | undefined): PlatformRequest =>\n platformRequest({ method: \"GET\", uri: dataPath(\"doc\", ids), tag });\n\n /**\n * A GROQ query, sent in the URL while it is short enough and as a `POST`\n * body otherwise (`_dataRequestOptions`, `encodeQueryString`).\n */\n const queryRequest = (args: {\n query: string;\n params: Record<string, unknown> | undefined;\n tag: string | null | undefined;\n perspective: Perspective | undefined;\n returnQuery: boolean | undefined;\n }): PlatformRequest => {\n const encoded = encodeGroqQuery(args.query, args.params);\n const useGet = encoded.length < QUERY_SIZE_LIMIT;\n return platformRequest({\n method: useGet ? \"GET\" : \"POST\",\n uri: dataPath(\"query\", useGet ? encoded : \"\"),\n tag: args.tag,\n canUseCdn: true,\n perspective: args.perspective,\n returnQuery: args.returnQuery,\n });\n };\n\n /** `POST <data>/mutate`, with the query parameters `getMutationQuery` sets. */\n const mutateRequest = (options: MutationQueryOptions = {}): PlatformRequest => {\n const { returnDocuments } = options;\n return platformRequest({\n method: \"POST\",\n uri: dataPath(\"mutate\"),\n tag: options.tag,\n canUseCdn: false,\n query: [\n [\"dryRun\", options.dryRun],\n [\"returnIds\", true],\n [\"returnDocuments\", returnDocuments === false ? undefined : (returnDocuments ?? true)],\n [\"visibility\", options.visibility || \"sync\"],\n [\"autoGenerateArrayKeys\", options.autoGenerateArrayKeys],\n [\"skipCrossDatasetReferenceValidation\", options.skipCrossDatasetReferenceValidation],\n ],\n });\n };\n\n /** `client.request({ url })`: a GET of `url` as given. */\n const rawRequest = (opts: { url?: string; uri?: string; tag?: string | null }): PlatformRequest =>\n platformRequest({ method: \"GET\", uri: opts.uri ?? opts.url ?? \"\", tag: opts.tag });\n /**\n * Every principal the handle's member listings see: the registered\n * identities, plus the handle's own inline `accessControl` actor, which is\n * a member wherever its config gives it roles.\n */\n const members = (): TestClientIdentity[] => [\n ...backend.identities.values(),\n ...(token === undefined && accessControl !== undefined\n ? [\n {\n user: accessControl.currentUser,\n ...(accessControl.globalUser !== undefined\n ? { globalUser: accessControl.globalUser }\n : {}),\n grants: accessControl.grants,\n ...(accessControl.access !== undefined ? { access: accessControl.access } : {}),\n },\n ]\n : []),\n ];\n const capabilities = capabilitiesOf(target.type);\n /** How error messages name the target, e.g. `dataset \"test:test\"`. */\n const targetLabel = capabilities.label(target.id);\n const targetEntry = ensureEntry(registry, target);\n const { store, history } = targetEntry;\n /**\n * The store as `client.store()` hands it out: seeding or writing to it\n * makes the resource exist. Writes through the handle use `store` and\n * make it exist when they commit.\n */\n const publicStore: TestStore = {\n ...store,\n seed: (documents) => {\n store.seed(documents);\n targetEntry.exists = true;\n },\n add: (documents) => {\n store.add(documents);\n targetEntry.exists = true;\n },\n put: (doc) => {\n store.put(doc);\n targetEntry.exists = true;\n },\n };\n\n /** Key into {@link Backend.concurrentEdits} for a doc in this resource. */\n const concurrentEditKey = (id: string): string => registryKey(target) + \" \" + id;\n\n /**\n * The id grant filters see as `identity()` and history records as the\n * author: the project-scoped record on a dataset, the account-global\n * one on every other resource type.\n */\n const actorId = (): string | undefined => {\n const access = activeAccess();\n if (access === undefined) return undefined;\n return capabilities.identity === \"global\"\n ? (access.globalUser ?? access.currentUser).id\n : access.currentUser.id;\n };\n\n /** The store before the write in flight; see {@link write}. */\n let beforeWrite: Map<string, SanityDocument> | undefined;\n /** The request of the write in flight, which a refused write's error names. */\n let writeRequest: PlatformRequest = DEFAULT_WRITE;\n\n /**\n * Queue a write on the commit lock and remember the store's images before\n * it, so {@link commitHistory} can record exactly what the write changed.\n */\n const write = <T>(run: () => Promise<T>, request: PlatformRequest): Promise<T> =>\n serializeWrite(backend, async () => {\n admitToLake(request);\n beforeWrite = new Map(store.all().map((doc) => [doc._id, doc]));\n writeRequest = request;\n try {\n return await run();\n } finally {\n beforeWrite = undefined;\n writeRequest = DEFAULT_WRITE;\n }\n });\n\n /** Record the write in flight in the resource's history, under `transactionId`. */\n const commitHistory = (transactionId: string): void => {\n if (beforeWrite === undefined) return;\n targetEntry.exists = true;\n history.commit({ transactionId, author: actorId(), before: beforeWrite, after: store.all() });\n };\n\n /** Append a committed transaction to the shared log. */\n const record = (transactionId: string, mutations: TxnMutation[], tag?: string | null): void => {\n const effectiveTag = effectiveRequestTag(tag);\n commitHistory(transactionId);\n backend.log.push({\n transactionId,\n projectId,\n dataset,\n ...(resource !== undefined ? { resource } : {}),\n mutations,\n ...(effectiveTag !== undefined ? { tag: effectiveTag } : {}),\n });\n };\n\n const effectiveRequestTag = (tag: string | null | undefined): string | null | undefined => {\n if (!requestTagPrefix) return tag;\n return tag ? `${requestTagPrefix}.${tag}` : requestTagPrefix;\n };\n\n /**\n * Shape a single-document write result. Create-family methods default\n * to returning the document (first), unlike transaction / mutate.\n */\n const writeResult = (\n documents: SanityDocument[],\n results: { id: string; operation: MutationOperation }[],\n transactionId: string,\n opts: MutationOptions | undefined,\n ): TxnCommitResult =>\n formatMutationResult(\n documents,\n results,\n transactionId,\n opts?.returnDocuments ?? true,\n opts?.returnFirst ?? true,\n );\n\n const promote = (doc: SanityDocumentLike): SanityDocument & SystemFields => {\n const now = new Date().toISOString();\n if (typeof doc._id !== \"string\" || doc._id.length === 0) {\n throw fakeError(\n `Cannot store a document without a non-empty string \\`_id\\` (got ${JSON.stringify(doc._id)}). ` +\n `Unlike @sanity/client, this fake never auto-generates ids — give every document an explicit _id.`,\n );\n }\n if (typeof doc._type !== \"string\" || doc._type.length === 0) {\n throw fakeError(\n `Document \"${doc._id}\" is missing a non-empty string \\`_type\\` (got ${JSON.stringify(doc._type)}). ` +\n `Every Sanity document needs a _type.`,\n );\n }\n return {\n ...doc,\n _id: doc._id,\n _type: doc._type,\n _rev: cryptoRandomKey(),\n _createdAt: doc._createdAt ?? now,\n _updatedAt: now,\n } as SanityDocument & SystemFields;\n };\n\n const checkRevision = (id: string, ifRevisionId: string | undefined) => {\n if (ifRevisionId === undefined) return;\n // A pending simulated concurrent edit beats the real revision check —\n // the guarded write loses the race even if its _rev currently matches.\n const editKey = concurrentEditKey(id);\n const pendingEdits = backend.concurrentEdits.get(editKey) ?? 0;\n if (pendingEdits > 0) {\n backend.concurrentEdits.set(editKey, pendingEdits - 1);\n throw fakeError(\n `ifRevisionId check failed: document \"${id}\" was concurrently edited (simulated via ` +\n `simulateConcurrentEdit) — the revision-guarded mutation lost the race; re-read and retry. ` +\n `(This mirrors Sanity's optimistic-locking 409.)`,\n );\n }\n const existing = store.get(id);\n if (existing === undefined) {\n throw fakeError(\n `ifRevisionId check failed: document \"${id}\" does not exist, so its revision cannot match ` +\n `the expected _rev=\"${ifRevisionId}\". Drop ifRevisionId when creating a new document, or ` +\n `seed/create the document before guarding on its revision.`,\n );\n }\n if (existing._rev !== ifRevisionId) {\n throw fakeError(\n `ifRevisionId check failed: document \"${id}\" is at _rev=\"${existing._rev}\" but the mutation ` +\n `required _rev=\"${ifRevisionId}\". The document changed since you read it — re-read it and ` +\n `retry. (This mirrors Sanity's optimistic-locking 409.)`,\n );\n }\n };\n\n /**\n * Gate a written document through the configured `accessControl.grants`.\n * No-op when accessControl is absent (legacy bench behavior). On denial\n * throws `WriteAccessDeniedError` — same failure mode tests get from\n * a real Sanity 403.\n *\n * Pass the document before the write (`undefined` when it is created)\n * and after it (`undefined` when it is deleted). The rule mirrors the\n * Content Lake's permission checker: a create needs `create` on the new\n * document; an update, replace or patch needs `update` on the document\n * before AND after; a delete needs `update` on the document before.\n * System ids (`_.`) need `manage` as well, checked first.\n */\n const gate = async (prev: SanityDocument | undefined, next: SanityDocument | undefined) => {\n const access = activeAccess();\n if (access === undefined) return;\n const id = (prev ?? next)?._id;\n if (id === undefined) return;\n const required: DocumentValuePermission[] = isSystemId(id) ? [\"manage\"] : [];\n required.push(prev === undefined ? \"create\" : \"update\");\n for (const permission of required) {\n for (const document of [prev, next]) {\n if (document === undefined) continue;\n await enforceWriteAccess({\n document,\n grants: access.grants,\n permission,\n userId: actorId() as string,\n request: writeRequest,\n });\n }\n }\n };\n\n const guardGate = async (contexts: readonly MutationGuardImageContext[]): Promise<void> => {\n const identity = actorId();\n await enforceMutationGuards(\n mutationGuard,\n contexts.map((context) => ({ ...context, identity })),\n writeRequest,\n );\n };\n\n const guardOne = async (\n action: MutationGuardContext[\"action\"],\n before: MutationGuardContext[\"before\"],\n after: MutationGuardContext[\"after\"],\n ): Promise<void> => {\n await guardGate([{ action, before, after }]);\n };\n\n /**\n * Read-gate predicate. Sanity surfaces invisible docs as \"doesn't\n * exist\" on the wire (404 / null) rather than throwing 403 — so\n * reads filter rather than refuse.\n *\n * - `fetch` filters the dataset before running GROQ.\n * - `getDocument` / `getDocuments` return null for invisible docs.\n * - `snapshot` is a test-helper that bypasses the read gate\n * intentionally (tests need raw inspection).\n *\n * Returns `true` when accessControl is absent (no gating).\n */\n const canRead = async (\n document: SanityDocumentLike & { _id?: string; _type?: string },\n ): Promise<boolean> => {\n const access = activeAccess();\n if (access === undefined) return true;\n return canAccess({\n document,\n grants: access.grants,\n permission: \"read\",\n userId: actorId() as string,\n });\n };\n\n const applyPatch = async (\n documentId: string,\n ops: PatchOp[],\n ifRev: string | undefined,\n options: { inTransaction?: boolean } = {},\n ): Promise<SanityDocument> => {\n const existing = store.get(documentId);\n if (existing === undefined) {\n throw fakeError(\n `Cannot patch document \"${documentId}\": not found — no document with that _id exists in ` +\n `${targetLabel}. patch() requires the target to already exist; use ` +\n `createOrReplace() / createIfNotExists() to upsert, or seed the document first.`,\n );\n }\n checkRevision(documentId, ifRev);\n const patched = applyPatchOps(existing, ops);\n const now = new Date().toISOString();\n const next: SanityDocument = {\n ...patched,\n _rev: cryptoRandomKey(),\n _updatedAt: now,\n };\n // A transaction checks its net change per document once staged.\n if (options.inTransaction !== true) {\n await gate(existing, next);\n await guardOne(\"update\", existing, next);\n }\n store.put(next);\n return next;\n };\n\n const applyTransaction = async (\n mutations: TxnMutation[],\n txnOptions?: TxnCommitOptions,\n ): Promise<TxnCommitResult> => {\n const transactionId = txnOptions?.transactionId ?? cryptoRandomKey();\n const snapshot = new Map<string, SanityDocument | undefined>();\n const touchedIds: string[] = [];\n const operations: { id: string; operation: MutationOperation }[] = [];\n\n const remember = (id: string) => {\n if (!snapshot.has(id)) snapshot.set(id, store.get(id));\n };\n\n try {\n for (const mut of mutations) {\n switch (mut.kind) {\n case \"create\": {\n remember(mut.doc._id as string);\n const promoted = promote(mut.doc);\n if (store.get(promoted._id) !== undefined) {\n throw fakeError(\n `create() failed: a document with _id \"${promoted._id}\" already exists in ` +\n `${targetLabel}. Use createOrReplace() to overwrite it, or ` +\n `createIfNotExists() to leave the existing one untouched.`,\n );\n }\n store.put(promoted);\n touchedIds.push(promoted._id);\n operations.push({ id: promoted._id, operation: \"create\" });\n break;\n }\n case \"createOrReplace\": {\n remember(mut.doc._id as string);\n const promoted = promote(mut.doc);\n const wasPresent = store.get(promoted._id) !== undefined;\n store.put(promoted);\n touchedIds.push(promoted._id);\n operations.push({ id: promoted._id, operation: wasPresent ? \"update\" : \"create\" });\n break;\n }\n case \"createIfNotExists\": {\n remember(mut.doc._id as string);\n if (store.get(mut.doc._id as string) === undefined) {\n const promoted = promote(mut.doc);\n store.put(promoted);\n touchedIds.push(promoted._id);\n operations.push({ id: promoted._id, operation: \"create\" });\n }\n break;\n }\n case \"delete\": {\n remember(mut.id);\n const removed = store.remove(mut.id);\n if (removed) {\n touchedIds.push(mut.id);\n operations.push({ id: mut.id, operation: \"delete\" });\n }\n break;\n }\n case \"patch\": {\n remember(mut.documentId);\n await applyPatch(mut.documentId, mut.ops, mut.ifRev, { inTransaction: true });\n touchedIds.push(mut.documentId);\n operations.push({ id: mut.documentId, operation: \"update\" });\n break;\n }\n }\n }\n // Like the Content Lake, check each document's net change once the\n // whole transaction is staged, not each mutation on its way there.\n const images = [...snapshot].map(([id, before]) => ({ before, after: store.get(id) }));\n for (const { before, after } of images) {\n if (before !== after) await gate(before, after);\n }\n await guardGate(mutationContexts(images));\n } catch (error) {\n for (const [id, doc] of snapshot) {\n if (doc === undefined) {\n store.remove(id);\n } else {\n store.put(doc);\n }\n }\n throw error;\n }\n\n // A committed transaction is logged whole; a single-mutation form\n // (create / patch / mutate of one op) lands as a one-entry transaction.\n record(transactionId, mutations, txnOptions?.tag);\n\n const documents = touchedIds\n .map((id) => store.get(id))\n .filter((d): d is SanityDocument => d !== undefined);\n // Transaction / mutate default to the id-shaped `MultipleMutationResult`,\n // matching `@sanity/client` (create-family defaults differ).\n return formatMutationResult(\n documents,\n operations,\n transactionId,\n txnOptions?.returnDocuments ?? false,\n txnOptions?.returnFirst ?? false,\n );\n };\n\n /**\n * The action-API write path, on the same commit lock as every mutation.\n * Document publish/unpublish and `version.create` actions run through the\n * access gate; publish/unpublish expose their net draft/published mutations\n * to `mutationGuard`. Other release/version actions apply unhooked.\n */\n const runAction = async (\n actions: Action | Action[],\n tag?: string | null,\n ): Promise<ActionResult> => {\n // Refused before anything applies: a refused token before routing, an\n // unmounted route before the Content Lake reads the token, a refused\n // action type after it does.\n const request = platformRequest({\n method: \"POST\",\n uri: dataPath(\"actions\"),\n tag,\n canUseCdn: false,\n });\n refuseSession(request);\n if (capabilities.actions !== \"served\") throw capabilities.actions(request);\n return write(async () => {\n for (const action of Array.isArray(actions) ? actions : [actions]) {\n const refused = capabilities.refusedActions[action.actionType];\n if (refused !== undefined) throw refused(request);\n }\n const result = await applyActions(store, actions, { gate, guard: guardGate, request });\n commitHistory(result.transactionId);\n return result;\n }, request);\n };\n\n /**\n * Filter the store's docs to only those `canRead` returns true\n * for. When accessControl is unset, returns the full set (no\n * filtering cost). Used by `fetch` so GROQ runs against the\n * actor-visible subset, matching Sanity's per-token query behavior.\n */\n const accessVisibleDocs = async (): Promise<SanityDocument[]> => {\n const all = store.all();\n if (activeAccess() === undefined) return all;\n const out: SanityDocument[] = [];\n for (const doc of all) {\n if (await canRead(doc)) out.push(doc);\n }\n return out;\n };\n\n /**\n * Apply both access-control gating and perspective layering. The order\n * matters: access first (an invisible doc shouldn't even be a candidate\n * for perspective layering), perspective second (which can hide / merge\n * docs that the actor would otherwise see).\n */\n const visibleDocs = async (override?: Perspective): Promise<SanityDocument[]> => {\n const accessible = await accessVisibleDocs();\n return applyPerspective(accessible, override ?? perspective);\n };\n\n const client: TestClient = {\n fetch: async (query, params, opts) => {\n admitToLake(\n queryRequest({\n query,\n params,\n tag: opts?.tag,\n perspective: opts?.perspective,\n returnQuery: opts?.filterResponse === false && opts.returnQuery !== false,\n }),\n );\n const start = Date.now();\n const tree = parse(query, params ? { params } : undefined);\n const value = await evaluate(tree, {\n dataset: await visibleDocs(opts?.perspective),\n ...(params !== undefined ? { params } : {}),\n });\n const result = await value.get();\n if (opts?.filterResponse === false) {\n const ms = Date.now() - start;\n const raw = opts.returnQuery === false ? { ms, result } : { query, ms, result };\n return raw as never;\n }\n return result as never;\n },\n\n getDocument: async (id, opts) => {\n admitToLake(docRequest(id, opts?.tag));\n const effective = opts?.perspective ?? perspective;\n if (effective === \"raw\") {\n const doc = store.get(id);\n if (doc === undefined) return undefined;\n if (!(await canRead(doc))) return undefined;\n return doc as never;\n }\n const view = await visibleDocs(effective);\n const found = resolveDocument(view, id, effective);\n return (found ?? undefined) as never;\n },\n\n getDocuments: async (ids, opts) => {\n admitToLake(docRequest(ids.join(\",\"), opts?.tag));\n const effective = opts?.perspective ?? perspective;\n if (effective === \"raw\") {\n const result: (SanityDocument | null)[] = [];\n for (const id of ids) {\n const doc = store.get(id);\n if (doc === undefined) result.push(null);\n else if (await canRead(doc)) result.push(doc);\n else result.push(null);\n }\n return result as never;\n }\n const view = await visibleDocs(effective);\n return ids.map((id) => resolveDocument(view, id, effective) ?? null) as never;\n },\n\n getDataUrl: dataPath,\n\n create: (doc, opts) =>\n write(async () => {\n checkRevision(doc._id as string, opts?.ifRevisionId);\n if (store.get(doc._id as string) !== undefined) {\n throw fakeError(\n `create() failed: a document with _id \"${doc._id}\" already exists in ` +\n `${targetLabel}. Use createOrReplace() to overwrite it, or ` +\n `createIfNotExists() to leave the existing one untouched.`,\n );\n }\n const promoted = promote(doc);\n await gate(undefined, promoted);\n await guardOne(\"create\", null, promoted);\n const transactionId = opts?.transactionId ?? cryptoRandomKey();\n if (opts?.dryRun !== true) {\n store.put(promoted);\n record(transactionId, [{ kind: \"create\", doc }], opts?.tag);\n }\n return writeResult(\n [promoted],\n [{ id: promoted._id, operation: \"create\" }],\n transactionId,\n opts,\n ) as never;\n }, mutateRequest(opts)),\n\n createOrReplace: (doc, opts) =>\n write(async () => {\n checkRevision(doc._id as string, opts?.ifRevisionId);\n const promoted = promote(doc);\n const existing = store.get(doc._id as string);\n await gate(existing, promoted);\n await guardOne(existing !== undefined ? \"update\" : \"create\", existing ?? null, promoted);\n const transactionId = opts?.transactionId ?? cryptoRandomKey();\n if (opts?.dryRun !== true) {\n store.put(promoted);\n record(transactionId, [{ kind: \"createOrReplace\", doc }], opts?.tag);\n }\n const operation: MutationOperation = existing !== undefined ? \"update\" : \"create\";\n return writeResult(\n [promoted],\n [{ id: promoted._id, operation }],\n transactionId,\n opts,\n ) as never;\n }, mutateRequest(opts)),\n\n createIfNotExists: (doc, opts) =>\n write(async () => {\n checkRevision(doc._id as string, opts?.ifRevisionId);\n const existing = store.get(doc._id as string);\n const transactionId = opts?.transactionId ?? cryptoRandomKey();\n if (existing !== undefined) {\n return writeResult(\n [existing],\n [{ id: existing._id, operation: \"none\" }],\n transactionId,\n opts,\n ) as never;\n }\n const promoted = promote(doc);\n await gate(undefined, promoted);\n await guardOne(\"create\", null, promoted);\n if (opts?.dryRun !== true) {\n store.put(promoted);\n record(transactionId, [{ kind: \"createIfNotExists\", doc }], opts?.tag);\n }\n return writeResult(\n [promoted],\n [{ id: promoted._id, operation: \"create\" }],\n transactionId,\n opts,\n ) as never;\n }, mutateRequest(opts)),\n\n delete: (id, opts) =>\n write(async () => {\n checkRevision(id, opts?.ifRevisionId);\n const existing = store.get(id);\n if (existing !== undefined) await gate(existing, undefined);\n if (existing !== undefined) await guardOne(\"delete\", existing, null);\n const transactionId = opts?.transactionId ?? cryptoRandomKey();\n let removed: SanityDocument | undefined = existing;\n if (opts?.dryRun !== true) {\n removed = store.remove(id);\n if (removed !== undefined) record(transactionId, [{ kind: \"delete\", id }], opts?.tag);\n }\n // Legacy default shape ({ _id } | null) unless the caller asked for\n // a specific return shape via returnDocuments / returnFirst.\n if (opts?.returnDocuments === undefined && opts?.returnFirst === undefined) {\n return (removed !== undefined ? { _id: id } : null) as never;\n }\n const results = removed !== undefined ? [{ id, operation: \"delete\" as const }] : [];\n const docs = removed !== undefined ? [removed] : [];\n return formatMutationResult(\n docs,\n results,\n transactionId,\n opts.returnDocuments ?? true,\n opts.returnFirst ?? true,\n ) as never;\n }, mutateRequest(opts)),\n\n patch: (documentId) =>\n createPatch(documentId, {\n apply: (docId, ops, ifRev, options) =>\n write(async () => {\n const doc = await applyPatch(docId, ops, ifRev);\n record(\n cryptoRandomKey(),\n [\n {\n kind: \"patch\",\n documentId: docId,\n ops,\n ...(ifRev !== undefined ? { ifRev } : {}),\n },\n ],\n options?.tag,\n );\n return doc;\n }, mutateRequest(options)),\n }),\n\n transaction: () =>\n createTransaction({\n apply: (mutations, txnOptions) =>\n write(\n () => applyTransaction(mutations, txnOptions),\n // `Transaction.commit()` asks for ids unless told otherwise.\n mutateRequest({ ...txnOptions, returnDocuments: txnOptions?.returnDocuments ?? false }),\n ),\n }),\n\n mutate: (operations, options) =>\n write(() => applyTransaction(toTxnMutations(operations), options), mutateRequest(options)),\n\n // Synchronous writes still take the lock: an action landing while a\n // guarded patch is parked in its gate would otherwise be clobbered\n // when that patch resumes over its pre-action snapshot.\n action: (action, options) => runAction(action, options?.tag),\n\n releases: {\n create: async ({ releaseId, metadata }) => {\n const result = await runAction({\n actionType: \"sanity.action.release.create\",\n releaseId,\n ...(metadata !== undefined ? { metadata } : {}),\n });\n const doc = store.get(`_.releases.${releaseId}`) as ReleaseDocument | undefined;\n return {\n ...result,\n releaseId,\n metadata: doc?.metadata ?? { releaseType: \"undecided\" },\n };\n },\n edit: ({ releaseId, patch }) =>\n runAction({ actionType: \"sanity.action.release.edit\", releaseId, patch }),\n // `@sanity/client` reads a release by id and its documents by query.\n get: async ({ releaseId }) => {\n admitToLake(docRequest(`_.releases.${releaseId}`, undefined));\n const doc = store.get(`_.releases.${releaseId}`) as ReleaseDocument | undefined;\n return doc !== undefined && (await canRead(doc)) ? doc : undefined;\n },\n fetchDocuments: async ({ releaseId }) => {\n admitToLake(\n queryRequest({\n query: \"*[sanity::partOfRelease($releaseId)]\",\n params: { releaseId },\n tag: undefined,\n perspective: undefined,\n returnQuery: undefined,\n }),\n );\n const prefix = `versions.${releaseId}.`;\n return (await accessVisibleDocs()).filter((d) => d._id.startsWith(prefix));\n },\n schedule: ({ releaseId, publishAt }) =>\n runAction({\n actionType: \"sanity.action.release.schedule\",\n releaseId,\n publishAt,\n }),\n unschedule: ({ releaseId }) =>\n runAction({ actionType: \"sanity.action.release.unschedule\", releaseId }),\n publish: ({ releaseId }) =>\n runAction({ actionType: \"sanity.action.release.publish\", releaseId }),\n archive: ({ releaseId }) =>\n runAction({ actionType: \"sanity.action.release.archive\", releaseId }),\n unarchive: ({ releaseId }) =>\n runAction({ actionType: \"sanity.action.release.unarchive\", releaseId }),\n delete: ({ releaseId }) =>\n runAction({ actionType: \"sanity.action.release.delete\", releaseId }),\n },\n\n createVersion: (params) => {\n const { releaseId, publishedId, document, baseId, ifBaseRevisionId } = params as {\n releaseId: string;\n publishedId: string;\n document?: DocumentBody;\n baseId?: string;\n ifBaseRevisionId?: string;\n };\n // `@sanity/client` copies from `baseId` whenever `document` is falsy.\n if (document) {\n return runAction({\n actionType: \"sanity.action.document.version.create\",\n releaseId,\n publishedId,\n document,\n });\n }\n if (baseId === undefined) {\n throw fakeError(\"createVersion() requires `baseId` when no `document` is provided.\");\n }\n return runAction({\n actionType: \"sanity.action.document.version.create\",\n publishedId,\n baseId,\n versionId: `versions.${releaseId}.${publishedId}`,\n ...(ifBaseRevisionId !== undefined ? { ifBaseRevisionId } : {}),\n });\n },\n\n unpublishVersion: ({ releaseId, publishedId }) =>\n runAction({\n actionType: \"sanity.action.document.version.unpublish\",\n versionId: `versions.${releaseId}.${publishedId}`,\n publishedId,\n }),\n\n ...(token !== undefined || accessControl !== undefined\n ? {\n request: withSession(\n makeRequestImpl({\n getAccess: requireActiveAccess,\n requestFor: rawRequest,\n projectId,\n useProjectHostname: projectHosted,\n organizationId: backend.organizationId,\n listIdentities: members,\n platform: {\n projectId,\n organizationId: backend.organizationId,\n projectFeatures: backend.projectFeatures,\n own: (asked) =>\n registryKey(asked) === registryKey(target)\n ? {\n grants: requireActiveAccess().grants,\n documents: () => store.all(),\n history,\n canRead,\n admitToLake,\n }\n : undefined,\n actorUser: () => {\n const access = requireActiveAccess();\n return access.globalUser ?? access.currentUser;\n },\n actorAccess: () => requireActiveAccess().access,\n actorProjectId: () =>\n token === undefined ? undefined : backend.identities.get(token)?.projectId,\n identities: members,\n // A dataset stands for its project, and every project exists in\n // the fake, so a dataset handle still counts as before.\n resources: () =>\n [...registry.values()]\n .filter((e) => e.exists || e.resource.type === \"dataset\")\n .map((e) => e.resource),\n },\n }),\n ),\n }\n : {}),\n\n config: () => ({\n ...(hasProjectId || resource !== undefined ? { projectId } : {}),\n dataset,\n ...(resource !== undefined ? { resource } : {}),\n perspective,\n ...(requestTagPrefix !== undefined ? { requestTagPrefix } : {}),\n ...(token !== undefined ? { token } : {}),\n useProjectHostname,\n apiHost,\n apiVersion,\n useCdn,\n }),\n\n withConfig: (config) => {\n rejectAmbiguousIdentity(\"withConfig()\", config);\n if (config.token !== undefined) {\n requireIdentity(backend, config.token, \"withConfig()\");\n }\n // Naming either identity mechanism switches the sibling to it\n // wholesale; naming neither inherits this handle's binding.\n const rebinds = config.token !== undefined || config.accessControl !== undefined;\n // `resource: undefined` drops the resource; omitting the key keeps it.\n const nextResource = \"resource\" in config ? config.resource : resource;\n if (nextResource !== undefined) validateResource(nextResource);\n return makeHandle({\n backend,\n projectId: config.projectId ?? projectId,\n dataset: config.dataset ?? dataset,\n resource: nextResource,\n accessControl: rebinds ? config.accessControl : accessControl,\n perspective: config.perspective ?? perspective,\n // `requestTagPrefix: undefined` unsets the prefix, and an inherited\n // one is normalized again, as `@sanity/client` re-runs `initConfig`\n // on the merged config (`SanityClient.ts:1417`, `config.ts:239-244`).\n requestTagPrefix: normalizeTagPrefix(\n \"requestTagPrefix\" in config ? config.requestTagPrefix : requestTagPrefix,\n ),\n mutationGuard,\n token: rebinds ? config.token : token,\n useProjectHostname: config.useProjectHostname ?? useProjectHostname,\n hasProjectId: hasProjectId || config.projectId !== undefined,\n apiHost: config.apiHost ?? apiHost,\n apiVersion:\n config.apiVersion !== undefined ? normalizeApiVersion(config.apiVersion) : apiVersion,\n useCdn: config.useCdn ?? useCdn,\n configuredPerspective: config.perspective ?? configuredPerspective,\n });\n },\n\n seed: (documents) => publicStore.seed(documents),\n add: (documents) => publicStore.add(documents),\n reset: () => store.reset(),\n snapshot: () => store.snapshot(),\n store: () => publicStore,\n simulateConcurrentEdit: (id, count = 1) => {\n const editKey = concurrentEditKey(id);\n backend.concurrentEdits.set(editKey, (backend.concurrentEdits.get(editKey) ?? 0) + count);\n },\n datasets: () =>\n [...registry.values()]\n .filter((entry) => entry.resource.type === \"dataset\")\n .map((entry) => datasetParts(entry.resource.id)),\n resources: () => [...registry.values()].map((entry) => ({ ...entry.resource })),\n stub: (name, handler) => {\n backend.stubs.set(name, handler);\n },\n stubs: () => Object.fromEntries(backend.stubs),\n addIdentity: (identityToken, identity) => {\n backend.identities.set(identityToken, identity);\n },\n identities: () => Object.fromEntries(backend.identities),\n revokeIdentity: (revokedToken) => {\n requireIdentity(backend, revokedToken, \"revokeIdentity()\");\n backend.revoked.add(revokedToken);\n },\n mutations: () => backend.log.flatMap((entry) => entry.mutations),\n transactions: () => [...backend.log],\n clearLog: () => {\n backend.log.length = 0;\n },\n calls: (method) =>\n method === undefined ? [...backend.calls] : backend.calls.filter((c) => c.method === method),\n callCount: (method) =>\n method === undefined\n ? backend.calls.length\n : backend.calls.filter((c) => c.method === method).length,\n clearCalls: () => {\n backend.calls.length = 0;\n },\n };\n\n // Record an invocation: capture args now, fill the outcome on settle.\n const onCall = (method: string, args: unknown[]): CallSettler => {\n const rec: CallRecord = {\n method,\n args,\n projectId,\n dataset,\n ...(resource !== undefined ? { resource } : {}),\n ...(token !== undefined ? { token } : {}),\n apiVersion,\n };\n const tag =\n OPTIONS_ARG_POSITION[method] === undefined\n ? undefined\n : effectiveRequestTag(tagFromArgs(method, args));\n if (tag !== undefined) rec.tag = tag;\n backend.calls.push(rec);\n return (outcome) => {\n if (\"error\" in outcome) rec.error = outcome.error;\n else rec.result = outcome.result;\n };\n };\n\n // Wrap so unimplemented members fail with actionable errors, registered\n // stubs are served, and surface-method calls are recorded (see\n // ./unsupported.ts).\n return wrapWithGuards(client, {\n getStub: (name) => backend.stubs.get(name),\n onCall,\n recorded: RECORDED_METHODS,\n });\n}\n"],"names":["uri","rest","resource","VERSIONS_PREFIX","RELEASE_DOC_PREFIX","promote"],"mappings":";;AAWO,MAAM,aAAa;AAGnB,SAAS,UAAU,SAAwB;AAChD,SAAO,IAAI,MAAM,GAAG,UAAU,IAAI,OAAO,EAAE;AAC7C;ACmBO,MAAM,0BAA0B,YAAY;AAAA,EACjD,YAAY,MAA4B;AACtC,UAAM,iBAAiB,IAAI,CAAC,GAC5B,KAAK,OAAO;AAAA,EACd;AACF;AAiBO,SAAS,iBAAiB,MAAmD;AAClF,QAAM,EAAE,QAAQ,KAAK,YAAY,eAAe,SAAS;AAEzD,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IAC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ACV,WAAW,SACP,QAAQ,MAAM,SACd,QAAQ,MAAM,OAAO,CAAC,MAAM,EAAE,WAAW,MAAM,EAAE;AAAA,IACvD,YAAY,MAAM;AAChB,cAAQ,MAAM,SAAS;AAAA,IACzB;AAAA,EAAA;AA6BF,SAAO,eAAe,QAAQ;AAAA,IAC5B,SAAS,CAAC,SAAS,QAAQ,MAAM,IAAI,IAAI;AAAA,IACzC,QA3Ba,CAAC,QAAgB,SAAiC;AAC/D,YAAM,MAAkB;AAAA,QACtB;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,GAAI,aAAa,SAAY,EAAE,SAAA,IAAa,CAAA;AAAA,QAC5C,GAAI,UAAU,SAAY,EAAE,MAAA,IAAU,CAAA;AAAA,QACtC;AAAA,MAAA,GAEI,MACJ,qBAAqB,MAAM,MAAM,SAC7B,SACA,oBAAoB,YAAY,QAAQ,IAAI,CAAC;AACnD,aAAI,QAAQ,WAAW,IAAI,MAAM,MACjC,QAAQ,MAAM,KAAK,GAAG,GACf,CAAC,YAAY;AACd,mBAAW,UAAS,IAAI,QAAQ,QAAQ,QACvC,IAAI,SAAS,QAAQ;AAAA,MAC5B;AAAA,IACF;AAAA,IAQE,UAAU;AAAA,EAAA,CACX;AACH;"}
1
+ {"version":3,"file":"index.js","sources":["../src/errors.ts","../src/resources.ts","../src/token-grants.ts","../src/history.ts","../src/platform-api.ts","../src/access-control.ts","../src/mutation-guard.ts","../src/patch.ts","../src/perspective.ts","../src/store.ts","../src/actions.ts","../src/transaction.ts","../src/request-url.ts","../src/internal-token.ts","../src/unsupported.ts","../src/client.ts"],"sourcesContent":["/**\n * Error helpers shared across the fake.\n *\n * Every error the fake throws is prefixed with {@link ERR_PREFIX} so it's\n * unmistakably attributable to *this fake* — not to `@sanity/client`,\n * groq-js, or the code under test — and carries enough context to fix\n * the problem without reading the fake's source. Short, bare messages\n * (\"not found\", \"invalid id\") are a poor developer experience in a test\n * double: prefer stating what happened, the offending value, and what to\n * do about it.\n */\nexport const ERR_PREFIX = \"[client-fake-for-test]\";\n\n/** Build a prefixed Error. Pass a fully-formed, dev-facing message. */\nexport function fakeError(message: string): Error {\n return new Error(`${ERR_PREFIX} ${message}`);\n}\n","/**\n * One capability table per resource type: how `@sanity/client` addresses\n * the resource, and what the platform serves or refuses there.\n *\n * Every value is taken from the staging capability probe (EDEX-2884): where\n * the platform says no, the entry holds the platform's own answer — status\n * and body — and the fake throws exactly that. Nothing here is policy of the\n * fake; adding a resource type means adding one row.\n */\n\nimport { ClientError, ServerError, type HttpError } from \"@sanity/client\";\n\nimport type { CurrentUser, TestClientIdentity } from \"./access-control.ts\";\nimport { fakeError } from \"./errors.ts\";\n\n/** Resource types `@sanity/client`'s `resource` config can name (the ones staging probed). */\nexport type ResourceType = \"dataset\" | \"media-library\" | \"canvas\" | \"dashboard\";\n\n/**\n * A resource, named exactly as `@sanity/client`'s `resource` config names\n * it: `{ type: \"media-library\", id: \"ml…\" }`, or `{ type: \"dataset\", id:\n * \"<projectId>.<dataset>\" }`.\n */\nexport interface TestClientResource {\n type: ResourceType;\n id: string;\n}\n\n/**\n * The platform's refusal, as the `ClientError` `@sanity/client` throws for\n * the same response: the fake hands `ClientError` the response the client\n * would have received, and `ClientError` derives `statusCode`, `message`,\n * `responseBody` and `details` from it. So `instanceof ClientError` holds for\n * the `@sanity/client` the consumer has installed.\n */\nexport class PlatformHttpError extends ClientError {\n constructor(args: PlatformResponseArgs) {\n super(platformResponse(args));\n this.name = \"PlatformHttpError\";\n }\n}\n\n/**\n * The platform's 5xx answer, as the `ServerError` `@sanity/client` throws\n * for a status of 500 or more (`src/http/request.ts`); see\n * {@link PlatformHttpError} for how the fields are derived.\n */\nexport class PlatformServerError extends ServerError {\n constructor(args: PlatformResponseArgs) {\n super(platformResponse(args));\n this.name = \"PlatformServerError\";\n }\n}\n\n/** What the platform answered, and to which request. */\ninterface PlatformResponseArgs {\n method: string;\n /** The URL `@sanity/client` requested; see {@link PlatformRequest.url}. */\n url: string;\n statusCode: number;\n statusMessage: string;\n body: unknown;\n}\n\n/**\n * The response `@sanity/client` reads an HTTP error from. The platform sends\n * JSON bodies as `application/json` and Express's text bodies as\n * `text/html`; `ClientError` only stringifies `responseBody` for the former.\n */\nexport function platformResponse(args: PlatformResponseArgs): HttpError[\"response\"] {\n const { method, url, statusCode, statusMessage, body } = args;\n const contentType = typeof body === \"string\" ? \"text/html\" : \"application/json\";\n return {\n body,\n url,\n method,\n headers: { \"content-type\": `${contentType}; charset=utf-8` },\n statusCode,\n statusMessage,\n };\n}\n\n/**\n * The Content Lake's 403 for a transaction it refuses: the cause wrapped in a\n * `mutationError`, as gradient's `convertMutationError` (a\n * `TransactionFailure`) and `proto_errors.DoError` write it.\n */\nexport function transactionRefused(\n request: PlatformRequest,\n cause: { description: string; type: string } & Record<string, unknown>,\n): HttpError[\"response\"] {\n return platformResponse({\n ...request,\n statusCode: 403,\n statusMessage: \"Forbidden\",\n body: {\n error: {\n description: `transaction failed: ${cause.description}`,\n items: [{ error: cause, index: 0 }],\n type: \"mutationError\",\n },\n },\n });\n}\n\n/** A request the platform answers. */\nexport interface PlatformRequest {\n method: \"GET\" | \"POST\";\n /** The API path the platform routes on, without host, API version or query string. */\n path: string;\n /**\n * The full URL `@sanity/client` requests, host, `/v<apiVersion>` and query\n * string included, which its errors carry as `response.url` (see\n * `./request-url.ts`).\n */\n url: string;\n}\n\n/**\n * The request a write error names when its caller gives none: an error built\n * outside any client has no host to name, so its URL is the API path alone.\n */\nexport const DEFAULT_WRITE: PlatformRequest = {\n method: \"POST\",\n path: \"/data/mutate\",\n url: \"/data/mutate\",\n};\n\n/** The platform's answer where it says no. */\nexport type Refusal = (request: PlatformRequest) => PlatformHttpError;\n\n/** Either the platform serves the capability, or it refuses with a fixed answer. */\nexport type Capability = \"served\" | Refusal;\n\n/** Express's 404 for a route the resource does not mount: `Cannot GET /dashboards/<id>/acl`. */\nconst notMounted: Refusal = ({ method, path, url }) =>\n new PlatformHttpError({\n method,\n url,\n statusCode: 404,\n statusMessage: \"Not Found\",\n body: `Cannot ${method} ${path}`,\n });\n\n/** An Access API or project API error: `{ statusCode, error, message }`, the body shape staging returns. */\nfunction accessApiError(args: {\n request: PlatformRequest;\n statusCode: number;\n statusMessage: string;\n message: string;\n}): PlatformHttpError {\n const { request, statusCode, statusMessage, message } = args;\n return new PlatformHttpError({\n ...request,\n statusCode,\n statusMessage,\n body: { statusCode, error: statusMessage, message },\n });\n}\n\n/**\n * The platform's answer to a token it has no session for — revoked, expired\n * or never issued — on every API, data and platform routes alike. Probed on\n * staging and production (EDEX-3204).\n */\nexport const sessionNotFound: Refusal = (request) =>\n new PlatformHttpError({\n ...request,\n statusCode: 401,\n statusMessage: \"Unauthorized\",\n body: {\n error: \"Unauthorized\",\n errorCode: \"SIO-401-ANF\",\n message: \"Session not found\",\n statusCode: 401,\n },\n });\n\n/**\n * The gateway's answer to a project-scoped token (a robot token) on another\n * project's API host, whatever the route: kong doorman `SIO-401-AWH`.\n * Probed on staging (EDEX-3204).\n */\nexport const sessionHostMismatch: Refusal = (request) =>\n new PlatformHttpError({\n ...request,\n statusCode: 401,\n statusMessage: \"Unauthorized\",\n body: {\n statusCode: 401,\n errorCode: \"SIO-401-AWH\",\n error: \"Unauthorized\",\n message: \"Session does not match project host\",\n },\n });\n\n/** The project API's 401 for a caller outside the project (populus `requireProjectMembership`). */\nexport const projectMembershipRequired: Refusal = (request) =>\n accessApiError({\n request,\n statusCode: 401,\n statusMessage: \"Unauthorized\",\n message: \"User must be a member of this project to perform this operation\",\n });\n\n/**\n * The project API's answer while it is down: populus's error handler sends\n * an unexpected error as Boom's 500 payload (`src/middleware/errorHandler.ts`),\n * and Boom hides the error's own message behind a fixed one\n * (`@hapi/boom` 10 `lib/index.js` `reformat`).\n */\nexport function projectApiDown(request: PlatformRequest): PlatformServerError {\n return new PlatformServerError({\n ...request,\n statusCode: 500,\n statusMessage: \"Internal Server Error\",\n body: {\n statusCode: 500,\n error: \"Internal Server Error\",\n message: \"An internal server error occurred\",\n },\n });\n}\n\n/** Media Library's 401 for a caller without `sanity.media-library.read` on the library. */\nconst mediaLibraryReadRequired: Refusal = (request) =>\n accessApiError({\n request,\n statusCode: 401,\n statusMessage: \"Unauthorized\",\n message:\n \"User must be able to read media libraries for this organization to perform this operation\",\n });\n\n/** The Access API's 403 when the caller lacks the permission; also the answer for unproven cells. */\nexport const noPermission: Refusal = (request) =>\n accessApiError({\n request,\n statusCode: 403,\n statusMessage: \"Forbidden\",\n message: \"You do not have permission to access this resource\",\n });\n\n/** The Access API's 400 for a resource type it does not know: `/access/bogustype/<id>/…`. */\nexport function invalidAccessType(request: PlatformRequest, type: string): PlatformHttpError {\n return accessApiError({\n request,\n statusCode: 400,\n statusMessage: \"Bad Request\",\n message: `Invalid resource type ${type}`,\n });\n}\n\n/** The Access API's 404 for an unknown id: `\"Canvas not found\"`, `\"User not found\"`. */\nexport function notFound(name: string): Refusal {\n return (request) =>\n accessApiError({\n request,\n statusCode: 404,\n statusMessage: \"Not Found\",\n message: `${name} not found`,\n });\n}\n\n/** Media Library's plan quota on releases: `releaseLimitExceededError`, limit 0. */\nconst releaseLimitExceeded: Refusal = ({ method, url }) =>\n new PlatformHttpError({\n method,\n url,\n statusCode: 403,\n statusMessage: \"Forbidden\",\n body: {\n error: {\n description: \"active release count exceeds the limit of 0\",\n type: \"releaseLimitExceededError\",\n },\n },\n });\n\n/** Throw the refusal, or run `serve` when the platform serves the capability. */\nexport function serveCapability<T>(\n capability: Capability,\n request: PlatformRequest,\n serve: () => T,\n): T {\n if (capability !== \"served\") throw capability(request);\n return serve();\n}\n\n/** The Access API's resource types (`/access/<type>/<id>/…`). */\nexport type AccessResourceType =\n | \"organization\"\n | \"project\"\n | \"media-library\"\n | \"canvas\"\n | \"dashboard\";\n\nexport interface ResourceCapabilities {\n /** How the fake's own error messages name the resource. */\n label: (id: string) => string;\n /** The data API base `@sanity/client`'s `resourceDataBase` builds for the resource. */\n dataBase: (id: string) => string;\n /** Match a request path on the data API base; `rest` is what follows it (`\"\"`, `/acl`, …). */\n matchBase: (path: string) => { id: string; rest: string } | undefined;\n /** Whether `GET <base>` answers the resource record (`{ organizationId, id, status }`). */\n lookupAtBase: boolean;\n /**\n * Whose id grant filters see as `identity()`: the project-scoped user\n * record for a dataset, the account-global one for every other resource.\n */\n identity: \"project\" | \"global\";\n /** `<base>/acl` → the caller's `Grant[]`. */\n acl: Capability;\n /**\n * Where `<base>/acl` is served: the answer for an id the registry does not\n * hold, and the Access API permission it needs. Absent where staging did not\n * probe it.\n */\n aclRefusals?: {\n unknown: Refusal;\n read?: { permission: string; refusal: Refusal };\n };\n /** `<base>/history/documents | transactions | events`. */\n history: Capability;\n /** `<base>/actions` — release, version and document actions. */\n actions: Capability;\n /** Action types refused where `/actions` is mounted. */\n refusedActions: Partial<Record<string, Refusal>>;\n /** The Access API addresses the resource as `/access/<type>/<id(resource id)>`. */\n access: { type: AccessResourceType; id: (id: string) => string };\n}\n\n/** An Access API resource: `/access/<type>/<id>`. */\nexport interface AccessResource {\n type: AccessResourceType;\n id: string;\n}\n\n/** What the Access API knows of the client family: its organization and the resources it holds. */\nexport interface AccessRegistry {\n organizationId: string | undefined;\n /** Every resource that exists: seeded or written to (a handle pointed at one does not create it). */\n resources: () => TestClientResource[];\n}\n\n/** One Access API resource type: what `/access/<type>/<id>/…` serves or refuses. */\nexport interface AccessCapabilities {\n /** `GET …/roles`. */\n roles: Capability;\n /** `GET …/users`. */\n users: Capability;\n /** `GET …/users/<sanityUserId>`. */\n user: Capability;\n /**\n * The permission served `users` and `user` need, checked against the\n * actor's `access` entry for the resource (what `user-permissions/me`\n * reports). When absent, no permission check runs.\n */\n usersPermission?: string;\n /** The permission served `roles` needs, checked like {@link usersPermission}. */\n rolesPermission?: string;\n /**\n * The answer for an id the family does not hold, on every endpoint but\n * `user-permissions/me/check` (which answers `false` even then). Absent\n * where staging did not probe it.\n */\n unknown?: { holds: (registry: AccessRegistry, id: string) => boolean; refusal: Refusal };\n /** The identity's role records on exactly this resource, or `undefined` when it is not a member. */\n memberRoles: (args: {\n identity: TestClientIdentity;\n id: string;\n }) => NonNullable<CurrentUser[\"roles\"]> | undefined;\n /**\n * Resources whose memberships a user row here lists besides its own: the\n * organization's users answer every membership in the organization.\n */\n owns?: (registry: AccessRegistry) => AccessResource[];\n}\n\nconst ACCESS_KEY_SEPARATOR = \":\";\n\n/** Key of an identity's `access` entry for a resource: `\"<access type>:<access id>\"`. */\nexport function accessKey(type: AccessResourceType, id: string): string {\n return `${type}${ACCESS_KEY_SEPARATOR}${id}`;\n}\n\n/** Match `/<segment>/<id>` and whatever follows. */\nfunction segmentBase(segment: string): ResourceCapabilities[\"matchBase\"] {\n const pattern = new RegExp(`^/${segment}/([^/]+)(/.*)?$`);\n return (path) => {\n const match = path.match(pattern);\n if (match === null) return undefined;\n return { id: decodeURIComponent(match[1] as string), rest: match[2] ?? \"\" };\n };\n}\n\nconst DATASET_BASE = /^\\/projects\\/([^/]+)\\/datasets\\/([^/]+)(\\/.*)?$/;\n\n/**\n * Split a dataset resource id, `\"<projectId>.<dataset>\"`, at its first dot.\n * Lenient on purpose: the fake builds these ids from any `(projectId,\n * dataset)` pair; caller-supplied ids are checked by {@link validateResource}.\n */\nexport function datasetParts(id: string): { projectId: string; dataset: string } {\n const dot = id.indexOf(\".\");\n return { projectId: id.slice(0, dot), dataset: id.slice(dot + 1) };\n}\n\n/**\n * Whether the identity is a member of the project: a project-scoped token\n * belongs to its own project only, any other identity to every project.\n */\nexport function isProjectMember(tokenProjectId: string | undefined, projectId: string): boolean {\n return tokenProjectId === undefined || tokenProjectId === projectId;\n}\n\n/** A non-project resource's roles live in the identity's `access` entry for it. */\nfunction accessEntryRoles(type: AccessResourceType): AccessCapabilities[\"memberRoles\"] {\n return ({ identity, id }) => identity.access?.[accessKey(type, id)]?.roles;\n}\n\nexport const RESOURCE_CAPABILITIES: Record<ResourceType, ResourceCapabilities> = {\n dataset: {\n label: (id) => {\n const { projectId, dataset } = datasetParts(id);\n return `dataset \"${projectId}:${dataset}\"`;\n },\n dataBase: (id) => {\n const { projectId, dataset } = datasetParts(id);\n return `/projects/${projectId}/datasets/${dataset}`;\n },\n matchBase: (path) => {\n const match = path.match(DATASET_BASE);\n if (match === null) return undefined;\n return { id: `${match[1]}.${match[2]}`, rest: match[3] ?? \"\" };\n },\n // A dataset's organization comes from its project: `GET /projects/<projectId>`.\n lookupAtBase: false,\n identity: \"project\",\n acl: \"served\",\n history: \"served\",\n actions: \"served\",\n refusedActions: {},\n access: { type: \"project\", id: (id) => datasetParts(id).projectId },\n },\n \"media-library\": {\n label: (id) => `media library \"${id}\"`,\n dataBase: (id) => `/media-libraries/${id}`,\n matchBase: segmentBase(\"media-libraries\"),\n lookupAtBase: true,\n identity: \"global\",\n acl: \"served\",\n aclRefusals: {\n unknown: notFound(\"Media library\"),\n read: { permission: \"sanity.media-library.read\", refusal: mediaLibraryReadRequired },\n },\n history: \"served\",\n actions: \"served\",\n refusedActions: { \"sanity.action.release.create\": releaseLimitExceeded },\n access: { type: \"media-library\", id: (id) => id },\n },\n canvas: {\n label: (id) => `canvas \"${id}\"`,\n dataBase: (id) => `/canvases/${id}`,\n matchBase: segmentBase(\"canvases\"),\n lookupAtBase: true,\n identity: \"global\",\n acl: \"served\",\n aclRefusals: { unknown: notFound(\"Resource\") },\n history: \"served\",\n actions: notMounted,\n refusedActions: {},\n access: { type: \"canvas\", id: (id) => id },\n },\n dashboard: {\n label: (id) => `dashboard \"${id}\"`,\n dataBase: (id) => `/dashboards/${id}`,\n matchBase: segmentBase(\"dashboards\"),\n lookupAtBase: true,\n identity: \"global\",\n acl: notMounted,\n history: notMounted,\n actions: notMounted,\n refusedActions: {},\n access: { type: \"dashboard\", id: (id) => id },\n },\n};\n\n/** The capability row for `type`, or a fake error naming the supported types. */\nexport function capabilitiesOf(type: string): ResourceCapabilities {\n // Own keys only: `toString` is no more a resource type than `bogustype`.\n const row = Object.hasOwn(RESOURCE_CAPABILITIES, type)\n ? RESOURCE_CAPABILITIES[type as ResourceType]\n : undefined;\n if (row === undefined) {\n throw fakeError(\n `Unsupported resource type ${JSON.stringify(type)}. The fake serves ` +\n `${Object.keys(RESOURCE_CAPABILITIES)\n .map((t) => `\"${t}\"`)\n .join(\", \")}.`,\n );\n }\n return row;\n}\n\n/** Validate a `resource` config the way `@sanity/client` does, returning its capability row. */\nexport function validateResource(resource: TestClientResource): ResourceCapabilities {\n const row = capabilitiesOf(resource.type);\n if (resource.type === \"dataset\" && resource.id.split(\".\").length !== 2) {\n throw fakeError(\n `Dataset resource ID must be in the format \"project.dataset\" (got ${JSON.stringify(resource.id)}), ` +\n \"the same rule @sanity/client applies to `resource: { type: 'dataset', id }`.\",\n );\n }\n return row;\n}\n\n/** Find the resource a data API path addresses, by trying every row's base. */\nexport function matchResourcePath(\n path: string,\n): { resource: TestClientResource; rest: string } | undefined {\n for (const [type, row] of Object.entries(RESOURCE_CAPABILITIES)) {\n const match = row.matchBase(path);\n if (match !== undefined) {\n return { resource: { type: type as ResourceType, id: match.id }, rest: match.rest };\n }\n }\n return undefined;\n}\n\n/** The Access API resource a registry resource is: a dataset is its project. */\nfunction accessResourceOf(resource: TestClientResource): AccessResource {\n const { access } = capabilitiesOf(resource.type);\n return { type: access.type, id: access.id(resource.id) };\n}\n\n/** Whether the registry holds an Access API resource of `type` with `id`. */\nexport function holdsResource(\n type: AccessResourceType,\n): (registry: AccessRegistry, id: string) => boolean {\n return (registry, id) =>\n registry.resources().some((resource) => {\n const access = accessResourceOf(resource);\n return access.type === type && access.id === id;\n });\n}\n\nexport const ACCESS_CAPABILITIES: Record<AccessResourceType, AccessCapabilities> = {\n organization: {\n // Proven on staging, but outside what the fake serves: refused like a missing permission.\n roles: noPermission,\n users: \"served\",\n user: \"served\",\n // Granted by the built-in organization `member` role.\n usersPermission: \"sanity.organization.members.read\",\n unknown: {\n holds: (registry, id) => id === registry.organizationId,\n refusal: notFound(\"Organization\"),\n },\n memberRoles: accessEntryRoles(\"organization\"),\n // Every resource of the family belongs to its organization (see `organizationId`).\n owns: (registry) => {\n const byKey = new Map<string, AccessResource>();\n for (const resource of registry.resources()) {\n const access = accessResourceOf(resource);\n byKey.set(accessKey(access.type, access.id), access);\n }\n return [...byKey.values()];\n },\n },\n project: {\n roles: \"served\",\n // Also gates `GET /projects/<id>/roles`; the stock user roles all hold it on staging.\n rolesPermission: \"sanity.project.roles.read\",\n users: \"served\",\n // Single-user reads are served only where the fake checks members read (the\n // organization); elsewhere they refuse rather than answer unchecked.\n user: noPermission,\n // A member holds its project roles; see `isProjectMember`.\n memberRoles: ({ identity, id }) =>\n isProjectMember(identity.projectId, id)\n ? ((identity.usersByProject?.[id] ?? identity.user).roles ?? [])\n : undefined,\n },\n \"media-library\": {\n // Staging refuses it only because no probe token held roles read.\n roles: \"served\",\n rolesPermission: \"sanity.media-library.roles.read\",\n users: \"served\",\n // Unproven on staging (only the listing was probed); refused as on `project`.\n user: noPermission,\n unknown: { holds: holdsResource(\"media-library\"), refusal: notFound(\"Media library\") },\n memberRoles: accessEntryRoles(\"media-library\"),\n },\n canvas: {\n // Unproven on staging (no canvas roles/members permission exists to grant).\n roles: noPermission,\n users: noPermission,\n user: noPermission,\n unknown: { holds: holdsResource(\"canvas\"), refusal: notFound(\"Canvas\") },\n memberRoles: accessEntryRoles(\"canvas\"),\n },\n dashboard: {\n // Unproven on staging, as for canvas.\n roles: noPermission,\n users: noPermission,\n user: noPermission,\n unknown: { holds: holdsResource(\"dashboard\"), refusal: notFound(\"Dashboard\") },\n memberRoles: accessEntryRoles(\"dashboard\"),\n },\n};\n\n/** The Access API row for `type`, if the Access API knows the type. */\nexport function accessCapabilitiesOf(type: string): AccessCapabilities | undefined {\n // Own keys only: `toString` or `constructor` is as unknown to the platform as `bogustype`.\n return Object.hasOwn(ACCESS_CAPABILITIES, type)\n ? ACCESS_CAPABILITIES[type as AccessResourceType]\n : undefined;\n}\n\n/**\n * `@sanity/client`'s `getDataUrl(operation, path)`: a `resource`-configured\n * handle addresses `<resource base>/<operation>/<path>`, a plain dataset\n * handle `/data/<operation>/<dataset>/<path>`.\n */\nexport function dataUrl(args: {\n resource: TestClientResource | undefined;\n dataset: string;\n operation: string;\n path?: string | undefined;\n}): string {\n const { resource, dataset, operation, path } = args;\n if (resource !== undefined) {\n const uri = path !== undefined ? `${operation}/${path}` : operation;\n return `${capabilitiesOf(resource.type).dataBase(resource.id)}/${uri}`.replace(\n /\\/($|\\?)/,\n \"$1\",\n );\n }\n const base = `/${operation}/${dataset}`;\n const uri = path !== undefined ? `${base}/${path}` : base;\n return `/data${uri}`.replace(/\\/($|\\?)/, \"$1\");\n}\n","/**\n * The grants the Content Lake refuses in a token.\n *\n * The Content Lake parses a token's grants on every request it serves\n * (gradient `pkg/server/httpapi/middlewares/sessions/session.go`\n * `ParseClaimFromToken`) and answers the whole request with 400 when one\n * does not parse. A token grant is a path and its permissions (gradient\n * `pkg/security/tokens.go` `deserializeGrant`): the path must parse\n * (`pkg/documents/paths/paths.go` `Parse`) and every permission must be one\n * the Content Lake knows (`pkg/security/permissions.go`\n * `PermissionFromString`). Filter grants come from the project's ACL, not\n * from the token, and are not parsed there.\n */\n\nimport type { Grant } from \"./access-control.ts\";\nimport { PlatformHttpError, type PlatformRequest } from \"./resources.ts\";\n\n/** `paths.MaxPathLength`, counted in UTF-8 bytes. */\nconst MAX_PATH_BYTES = 128;\n\n/** `security.AllPermissions`. */\nconst PERMISSIONS = new Set([\"read\", \"update\", \"create\", \"manage\", \"history\", \"editHistory\"]);\n\n/** A wildcard element and gradient's message when it is not the last element. */\nconst MISPLACED_WILDCARD = new Map([\n [\"*\", \"wildcard must be last component\"],\n [\"**\", \"descendant wildcard must be last component\"],\n]);\n\n/** `validElementLine`: ASCII letters, digits, `_` and `-`, not starting with `-`. */\nconst ELEMENT_LINE = /^[A-Za-z0-9_][A-Za-z0-9_-]*$/;\n\n/**\n * `validElement`: gradient accepts an element when any one of its\n * newline-separated lines is valid, a quirk it keeps for existing ids.\n */\nfunction validElement(element: string): boolean {\n return element.split(\"\\n\").some((line) => ELEMENT_LINE.test(line));\n}\n\n/** Go's `\\a` … `\\v` escapes, which `strconv.Quote` writes for these control characters. */\nconst GO_ESCAPES: Record<string, string> = {\n \"\\u0007\": \"\\\\a\",\n \"\\b\": \"\\\\b\",\n \"\\f\": \"\\\\f\",\n \"\\n\": \"\\\\n\",\n \"\\r\": \"\\\\r\",\n \"\\t\": \"\\\\t\",\n \"\\v\": \"\\\\v\",\n '\"': '\\\\\"',\n \"\\\\\": \"\\\\\\\\\",\n};\n\n/** Letters, marks, numbers, punctuation, symbols and the ASCII space: what Go's `IsPrint` keeps. */\nconst GO_PRINTABLE = /^[\\p{L}\\p{M}\\p{N}\\p{P}\\p{S} ]$/u;\n\n/**\n * Go's `%q` (and `%#v`) of a string, `strconv.Quote`: printable characters\n * as they are, the named control escapes, then `\\xNN`, `\\uNNNN` or\n * `\\UNNNNNNNN` for everything else.\n */\nexport function quoted(value: string): string {\n let out = '\"';\n for (const char of value) {\n const escape = GO_ESCAPES[char];\n if (escape !== undefined) {\n out += escape;\n continue;\n }\n if (GO_PRINTABLE.test(char)) {\n out += char;\n continue;\n }\n const code = char.codePointAt(0) as number;\n out +=\n code < 0x80\n ? `\\\\x${code.toString(16).padStart(2, \"0\")}`\n : code <= 0xffff\n ? `\\\\u${code.toString(16).padStart(4, \"0\")}`\n : `\\\\U${code.toString(16).padStart(8, \"0\")}`;\n }\n return `${out}\"`;\n}\n\n/**\n * Gradient's message for the first rule `path` breaks, checked in\n * `Parse`'s order, or `undefined` when the path parses.\n */\nexport function pathError(path: string): string | undefined {\n if (path === \"\") return \"path must contain at least one element\";\n if (path.startsWith(\".\")) return 'path cannot begin with delimiter \".\"';\n const elements = path.split(\".\");\n if (elements.includes(\"\")) return \"path contains empty element\";\n if (new TextEncoder().encode(path).length > MAX_PATH_BYTES) {\n return `path exceeds maximum length of ${MAX_PATH_BYTES}`;\n }\n const last = elements.length - 1;\n for (const [index, element] of elements.entries()) {\n const misplaced = MISPLACED_WILDCARD.get(element);\n if (misplaced !== undefined) {\n if (index < last) return misplaced;\n } else if (!validElement(element)) {\n return `invalid element ${quoted(element)}`;\n }\n }\n return undefined;\n}\n\n/**\n * Whether the path glob `path` includes the document id `id`, as gradient's\n * `Path.Includes` decides for `_id in path(...)`: elements compare\n * literally, `*` matches exactly one element and a trailing `**` one or\n * more. groq-js's `path()` is not used: it matches `.` as any character.\n */\nexport function pathIncludes(path: string, id: string): boolean {\n const pattern = path.split(\".\");\n const elements = id.split(\".\");\n for (const [index, element] of pattern.entries()) {\n if (index >= elements.length) return false;\n if (element === \"**\") return true;\n // `*` covers one element, but not a `**` one.\n if (element === \"*\" ? elements[index] === \"**\" : element !== elements[index]) return false;\n }\n return pattern.length === elements.length;\n}\n\n/** Gradient's message for the first grant in `grants` it cannot parse, or `undefined`. */\nexport function grantsError(grants: readonly Grant[]): string | undefined {\n for (const grant of grants) {\n if (!(\"path\" in grant)) continue;\n const error = pathError(grant.path);\n if (error !== undefined) return error;\n const invalid = grant.permissions.find((permission) => !PERMISSIONS.has(permission));\n if (invalid !== undefined) return `invalid permission ${quoted(invalid)}`;\n }\n return undefined;\n}\n\n/**\n * The Content Lake's answer to `request` when its token carries a grant it\n * cannot parse: 400 with an `httpBadRequest` error describing the first\n * such grant, as `httputils.WriteError` writes `proto.HTTPBadRequest`.\n * `undefined` when every grant parses.\n */\nexport function refusedToken(\n request: PlatformRequest,\n grants: readonly Grant[],\n): PlatformHttpError | undefined {\n const description = grantsError(grants);\n return description === undefined ? undefined : badRequest(request, description);\n}\n\n/** `proto.HTTPBadRequest{Description}`, as `httputils.WriteError` writes it. */\nexport function badRequest(request: PlatformRequest, description: string): PlatformHttpError {\n return new PlatformHttpError({\n ...request,\n statusCode: 400,\n statusMessage: \"Bad Request\",\n body: { error: { description, type: \"httpBadRequest\" } },\n });\n}\n","/**\n * Document history for one store — the fake's mirror of the History API\n * (`<data base>/history/documents | transactions | events`).\n *\n * Every committed write is recorded as one transaction holding the net\n * change of each document it touched. Revisions are kept by `_rev`, so a\n * document can be read at any revision it has held since the client was\n * created, plus the revision it holds now (seeded documents included).\n */\n\nimport type { SanityDocument } from \"@sanity/types\";\n\n/** One document's net change in a committed transaction. */\ninterface HistoryEffect {\n documentId: string;\n /** The document after the transaction; `undefined` when it was deleted. */\n after: SanityDocument | undefined;\n /** Whether the document existed before the transaction. */\n existed: boolean;\n}\n\ninterface HistoryTransaction {\n id: string;\n timestamp: string;\n author: string | undefined;\n effects: HistoryEffect[];\n}\n\n/** A transaction as `GET <data base>/history/transactions/<ids>` serves it, one per NDJSON line. */\nexport interface HistoryTransactionRecord {\n id: string;\n timestamp: string;\n author?: string;\n mutations: (\n | { create: SanityDocument }\n | { createOrReplace: SanityDocument }\n | { delete: { id: string } }\n )[];\n documentIDs: string[];\n}\n\n/** An event as `GET <data base>/history/events/documents/<ids>` serves it. */\nexport interface HistoryEvent {\n type: \"createLiveDocument\" | \"updateLiveDocument\";\n id: string;\n documentId: string;\n revisionId: string;\n timestamp: string;\n author?: string;\n}\n\nexport interface DocumentHistory {\n /**\n * Record a committed write: compare the images from before the write with\n * the store after it, and keep one transaction for the documents that\n * changed. A write that changed nothing records nothing.\n */\n commit: (args: {\n transactionId: string;\n author: string | undefined;\n before: ReadonlyMap<string, SanityDocument>;\n after: readonly SanityDocument[];\n }) => void;\n /** `?revision=<rev>`: the listed documents as they were at that revision. */\n documentsAt: (args: {\n ids: readonly string[];\n revision: string;\n current: readonly SanityDocument[];\n }) => SanityDocument[];\n /** Every document id the history has recorded a change for. */\n documentIds: () => string[];\n /** The last image `id` held before it was deleted, or `undefined` if it never changed. */\n lastImage: (id: string) => SanityDocument | undefined;\n /**\n * Transactions touching any of `ids`, oldest first. Each record keeps only\n * the effects on documents `visible` lets through, so a transaction shared\n * with a document the reader cannot see does not reveal it.\n */\n transactions: (\n ids: readonly string[],\n visible: (documentId: string) => boolean,\n ) => HistoryTransactionRecord[];\n /** Live-document events for each of `ids`, newest first. */\n events: (ids: readonly string[]) => Record<string, HistoryEvent[]>;\n}\n\nexport function createDocumentHistory(): DocumentHistory {\n const transactions: HistoryTransaction[] = [];\n /** Every image a document has held, keyed `<id> <rev>`. */\n const revisions = new Map<string, SanityDocument>();\n /** Every image is a copy, so neither the store nor a caller's edits rewrite history. */\n const keep = (doc: SanityDocument) =>\n revisions.set(`${doc._id} ${doc._rev}`, structuredClone(doc));\n /** The last image each deleted document held. */\n const deleted = new Map<string, SanityDocument>();\n\n return {\n commit: ({ transactionId, author, before, after }) => {\n const afterById = new Map(after.map((doc) => [doc._id, doc]));\n const ids = new Set([...before.keys(), ...afterById.keys()]);\n const effects: HistoryEffect[] = [];\n for (const id of ids) {\n const previous = before.get(id);\n const next = afterById.get(id);\n if (previous === next) continue;\n if (previous !== undefined) keep(previous);\n if (next !== undefined) keep(next);\n if (next === undefined && previous !== undefined)\n deleted.set(id, structuredClone(previous));\n else deleted.delete(id);\n effects.push({\n documentId: id,\n after: next === undefined ? undefined : structuredClone(next),\n existed: previous !== undefined,\n });\n }\n if (effects.length === 0) return;\n transactions.push({\n id: transactionId,\n timestamp: new Date().toISOString(),\n author,\n effects,\n });\n },\n\n documentsAt: ({ ids, revision, current }) =>\n ids.flatMap((id) => {\n const image =\n revisions.get(`${id} ${revision}`) ??\n current.find((doc) => doc._id === id && doc._rev === revision);\n return image === undefined ? [] : [structuredClone(image)];\n }),\n\n lastImage: (id) => deleted.get(id),\n\n documentIds: () => [\n ...new Set(transactions.flatMap((txn) => txn.effects.map((effect) => effect.documentId))),\n ],\n\n transactions: (ids, visible) =>\n transactions\n .filter((txn) => txn.effects.some((effect) => ids.includes(effect.documentId)))\n .map((txn) => ({ ...txn, effects: txn.effects.filter((e) => visible(e.documentId)) }))\n .map((txn) => ({\n id: txn.id,\n timestamp: txn.timestamp,\n ...(txn.author !== undefined ? { author: txn.author } : {}),\n mutations: txn.effects.map((effect) => {\n if (effect.after === undefined) return { delete: { id: effect.documentId } };\n const after = structuredClone(effect.after);\n return effect.existed ? { createOrReplace: after } : { create: after };\n }),\n documentIDs: txn.effects.map((effect) => effect.documentId),\n })),\n\n events: (ids) =>\n Object.fromEntries(\n ids.map((id) => [\n id,\n transactions\n .flatMap((txn) =>\n txn.effects\n .filter((effect) => effect.documentId === id && effect.after !== undefined)\n .map(\n (effect): HistoryEvent => ({\n type: effect.existed ? \"updateLiveDocument\" : \"createLiveDocument\",\n id: txn.id,\n documentId: id,\n revisionId: (effect.after as SanityDocument)._rev,\n timestamp: txn.timestamp,\n ...(txn.author !== undefined ? { author: txn.author } : {}),\n }),\n ),\n )\n .toReversed(),\n ]),\n ),\n };\n}\n\n/** Split `documents/<ids>` style id lists: comma-separated, URI-encoded. */\nexport function historyIds(list: string): string[] {\n return list.split(\",\").map((id) => decodeURIComponent(id));\n}\n","/**\n * The platform endpoints `client.request()` serves beyond identity: the\n * Access API (`/access/<type>/<id>/…`, routed through `ACCESS_CAPABILITIES`),\n * the project role catalog (`/projects/<id>/roles`, gated by the same table's\n * project row), the project's features (`/projects/<id>/features`), the\n * caller's organization attributes, resource lookup (`GET <data\n * base>`), ACL (`<data base>/acl`) and the History API (`<data\n * base>/history/…`, or `/data/history/<dataset>/…` on a plain dataset\n * handle). Routing goes through the capability table: a capability the\n * resource refuses throws the platform's own answer.\n */\n\nimport type { SanityDocument } from \"@sanity/types\";\n\nimport type {\n CurrentUser,\n Grant,\n ResourceAccess,\n TestClientIdentity,\n UserAttribute,\n UserAttributeValue,\n} from \"./access-control.ts\";\nimport { fakeError } from \"./errors.ts\";\nimport { historyIds, type DocumentHistory } from \"./history.ts\";\nimport {\n accessCapabilitiesOf,\n accessKey,\n ACCESS_CAPABILITIES,\n capabilitiesOf,\n holdsResource,\n invalidAccessType,\n isProjectMember,\n matchResourcePath,\n noPermission,\n notFound,\n projectMembershipRequired,\n serveCapability,\n type AccessRegistry,\n type AccessResource,\n type AccessResourceType,\n type Capability,\n type PlatformRequest,\n type ResourceCapabilities,\n type TestClientResource,\n} from \"./resources.ts\";\n\n/** What the handle answers for its own resource: the actor's grants, and the history it may read. */\nexport interface OwnResource {\n grants: Grant[];\n documents: () => readonly SanityDocument[];\n history: DocumentHistory;\n /** The actor's read gate; history serves only documents it lets through. */\n canRead: (document: SanityDocument) => Promise<boolean>;\n /** Throws the Content Lake's refusal of the actor's token; the History API is the Content Lake's. */\n admitToLake: (request: PlatformRequest) => void;\n}\n\n/** Everything {@link servePlatformRequest} needs from the owning handle. */\nexport interface PlatformApiContext {\n /** The handle's projectId, for `/data/history/<dataset>` paths. */\n projectId: string;\n /** The organization every resource of the family belongs to, if configured. */\n organizationId: string | undefined;\n /** The features each project has, keyed by projectId. */\n projectFeatures: Record<string, string[]>;\n /**\n * The handle's own resource, or `undefined` for any other: a token's\n * grants and history answer for the resource the handle addresses.\n */\n own: (resource: TestClientResource) => OwnResource | undefined;\n /** The acting identity's account-global user record (its `sanityUserId`). */\n actorUser: () => CurrentUser;\n /** The acting identity's Access API entries, keyed `\"<access type>:<id>\"`. */\n actorAccess: () => Record<string, ResourceAccess> | undefined;\n /** The project the acting identity's token belongs to, if it is project-scoped. */\n actorProjectId: () => string | undefined;\n /** The handle's members (registered identities, plus its inline `accessControl` actor), for member and role listings. */\n identities: () => TestClientIdentity[];\n /** Every resource that exists: seeded or written to (a handle pointed at one does not create it). */\n resources: () => TestClientResource[];\n}\n\n/** `{ value }` when the platform answers the path, `undefined` when the fake has no handler for it. */\ntype Served = { value: unknown } | undefined;\n\nconst ACCESS_PATH = /^\\/access\\/([^/]+)\\/([^/]+)(\\/.*)?$/;\nconst ACCESS_USER_PATH = /^\\/users\\/([^/]+)$/;\nconst ACCESS_ROLE_PATH = /^\\/roles\\/([^/]+)$/;\nconst PROJECT_ROLES_PATH = /^\\/projects\\/([^/]+)\\/roles$/;\nconst PROJECT_FEATURES_PATH = /^\\/projects\\/([^/]+)\\/features$/;\nconst MY_ATTRIBUTES_PATH = /^\\/organizations\\/([^/]+)\\/users\\/me\\/attributes$/;\nconst DATA_HISTORY_PATH = /^\\/data\\/history\\/([^/]+)(\\/.*)$/;\n\n/** The paths `request()` serves that populus answers, besides resource lookup and ACL. */\nconst PROJECT_API_PATHS = [\n /^\\/users\\/me$/,\n /^\\/projects\\/[^/]+$/,\n /^\\/projects\\/[^/]+\\/users\\/[^/]+$/,\n PROJECT_ROLES_PATH,\n PROJECT_FEATURES_PATH,\n MY_ATTRIBUTES_PATH,\n ACCESS_PATH,\n];\n\n/**\n * Whether populus (the project API) answers `GET <path>`: `/users/me`, the\n * project, organization and Access APIs, a resource's lookup and the ACL of a\n * resource that mounts one. The Content Lake answers the data and History\n * APIs, and a route a resource does not mount is Express's 404 before any\n * handler runs (kong `config/common/kong.jsonnet` routes).\n */\nexport function servedByProjectApi(path: string): boolean {\n if (PROJECT_API_PATHS.some((pattern) => pattern.test(path))) return true;\n const matched = matchResourcePath(path);\n if (matched === undefined) return false;\n const row = capabilitiesOf(matched.resource.type);\n if (matched.rest === \"\") return row.lookupAtBase;\n return matched.rest === \"/acl\" && row.acl === \"served\";\n}\n\n/** An Access API listing page; the fake serves everything on one page. */\nfunction page<T>(data: T[]): { data: T[]; nextCursor: null } {\n return { data, nextCursor: null };\n}\n\n/** A users listing page, which also counts the users. */\nfunction usersPage<T>(data: T[]): { data: T[]; nextCursor: null; totalCount: number } {\n return { ...page(data), totalCount: data.length };\n}\n\n/**\n * The platform's profile fields the fake does not model: every user signs in\n * with `sanity` and was created at the epoch. `email` is required too, and is\n * empty where the identity seeds none.\n */\nconst UNMODELLED_PROFILE = { provider: \"sanity\", createdAt: new Date(0).toISOString() } as const;\n\n/** Serve `request` if its path is a platform endpoint the table knows; `uri` is the URI as the caller passed it. */\nexport async function servePlatformRequest(\n ctx: PlatformApiContext,\n uri: string,\n request: PlatformRequest,\n): Promise<Served> {\n const { path } = request;\n const queryStart = uri.indexOf(\"?\");\n const query = new URLSearchParams(queryStart === -1 ? \"\" : uri.slice(queryStart + 1));\n\n const myAttributes = path.match(MY_ATTRIBUTES_PATH);\n if (myAttributes !== null) {\n const organizationId = decodeURIComponent(myAttributes[1] as string);\n return serveMyAttributes({ ctx, request, organizationId, query });\n }\n\n const projectRoles = path.match(PROJECT_ROLES_PATH);\n if (projectRoles !== null) {\n const projectId = decodeURIComponent(projectRoles[1] as string);\n return serveProjectRoles({ ctx, request, projectId });\n }\n\n const projectFeatures = path.match(PROJECT_FEATURES_PATH);\n if (projectFeatures !== null) {\n const projectId = decodeURIComponent(projectFeatures[1] as string);\n if (!isProjectMember(ctx.actorProjectId(), projectId)) {\n throw projectMembershipRequired(request);\n }\n return { value: featuresOf(ctx, projectId) };\n }\n\n const access = path.match(ACCESS_PATH);\n if (access !== null) {\n const [, type, id, rest] = access as [string, string, string, string | undefined];\n return serveAccess({ ctx, request, type, id: decodeURIComponent(id), rest: rest ?? \"\", query });\n }\n\n const dataHistory = path.match(DATA_HISTORY_PATH);\n if (dataHistory !== null) {\n const [, dataset, rest] = dataHistory as [string, string, string];\n const resource: TestClientResource = { type: \"dataset\", id: `${ctx.projectId}.${dataset}` };\n return serveHistory({ ctx, request, resource, rest, query });\n }\n\n const matched = matchResourcePath(path);\n if (matched === undefined) return undefined;\n const { resource, rest } = matched;\n const row = capabilitiesOf(resource.type);\n if (rest === \"\") return row.lookupAtBase ? serveLookup(ctx, resource) : undefined;\n if (rest === \"/acl\") {\n return serveCapability(row.acl, request, () => {\n refuseAcl({ ctx, request, resource, row });\n const own = ctx.own(resource);\n return own === undefined ? undefined : { value: own.grants };\n });\n }\n if (rest.startsWith(\"/history/\")) {\n return serveHistory({ ctx, request, resource, rest: rest.slice(\"/history\".length), query });\n }\n return undefined;\n}\n\n/**\n * Throw the platform's refusal of `GET <path>` when it is a resource's ACL\n * the actor may not read: a route the resource does not mount, an unknown id,\n * or a missing read permission. An explicit `aclPath` alias runs it too.\n */\nexport function refuseAclRequest(ctx: PlatformApiContext, request: PlatformRequest): void {\n const matched = matchResourcePath(request.path);\n if (matched?.rest !== \"/acl\") return;\n const row = capabilitiesOf(matched.resource.type);\n serveCapability(row.acl, request, () =>\n refuseAcl({ ctx, request, resource: matched.resource, row }),\n );\n}\n\n/**\n * Throw the platform's refusal of `<base>/acl` on a resource it serves: an id\n * the registry does not hold, then an actor without the read permission.\n */\nfunction refuseAcl(args: {\n ctx: PlatformApiContext;\n request: PlatformRequest;\n resource: TestClientResource;\n row: ResourceCapabilities;\n}): void {\n const { ctx, request, resource, row } = args;\n if (row.aclRefusals === undefined) return;\n const type = row.access.type;\n const id = row.access.id(resource.id);\n const registry: AccessRegistry = { organizationId: ctx.organizationId, resources: ctx.resources };\n if (!holdsResource(type)(registry, id)) throw row.aclRefusals.unknown(request);\n const { read } = row.aclRefusals;\n if (read === undefined) return;\n const permissions = ctx.actorAccess()?.[accessKey(type, id)]?.permissions ?? [];\n if (!permissions.includes(read.permission)) throw read.refusal(request);\n}\n\n/**\n * `GET /projects/<id>/features`: the project's feature names, a bare array,\n * sorted and once each as the project API answers them. Only a project\n * member may read them (see `isProjectMember`).\n */\nfunction featuresOf(ctx: PlatformApiContext, projectId: string): string[] {\n const features = Object.hasOwn(ctx.projectFeatures, projectId)\n ? (ctx.projectFeatures[projectId] as string[])\n : [];\n return [...new Set(features)].toSorted();\n}\n\n/** `GET /media-libraries/<id>`, `/canvases/<id>`, `/dashboards/<id>`: the resource record. */\nfunction serveLookup(ctx: PlatformApiContext, resource: TestClientResource): Served {\n if (ctx.organizationId === undefined) {\n throw fakeError(\n `request(): GET ${capabilitiesOf(resource.type).dataBase(resource.id)} answers the ` +\n \"resource's organization, but no organization is configured. Pass \" +\n \"createTestClient({ organizationId }).\",\n );\n }\n return { value: { organizationId: ctx.organizationId, id: resource.id, status: \"active\" } };\n}\n\nfunction serveHistory(args: {\n ctx: PlatformApiContext;\n request: PlatformRequest;\n resource: TestClientResource;\n rest: string;\n query: URLSearchParams;\n}): Promise<Served> | undefined {\n const { ctx, request, resource, rest, query } = args;\n return serveCapability(capabilitiesOf(resource.type).history, request, () => {\n const own = ctx.own(resource);\n if (own === undefined) return undefined;\n own.admitToLake(request);\n return readHistory({ own, request, rest, query });\n });\n}\n\n/** Answer a History API path from the handle's own history, hiding what the actor cannot read. */\nasync function readHistory(args: {\n own: OwnResource;\n request: PlatformRequest;\n rest: string;\n query: URLSearchParams;\n}): Promise<Served> {\n const { own, request, rest, query } = args;\n // The read grant filters the document as it is now or, once deleted, as it last was.\n const current = new Map(own.documents().map((doc) => [doc._id, doc]));\n const hidden = new Set<string>();\n for (const id of new Set([...current.keys(), ...own.history.documentIds()])) {\n const image = current.get(id) ?? own.history.lastImage(id);\n if (image !== undefined && !(await own.canRead(image))) hidden.add(id);\n }\n const visible = (id: string) => !hidden.has(id);\n const readable = (list: string): string[] => historyIds(list).filter(visible);\n\n const documents = rest.match(/^\\/documents\\/([^/]+)$/);\n if (documents !== null) {\n const revision = query.get(\"revision\");\n if (revision === null) {\n throw fakeError(\n `request(): ${request.path} is served for \\`?revision=<rev>\\` reads only ` +\n \"(time-based history reads are not simulated).\",\n );\n }\n const ids = readable(documents[1] as string);\n return {\n value: { documents: own.history.documentsAt({ ids, revision, current: own.documents() }) },\n };\n }\n const transactions = rest.match(/^\\/transactions\\/([^/]+)$/);\n if (transactions !== null) {\n // NDJSON, one transaction per line, as the History API streams it.\n const records = own.history.transactions(readable(transactions[1] as string), visible);\n return { value: records.map((record) => JSON.stringify(record)).join(\"\\n\") };\n }\n const events = rest.match(/^\\/events\\/documents\\/([^/]+)$/);\n if (events !== null) {\n return {\n value: {\n events: own.history.events(readable(events[1] as string)),\n nextCursor: \"\",\n },\n };\n }\n return undefined;\n}\n\n/** One membership of an Access API user row: the roles held on one resource. */\ninterface AccessMembership {\n resourceType: AccessResourceType;\n resourceId: string;\n roleNames: string[];\n}\n\n/** An Access API user row: the account-global id, a profile, and its memberships. */\ninterface AccessUserRecord {\n sanityUserId: string;\n profile: {\n id: string;\n displayName: string;\n email: string;\n provider: string;\n createdAt: string;\n };\n memberships: AccessMembership[];\n}\n\n/** One `user-permissions/me` row. */\ninterface PermissionRecord {\n type: string;\n resourceType: AccessResourceType;\n resourceId: string;\n action: string;\n}\n\n/** Serve the capability when the actor holds `permission` (if the row names one), else 403. */\nfunction serveGated<T>(args: {\n capability: Capability;\n permission: string | undefined;\n held: readonly string[];\n request: PlatformRequest;\n serve: () => T;\n}): T {\n const { capability, permission, held, request, serve } = args;\n return serveCapability(capability, request, () => {\n if (permission !== undefined && !held.includes(permission)) throw noPermission(request);\n return serve();\n });\n}\n\n/**\n * `GET /projects/<id>/roles`: the project role catalog, a bare array of\n * `{ name, title, description?, projectId, appliesToUsers, appliesToRobots }`,\n * gated like the Access API's project roles. The fake does not model role\n * definitions, so the platform's `isCustom` and `grants` are not served.\n */\nfunction serveProjectRoles(args: {\n ctx: PlatformApiContext;\n request: PlatformRequest;\n projectId: string;\n}): Served {\n const { ctx, request, projectId } = args;\n const row = ACCESS_CAPABILITIES.project;\n return serveGated({\n capability: row.roles,\n permission: row.rolesPermission,\n held: ctx.actorAccess()?.[accessKey(\"project\", projectId)]?.permissions ?? [],\n request,\n serve: () => ({\n value: rolesOn({\n resource: { type: \"project\", id: projectId },\n identities: ctx.identities(),\n }).map((role) => ({\n name: role.name,\n title: role.title,\n ...(role.description !== undefined ? { description: role.description } : {}),\n projectId,\n appliesToUsers: role.appliesToUsers,\n appliesToRobots: role.appliesToRobots,\n })),\n }),\n });\n}\n\nfunction serveAccess(args: {\n ctx: PlatformApiContext;\n request: PlatformRequest;\n type: string;\n id: string;\n rest: string;\n query: URLSearchParams;\n}): Served {\n const { ctx, request, type, id, rest, query } = args;\n const row = accessCapabilitiesOf(type);\n if (row === undefined) throw invalidAccessType(request, type);\n const resource: AccessResource = { type: type as AccessResourceType, id };\n const registry: AccessRegistry = {\n organizationId: ctx.organizationId,\n resources: ctx.resources,\n };\n const permissions = ctx.actorAccess()?.[accessKey(resource.type, id)]?.permissions ?? [];\n // `check` answers `false` even for an id the platform does not hold.\n if (rest === \"/user-permissions/me/check\") {\n const requested = query.getAll(\"permissions\").flatMap((value) => value.split(\",\"));\n return {\n value: { data: Object.fromEntries(requested.map((p) => [p, permissions.includes(p)])) },\n };\n }\n if (row.unknown !== undefined && !row.unknown.holds(registry, id)) {\n throw row.unknown.refusal(request);\n }\n const gated = <T>(capability: Capability, permission: string | undefined, serve: () => T): T =>\n serveGated({ capability, permission, held: permissions, request, serve });\n const users = <T>(capability: Capability, serve: () => T): T =>\n gated(capability, row.usersPermission, serve);\n const roles = () => rolesOn({ resource, identities: ctx.identities() });\n\n const user = rest.match(ACCESS_USER_PATH);\n if (user !== null) {\n return users(row.user, () => {\n const sanityUserId = decodeURIComponent(user[1] as string);\n const record = usersOn({ resource, registry, identities: ctx.identities() }).find(\n (candidate) => candidate.sanityUserId === sanityUserId,\n );\n if (record === undefined) throw notFound(\"User\")(request);\n return { value: record };\n });\n }\n\n const role = rest.match(ACCESS_ROLE_PATH);\n if (role !== null) {\n return gated(row.roles, row.rolesPermission, () => {\n const name = decodeURIComponent(role[1] as string);\n const record = roles().find((candidate) => candidate.name === name);\n if (record === undefined) throw notFound(\"Role\")(request);\n return { value: record };\n });\n }\n\n switch (rest) {\n case \"/roles\":\n return gated(row.roles, row.rolesPermission, () => ({ value: page(roles()) }));\n case \"/users\":\n return users(row.users, () => ({\n value: usersPage(usersOn({ resource, registry, identities: ctx.identities() })),\n }));\n case \"/user-permissions/me\":\n return {\n value: page(\n permissions.map((permission): PermissionRecord => {\n const dot = permission.lastIndexOf(\".\");\n return {\n type: permission.slice(0, dot),\n resourceType: resource.type,\n resourceId: id,\n action: permission.slice(dot + 1),\n };\n }),\n ),\n };\n default:\n return undefined;\n }\n}\n\n/** An Access API role row: the seeded role, on its resource. */\ninterface AccessRoleRecord {\n name: string;\n title: string;\n description?: string;\n resourceType: AccessResourceType;\n resourceId: string;\n appliesToUsers: boolean;\n appliesToRobots: boolean;\n permissions: { type: string; action: string; name?: string; params?: Record<string, unknown> }[];\n}\n\n/**\n * The platform's role fields the fake does not model: every role applies to\n * users and robots. `title` and `permissions` are required too: the title is\n * the role's name where the seed gives none, and the permissions are empty.\n */\nconst UNMODELLED_ROLE = { appliesToUsers: true, appliesToRobots: true } as const;\n\n/** Every role a member holds on the resource, once each, in canonical order. */\nfunction rolesOn(args: {\n resource: AccessResource;\n identities: TestClientIdentity[];\n}): AccessRoleRecord[] {\n const { resource } = args;\n const byName = new Map<string, AccessRoleRecord>();\n for (const identity of args.identities) {\n const roles = ACCESS_CAPABILITIES[resource.type].memberRoles({ identity, id: resource.id });\n for (const role of roles ?? []) {\n byName.set(role.name, {\n name: role.name,\n title: role.title ?? role.name,\n ...(role.description !== undefined ? { description: role.description } : {}),\n resourceType: resource.type,\n resourceId: resource.id,\n ...UNMODELLED_ROLE,\n permissions: [],\n });\n }\n }\n return [...byName.values()].toSorted((a, b) => a.name.localeCompare(b.name));\n}\n\n/** The identity's membership on `resource`, or `undefined` when it holds no role there. */\nfunction membershipOn(\n identity: TestClientIdentity,\n resource: AccessResource,\n): AccessMembership | undefined {\n const roles = ACCESS_CAPABILITIES[resource.type].memberRoles({ identity, id: resource.id });\n if (roles === undefined) return undefined;\n return {\n resourceType: resource.type,\n resourceId: resource.id,\n roleNames: roles.map((role) => role.name).toSorted(),\n };\n}\n\n/**\n * One row per member of the resource, keyed by account-global id.\n * A member holds a role on the resource itself; its row also lists its\n * memberships on every resource the resource owns.\n */\nfunction usersOn(args: {\n resource: AccessResource;\n registry: AccessRegistry;\n identities: TestClientIdentity[];\n}): AccessUserRecord[] {\n const { resource, registry, identities } = args;\n const owned = ACCESS_CAPABILITIES[resource.type].owns?.(registry) ?? [];\n const bySanityUserId = new Map<string, AccessUserRecord>();\n for (const identity of identities) {\n const own = membershipOn(identity, resource);\n if (own === undefined) continue;\n const user = identity.globalUser ?? identity.user;\n const record: AccessUserRecord = {\n sanityUserId: user.id,\n profile: {\n id: user.id,\n displayName: user.name ?? user.id,\n email: user.email ?? \"\",\n ...UNMODELLED_PROFILE,\n },\n memberships: [\n own,\n // A user is part of a resource only through a role on it.\n ...owned.flatMap((child) => {\n const membership = membershipOn(identity, child);\n return membership === undefined || membership.roleNames.length === 0 ? [] : [membership];\n }),\n ],\n };\n const existing = bySanityUserId.get(record.sanityUserId);\n if (existing !== undefined && JSON.stringify(existing) !== JSON.stringify(record)) {\n throw fakeError(\n `request(): two identities (registered, or the handle's inline accessControl) resolve to ` +\n `user \"${record.sanityUserId}\" on ${resource.type} \"${resource.id}\" with different ` +\n \"records. Fix the identities so duplicate ids carry identical records.\",\n );\n }\n bySanityUserId.set(record.sanityUserId, record);\n }\n return [...bySanityUserId.values()];\n}\n\n/** The page size \"Get My Attributes\" answers when the request names none. */\nconst DEFAULT_ATTRIBUTES_LIMIT = 50;\n\n/** One served attribute row: the seeded values plus the one in effect. */\ninterface UserAttributeRecord extends UserAttribute {\n activeSource: \"saml\" | \"sanity\";\n activeValue: UserAttributeValue;\n}\n\n/**\n * `GET /organizations/<id>/users/me/attributes`: the caller's own\n * attributes in the organization, ordered by key, one page of `limit` after\n * the `cursor` key. No permission is needed; a caller outside the\n * organization is not found there.\n */\nfunction serveMyAttributes(args: {\n ctx: PlatformApiContext;\n request: PlatformRequest;\n organizationId: string;\n query: URLSearchParams;\n}): Served {\n const { ctx, request, organizationId, query } = args;\n const organization = ACCESS_CAPABILITIES.organization;\n const registry: AccessRegistry = { organizationId: ctx.organizationId, resources: ctx.resources };\n if (organization.unknown !== undefined && !organization.unknown.holds(registry, organizationId)) {\n throw organization.unknown.refusal(request);\n }\n // Organization membership is a role in the actor's organization entry.\n const entry = ctx.actorAccess()?.[accessKey(\"organization\", organizationId)];\n if (entry?.roles === undefined) throw notFound(\"User\")(request);\n const limit = attributesLimit(query);\n const cursor = query.get(\"cursor\");\n const attributes = (entry.attributes ?? [])\n .map(attributeRecord)\n .toSorted((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))\n .filter((attribute) => cursor === null || attribute.key > cursor)\n .slice(0, limit);\n return { value: { sanityUserId: ctx.actorUser().id, organizationId, attributes } };\n}\n\nfunction attributesLimit(query: URLSearchParams): number {\n const raw = query.get(\"limit\");\n if (raw === null) return DEFAULT_ATTRIBUTES_LIMIT;\n const limit = Number(raw);\n if (!Number.isInteger(limit) || limit < 1) {\n throw fakeError(\n `request(): users/me/attributes got limit=${JSON.stringify(raw)}; the fake serves positive ` +\n \"integer limits only (the platform's answer to others is not simulated).\",\n );\n }\n return limit;\n}\n\n/** Sanity values take precedence over SAML ones, as the platform resolves them. */\nfunction attributeRecord(attribute: UserAttribute): UserAttributeRecord {\n const { key, type, values } = attribute;\n const activeSource = values.sanity !== undefined ? \"sanity\" : \"saml\";\n const activeValue = values[activeSource];\n if (activeValue === undefined) {\n throw fakeError(\n `request(): user attribute \"${key}\" is seeded with no value; give it a \\`saml\\` or ` +\n \"`sanity` value.\",\n );\n }\n return { type, key, values: { ...values }, activeSource, activeValue };\n}\n","/**\n * Optional access-control simulation for the in-memory test client.\n *\n * When configured, the TestClient does two things a real `@sanity/client`\n * does over the wire but the bare in-memory client otherwise skips:\n *\n * 1. **Exposes `client.request({ url })`** that serves:\n * - `GET /users/me` → the configured `CurrentUser` — the project-scoped\n * record on a project-hosted handle, `globalUser` on a\n * `useProjectHostname: false` handle (Sanity's global API host,\n * set at creation or via `withConfig`)\n * - `GET <aclPath>` → the configured `Grant[]`\n * - `GET /projects/<projectId>` → the project's members (ids, roles, and robot flags)\n * - `GET /projects/<projectId>/users/<ids>` → project-user records\n * carrying `id` + `sanityUserId`, the bridge between the project\n * and global identity namespaces\n * - `GET /projects/<projectId>/roles` → the project role catalog\n * - `GET /projects/<projectId>/features` → the project's feature names\n * This is what the engine reads from to resolve `WorkflowAccess`\n * when no override is passed.\n *\n * 2. **Gates writes through the configured grants.** Every mutation\n * (`create`, `createOrReplace`, `createIfNotExists`, `delete`,\n * `patch`, and per-op inside `transaction`) checks whether the\n * configured grants allow the relevant `DocumentValuePermission`\n * on the target document. On denial the client throws a\n * `WriteAccessDeniedError` shaped like Sanity's 403 — the same\n * failure mode a production write would hit, surfaced in tests.\n *\n * When `accessControl` is omitted, the client behaves as before:\n * `request` is undefined, writes are unconditional, and the engine\n * falls through to whatever override the caller (typically the\n * bench's `ALL_ACCESS`) provides.\n *\n * A filter grant is evaluated by running its GROQ filter on a singleton\n * dataset containing the doc being checked — same shape as the engine's\n * `permissions.matchesFilter`, lifted from canvas. A path grant matches\n * the doc's `_id` element by element (see `./token-grants.ts`).\n */\n\nimport { ClientError } from \"@sanity/client\";\nimport type { SanityDocumentLike } from \"@sanity/types\";\nimport { evaluate, parse } from \"groq-js\";\n\nimport { fakeError } from \"./errors.ts\";\nimport { pathIncludes } from \"./token-grants.ts\";\nimport { refuseAclRequest, servePlatformRequest, type PlatformApiContext } from \"./platform-api.ts\";\nimport {\n DEFAULT_WRITE,\n isProjectMember,\n transactionRefused,\n type PlatformRequest,\n} from \"./resources.ts\";\n\nexport type DocumentValuePermission = \"create\" | \"manage\" | \"read\" | \"update\";\n\nexport interface CurrentUser {\n id: string;\n name?: string;\n email?: string;\n /** @deprecated singular `role` on legacy responses */\n role?: string;\n roles?: { name: string; title?: string; description?: string }[];\n}\n\n/**\n * One grant: `permissions` on the documents a GROQ `filter` matches, as an\n * ACL serves them, or on the ids a `path()` glob matches, as a token\n * carries them (the token's `{ s, p }`). The Content Lake holds a grant as\n * one or the other and evaluates a path grant as `_id in path(\"<path>\")`\n * (gradient `pkg/security/models.go` `Grant`, `FilterExpression`). A token\n * grant's permissions are whatever the token names; the Content Lake\n * refuses the token when one is not a permission it knows, or when the\n * path does not parse (see `./token-grants.ts`).\n */\nexport type Grant =\n | { filter: string; permissions: DocumentValuePermission[] }\n | { path: string; permissions: string[] };\n\n/**\n * What the Access API (`/access/<type>/<id>/…`) answers about one actor on\n * one resource: the roles it holds there (served by `…/users` and collected\n * into `…/roles`) and the permissions `…/user-permissions/me` and\n * `…/user-permissions/me/check` report, as `\"<type>.<action>\"` names such\n * as `\"sanity.media-library.read\"`.\n */\nexport interface ResourceAccess {\n roles?: NonNullable<CurrentUser[\"roles\"]>;\n permissions?: string[];\n /**\n * The user's attributes in the organization, as an administrator or SAML\n * sync set them; read on `\"organization:<id>\"` entries only. Served by\n * `GET /organizations/<id>/users/me/attributes`.\n */\n attributes?: UserAttribute[];\n}\n\n/** A value a user attribute can hold. */\nexport type UserAttributeValue = string | number | boolean | string[] | number[] | boolean[];\n\n/**\n * One user attribute: its key, its type, and its value from each source.\n * The served row adds `activeSource` and `activeValue`: the Sanity value\n * when there is one, else the SAML value.\n */\nexport interface UserAttribute {\n key: string;\n type: string;\n values: { saml?: UserAttributeValue; sanity?: UserAttributeValue };\n}\n\nexport interface TestClientAccessControl {\n /** What `client.request({ url: \"/users/me\" })` returns on a project-hosted handle. */\n currentUser: CurrentUser;\n /**\n * What `/users/me` returns on a global-hosted handle\n * (`useProjectHostname: false`, at creation or via `withConfig`).\n * Sanity serves the\n * account-global user there (`g*` id, no roles) while the project host\n * serves the project-scoped record (`p*` id, project roles). Omit to\n * serve `currentUser` from both — the single-id collapse that is also\n * exactly how robot tokens behave in production.\n */\n globalUser?: CurrentUser;\n /** What `client.request({ url: aclPath })` returns. */\n grants: Grant[];\n /**\n * URL path the engine's access resolver hits to fetch grants.\n * Defaults to the handle's resource ACL: `/projects/<projectId>/datasets/<dataset>/acl`,\n * or `<resource base>/acl` on a `resource`-configured handle.\n */\n aclPath?: string;\n /**\n * Access API entries, keyed `\"<access type>:<id>\"` — `\"organization:<id>\"`,\n * `\"project:<projectId>\"`, `\"media-library:<id>\"`, `\"canvas:<id>\"`, `\"dashboard:<id>\"`. See {@link ResourceAccess}.\n */\n access?: Record<string, ResourceAccess>;\n}\n\n/**\n * One entry in the token→identity registry (`createTestClient({ identities })`).\n * A handle bound to the entry's token behaves exactly like one configured\n * with `accessControl: { currentUser: projectUserOf(identity, projectId),\n * globalUser, grants }` — `/users/me` serves the record for the handle's\n * project and host, the default ACL path serves `grants`, and reads/writes\n * are gated through them.\n */\nexport interface TestClientIdentity {\n /**\n * The project-scoped user record: what `/users/me` returns on a\n * project-hosted handle, and the id `identity()` resolves to in grant\n * filters and mutation guards. Per-project variants go in\n * `usersByProject`; this is the record for any project without one.\n */\n user: CurrentUser;\n /**\n * The account-global user record, served by `/users/me` on a\n * global-hosted handle (`useProjectHostname: false`, at creation or\n * via `withConfig`).\n * The same person carries a different id per project (`p*`) and one\n * global id (`g*`); registering both lets tests model that split.\n * Omit for single-id principals (robot tokens resolve to one `p-*` id\n * on every host).\n */\n globalUser?: CurrentUser;\n /**\n * Per-project user records, keyed by projectId — the same token\n * resolves to a different project-scoped id in each project, which is\n * what these model. A handle's project falls back to `user` when its\n * projectId has no entry.\n */\n usersByProject?: Record<string, CurrentUser>;\n /** Grants served at the ACL path and enforced on reads/writes. */\n grants: Grant[];\n /**\n * Access API entries, keyed `\"<access type>:<id>\"` (`\"organization:<id>\"`,\n * `\"media-library:<id>\"`, `\"canvas:<id>\"`, `\"dashboard:<id>\"`,\n * `\"project:<projectId>\"`). On a non-project resource, an entry with\n * `roles` makes the identity a member there. Project membership needs no\n * entry: see `projectId`.\n */\n access?: Record<string, ResourceAccess>;\n /**\n * The project a project-scoped token belongs to, as a robot token belongs\n * to the project that issued it. The identity is a member of that project\n * only, and its token is refused on any other project's API host. Omit\n * for a token the fake treats as a member of every project, accepted on\n * every host.\n */\n projectId?: string;\n}\n\n/**\n * A row served by the project-users directory endpoint\n * (`/projects/<projectId>/users/<id>`), mirroring Sanity's project user\n * retrieval API: the project-scoped `id` alongside its account-global\n * `sanityUserId` — the one sanctioned bridge between the two namespaces.\n */\nexport interface ProjectUserRecord {\n id: string;\n sanityUserId: string;\n displayName: string;\n email?: string;\n}\n\n/**\n * The Content Lake's 403 when a configured-grants check rejects a write: a\n * `ClientError` built from the response gradient sends, an\n * `insufficientPermissionsError` wrapped in a `mutationError`\n * (`pkg/security/errors.go` `InsufficientPermissionsError`). It also carries\n * the doc id, the attempted permission, and the count of matching grants\n * (for parity with the engine's `permission-denied` disabled-reason), which\n * the platform's answer does not name.\n */\nexport class WriteAccessDeniedError extends ClientError {\n readonly documentId: string;\n readonly permission: DocumentValuePermission;\n readonly matchingGrants: number;\n\n constructor(args: {\n documentId: string;\n permission: DocumentValuePermission;\n matchingGrants: number;\n /** The write the Content Lake refused; defaults to a `/data/mutate` request. */\n request?: PlatformRequest;\n }) {\n super(\n transactionRefused(args.request ?? DEFAULT_WRITE, {\n description: `Insufficient permissions; permission \"${args.permission}\" required`,\n permission: args.permission,\n type: \"insufficientPermissionsError\",\n }),\n );\n this.name = \"WriteAccessDeniedError\";\n this.documentId = args.documentId;\n this.permission = args.permission;\n this.matchingGrants = args.matchingGrants;\n }\n}\n\n/**\n * Predicate variant of `evaluateWriteAccess` — same logic but only\n * returns the boolean. Used by read-gating paths\n * (`fetch`/`getDocument`/`getDocuments`) where the documented Sanity\n * behavior is \"invisible doc → 404\", not \"throw 403\". So callers\n * can filter the dataset before GROQ runs and return `null` from\n * single-doc reads, instead of leaking a denial through the read\n * surface.\n */\nexport async function canAccess(args: {\n document: SanityDocumentLike & { _id?: string; _type?: string };\n grants: Grant[];\n permission: DocumentValuePermission;\n userId: string;\n}): Promise<boolean> {\n const result = await evaluateWriteAccess(args);\n return result.allowed;\n}\n\n// Parsed-filter cache. The GROQ AST is cheap to reuse; only the\n// per-document evaluation needs to re-run.\nconst parsedFilters = new Map<string, ReturnType<typeof parse>>();\n\nasync function matchesFilter(args: {\n document: SanityDocumentLike & { _id?: string; _type?: string };\n filter: string;\n userId: string;\n}): Promise<boolean> {\n const { document, filter, userId } = args;\n if (!parsedFilters.has(filter)) {\n parsedFilters.set(filter, parse(`*[${filter}]`));\n }\n const ast = parsedFilters.get(filter)!;\n const result = await evaluate(ast, {\n dataset: [document as Record<string, unknown>],\n identity: userId,\n });\n const data = await result.get();\n return Array.isArray(data) && data.length === 1;\n}\n\n/** Whether `grant` covers `document`: its path glob includes the `_id`, or its filter matches. */\nasync function grantMatches(args: {\n document: SanityDocumentLike & { _id?: string; _type?: string };\n grant: Grant;\n userId: string;\n}): Promise<boolean> {\n const { document, grant, userId } = args;\n if (\"path\" in grant) {\n return typeof document._id === \"string\" && pathIncludes(grant.path, document._id);\n }\n return matchesFilter({ document, filter: grant.filter, userId });\n}\n\n/**\n * Resolve whether `grants` allow `permission` on `document` for the\n * given user. Same logic the engine uses in `permissions.grantsPermissionOn`\n * (most-permissive wins; multiple grants compose).\n *\n * Returns `{ allowed: boolean, matchingGrants: number }` so callers\n * can build the structured error without re-counting.\n */\nexport async function evaluateWriteAccess(args: {\n document: SanityDocumentLike & { _id?: string; _type?: string };\n grants: Grant[];\n permission: DocumentValuePermission;\n userId: string;\n}): Promise<{ allowed: boolean; matchingGrants: number }> {\n const { document, grants, permission, userId } = args;\n let matching = 0;\n for (const grant of grants) {\n if (await grantMatches({ document, grant, userId })) {\n matching++;\n if (grant.permissions.includes(permission)) {\n return { allowed: true, matchingGrants: matching };\n }\n }\n }\n return { allowed: false, matchingGrants: matching };\n}\n\n/**\n * Throw a `WriteAccessDeniedError` if `grants` don't permit the\n * supplied `permission` on `document`. No-op when allowed. Use this\n * from every mutation entrypoint that wants to simulate Sanity's\n * write boundary.\n */\nexport async function enforceWriteAccess(args: {\n document: SanityDocumentLike & { _id?: string; _type?: string };\n grants: Grant[];\n permission: DocumentValuePermission;\n userId: string;\n request: PlatformRequest;\n}): Promise<void> {\n const { document, grants, permission, userId, request } = args;\n const { allowed, matchingGrants } = await evaluateWriteAccess({\n document,\n grants,\n permission,\n userId,\n });\n if (allowed) return;\n if (typeof document._id !== \"string\") {\n throw new WriteAccessDeniedError({\n documentId: \"(unknown)\",\n permission,\n matchingGrants,\n request,\n });\n }\n throw new WriteAccessDeniedError({\n documentId: document._id,\n permission,\n matchingGrants,\n request,\n });\n}\n\n/**\n * Default ACL path the engine pulls grants from. Mirrors Sanity's\n * project/dataset ACL endpoint. Callers can override via\n * `accessControl.aclPath`.\n */\nexport function defaultAclPath(projectId: string, dataset: string): string {\n return `/projects/${projectId}/datasets/${dataset}/acl`;\n}\n\n/** Everything {@link makeRequestImpl} needs from the owning handle. */\nexport interface RequestImplContext {\n /**\n * Active access-control resolver. A getter, not a value — token-bound\n * handles resolve their identity from the shared registry on every\n * call, so a re-registered identity (`addIdentity` upsert) takes\n * effect on already-minted siblings.\n */\n getAccess: () => TestClientAccessControl;\n /** The request `@sanity/client` sends for `request(opts)` on the handle, full URL included. */\n requestFor: (opts: { url?: string; uri?: string; tag?: string | null }) => PlatformRequest;\n projectId: string;\n /**\n * Whether this handle addresses the project host (default) or the\n * global API host (`useProjectHostname: false`, at creation or via\n * `withConfig`).\n * Decides which user record `/users/me` serves.\n */\n useProjectHostname: boolean;\n /**\n * The handle's members (registered identities, plus its inline\n * `accessControl` actor), for the project endpoints. A getter so\n * `addIdentity` upserts are visible.\n */\n listIdentities: () => TestClientIdentity[];\n /** See {@link CreateTestClientOptions.organizationId}; added to `/projects/<projectId>`. */\n organizationId: string | undefined;\n /** History, ACL, resource lookup and Access API routing; see `./platform-api.ts`. */\n platform: PlatformApiContext;\n}\n\n/** The project-scoped record a registry identity resolves to in `projectId`. */\nexport function projectUserOf(identity: TestClientIdentity, projectId: string): CurrentUser {\n return identity.usersByProject?.[projectId] ?? identity.user;\n}\n\n/**\n * Deduplicate tokens that resolve to one project user. Callers supply served\n * records in canonical form so stringify equality represents semantic\n * equality; divergent records fail instead of producing a last-wins answer.\n */\nfunction consistentProjectRecords<RecordType extends { id: string }>(args: {\n projectId: string;\n records: readonly RecordType[];\n}): Map<string, RecordType> {\n const byId = new Map<string, RecordType>();\n for (const record of args.records) {\n const existing = byId.get(record.id);\n if (existing !== undefined && JSON.stringify(existing) !== JSON.stringify(record)) {\n throw fakeError(\n `request(): two identities (registered, or the handle's inline accessControl) resolve to ` +\n `project user \"${record.id}\" in project ` +\n `\"${args.projectId}\" with different records. The project records cannot be served ` +\n `unambiguously — fix the identities so duplicate ids carry identical records.`,\n );\n }\n byId.set(record.id, record);\n }\n return byId;\n}\n\n/** Normalize role key and array order for stable serving and duplicate comparison. */\nfunction canonicalProjectRoles(roles: NonNullable<CurrentUser[\"roles\"]>) {\n return roles\n .map((role) => ({\n name: role.name,\n ...(role.title !== undefined ? { title: role.title } : {}),\n ...(role.description !== undefined ? { description: role.description } : {}),\n }))\n .toSorted((left, right) => {\n const leftKey = JSON.stringify(left);\n const rightKey = JSON.stringify(right);\n if (leftKey < rightKey) return -1;\n if (leftKey > rightKey) return 1;\n return 0;\n });\n}\n\n/**\n * Serve `/projects/<projectId>/users/<ids>` from the identity registry —\n * the fake's mirror of Sanity's project user retrieval API, the bridge\n * between project-scoped ids and `sanityUserId`. Single id → one record;\n * comma-separated ids → array. Unknown single id throws with\n * `statusCode: 404`, like production; unknown ids in a list are omitted.\n */\nfunction serveProjectUsers(args: {\n requestedIds: string;\n pathProjectId: string;\n identities: TestClientIdentity[];\n}): ProjectUserRecord | ProjectUserRecord[] {\n const { requestedIds, pathProjectId, identities } = args;\n const toRecord = (identity: TestClientIdentity): ProjectUserRecord => {\n const projectUser = projectUserOf(identity, pathProjectId);\n return {\n id: projectUser.id,\n sanityUserId: identity.globalUser?.id ?? projectUser.id,\n displayName: projectUser.name ?? projectUser.id,\n ...(projectUser.email !== undefined ? { email: projectUser.email } : {}),\n };\n };\n const byId = consistentProjectRecords({\n projectId: pathProjectId,\n records: identities\n .filter((identity) => isProjectMember(identity.projectId, pathProjectId))\n .map(toRecord),\n });\n const ids = requestedIds.split(\",\");\n const [firstId] = ids;\n if (ids.length === 1 && firstId !== undefined) {\n const record = byId.get(firstId);\n if (record === undefined) {\n throw Object.assign(\n fakeError(\n `request(): no registered identity resolves to project user \"${firstId}\" in project ` +\n `\"${pathProjectId}\". Register it via createTestClient({ identities }) or addIdentity().`,\n ),\n { statusCode: 404 },\n );\n }\n return record;\n }\n return ids.flatMap((id) => {\n const record = byId.get(id);\n return record === undefined ? [] : [record];\n });\n}\n\nconst PROJECT_USERS_PATH = /^\\/projects\\/([^/]+)\\/users\\/([^/]+)$/;\n\nconst PROJECT_PATH = /^\\/projects\\/([^/]+)$/;\n\n/** One row of the `/projects/<projectId>` members response. */\nexport interface ProjectMemberRecord {\n id: string;\n isRobot?: boolean;\n /** Project-scoped roles in canonical order, independent of registration order. */\n roles?: NonNullable<CurrentUser[\"roles\"]>;\n}\n\n/**\n * Serve `/projects/<projectId>` from the identity registry — the members\n * half of Sanity's project API, which member-list joins read\n * (`members[].id` carries the project-scoped principal id and `roles` carries\n * its project roles; robot principals are flagged `isRobot`, matching their\n * production `p-…` form). One row per distinct resolved project user, however\n * many tokens map to that person.\n */\nfunction serveProject(args: {\n pathProjectId: string;\n identities: TestClientIdentity[];\n organizationId: string | undefined;\n}): {\n organizationId?: string;\n members: ProjectMemberRecord[];\n} {\n const members = args.identities.filter((identity) =>\n isProjectMember(identity.projectId, args.pathProjectId),\n );\n const records = members.map((identity) => {\n const projectUser = projectUserOf(identity, args.pathProjectId);\n return {\n id: projectUser.id,\n ...(projectUser.id.startsWith(\"p-\") ? { isRobot: true } : {}),\n ...(projectUser.roles !== undefined\n ? { roles: canonicalProjectRoles(projectUser.roles) }\n : {}),\n } satisfies ProjectMemberRecord;\n });\n const byId = consistentProjectRecords({ projectId: args.pathProjectId, records });\n return {\n ...(args.organizationId !== undefined ? { organizationId: args.organizationId } : {}),\n members: [...byId.values()],\n };\n}\n\n/**\n * Internal helper: turn the active access-control config into the bound\n * `request` function the client exposes.\n */\nexport function makeRequestImpl(\n ctx: RequestImplContext,\n): <T>(opts: {\n url?: string;\n uri?: string;\n signal?: AbortSignal;\n tag?: string | null;\n}) => Promise<T> {\n const { getAccess, requestFor, useProjectHostname, listIdentities, organizationId, platform } =\n ctx;\n return async <T>(opts: {\n url?: string;\n uri?: string;\n signal?: AbortSignal;\n tag?: string | null;\n }): Promise<T> => {\n const accessControl = getAccess();\n // `@sanity/client` distinguishes `uri` (project-scoped, e.g.\n // `/users/me`) from `url` (raw). Treat both identically for the mock —\n // host selection rides the handle's `useProjectHostname`, as in\n // production, not the option name.\n const path = opts.uri ?? opts.url;\n if (path === \"/users/me\") {\n // The global host serves the account-global user; the project host\n // serves the project-scoped record. Absent `globalUser` both serve\n // the same record — the single-id shape robot tokens have in\n // production.\n const user = useProjectHostname\n ? accessControl.currentUser\n : (accessControl.globalUser ?? accessControl.currentUser);\n return user as unknown as T;\n }\n // An explicit `aclPath` aliases the grants, but never past the platform's refusal of that ACL.\n if (path !== undefined && path === accessControl.aclPath) {\n refuseAclRequest(platform, requestFor(opts));\n return accessControl.grants as unknown as T;\n }\n const projectUsers = path?.match(PROJECT_USERS_PATH);\n const [, pathProjectId, requestedIds] = projectUsers ?? [];\n if (pathProjectId !== undefined && requestedIds !== undefined) {\n return serveProjectUsers({\n requestedIds,\n pathProjectId,\n identities: listIdentities(),\n }) as unknown as T;\n }\n const project = path?.match(PROJECT_PATH);\n const [, membersProjectId] = project ?? [];\n if (membersProjectId !== undefined) {\n return serveProject({\n pathProjectId: membersProjectId,\n identities: listIdentities(),\n organizationId,\n }) as unknown as T;\n }\n const served =\n path === undefined ? undefined : await servePlatformRequest(platform, path, requestFor(opts));\n if (served !== undefined) return served.value as T;\n throw fakeError(\n `request(): no handler for path \"${path}\". With access control configured, the fake serves ` +\n `\"/users/me\", the handle's ACL (\"<data base>/acl\"), \"/projects/<projectId>\" (members), ` +\n `\"/projects/<projectId>/users/<ids>\", \"/projects/<projectId>/roles\", \"/projects/<projectId>/features\", the History API (client.getDataUrl(\"history\", …)), ` +\n `resource lookup (\"/media-libraries/<id>\", \"/canvases/<id>\", \"/dashboards/<id>\") and the ` +\n `Access API (\"/access/<type>/<id>/roles[/<name>] | users[/<id>] | user-permissions/me[/check]\") ` +\n `and the caller's attributes (\"/organizations/<id>/users/me/attributes\"). For any ` +\n `other endpoint, register a stub (client.stub(\"request\", fn)) or intercept the HTTP call ` +\n `with a tool like MSW.`,\n );\n };\n}\n","import { ClientError } from \"@sanity/client\";\nimport type { SanityDocument, SanityDocumentLike } from \"@sanity/types\";\n\nimport { DEFAULT_WRITE, transactionRefused, type PlatformRequest } from \"./resources.ts\";\n\n/** The Content Lake mutation actions that a {@link MutationGuard} can match. */\nexport type MutationGuardAction = \"create\" | \"update\" | \"delete\";\n\nexport interface MutationGuardDenial {\n guardID: string;\n name?: string;\n}\n\n/** The `response` of a {@link GuardDeniedError}: the Content Lake's 403 for a guarded write. */\nexport interface GuardDeniedResponse {\n body: {\n error: {\n description: string;\n items: [{ error: GuardDeniedDetails; index: 0 }];\n type: \"mutationError\";\n };\n };\n url: string;\n method: string;\n headers: Record<string, string>;\n statusCode: 403;\n statusMessage: \"Forbidden\";\n}\n\n/** The `guardDeniedError` gradient writes for a guard's denial (`proto.GuardDeniedError`). */\nexport interface GuardDeniedDetails {\n description: string;\n denials: MutationGuardDenial[];\n type: \"guardDeniedError\";\n}\n\n/**\n * The Content Lake's 403 when a mutation guard denies a write: a\n * `ClientError` built from the response gradient sends, a\n * `guardDeniedError` wrapped in a `mutationError`. `denials` and `type` lift\n * the guard's answer to the top for callers that match on them.\n */\nexport class GuardDeniedError extends ClientError {\n readonly type = \"guardDeniedError\";\n readonly denials: MutationGuardDenial[];\n declare readonly statusCode: 403;\n declare readonly response: GuardDeniedResponse;\n\n constructor(denials: readonly MutationGuardDenial[], request: PlatformRequest = DEFAULT_WRITE) {\n const unique = uniqueDenials(denials);\n super(\n transactionRefused(request, {\n description: guardDeniedDescription(unique),\n denials: unique,\n type: \"guardDeniedError\",\n }),\n );\n this.name = \"GuardDeniedError\";\n this.denials = unique;\n }\n}\n\n/** gradient's `guard.DenyError.Error()` (`pkg/documents/guard/errors.go`). */\nfunction guardDeniedDescription(denials: readonly MutationGuardDenial[]): string {\n const named = denials.map(({ guardID, name }) => (name ? `${guardID} (${name})` : guardID));\n return `mutation denied by ${denials.length} guard(s): ${named.join(\" \")}`;\n}\n\nfunction uniqueDenials(denials: readonly MutationGuardDenial[]): MutationGuardDenial[] {\n const seen = new Set<string>();\n return denials.filter((denial) => {\n if (seen.has(denial.guardID)) return false;\n seen.add(denial.guardID);\n return true;\n });\n}\n\n/** The before/final-after image of one net document change in a commit. */\nexport interface MutationGuardContext {\n action: MutationGuardAction;\n /** Original live store image. Treat it as read-only; it is not a copy. */\n before: Readonly<SanityDocumentLike & { _id: string; _type: string }> | null;\n /** Final live store image. Treat it as read-only; it is not a copy. */\n after: Readonly<SanityDocumentLike & { _id: string; _type: string }> | null;\n /** Acting project identity, or `undefined` on an identity-free client. */\n identity: string | undefined;\n}\n\n/**\n * Evaluates one net document change and returns every denying guard. Returning\n * `void` allows observe-only hooks. A thrown value is a callback failure and\n * propagates unchanged; only returned entries become {@link GuardDeniedError}.\n */\nexport type MutationGuard = (\n context: MutationGuardContext,\n) => readonly MutationGuardDenial[] | void | Promise<readonly MutationGuardDenial[] | void>;\n\nexport interface MutationImages {\n before: SanityDocument | undefined;\n after: SanityDocument | undefined;\n}\n\nexport type MutationGuardImageContext = Omit<MutationGuardContext, \"identity\">;\n\n/** Derive the single lake action represented by a document's net commit image. */\nexport function mutationContext(images: MutationImages): MutationGuardImageContext | undefined {\n const { before, after } = images;\n // TestStore preserves object identity until a write replaces the document.\n // Equal references therefore mean an ID was remembered but never changed.\n if (before === after) return undefined;\n if (before === undefined) return { action: \"create\", before: null, after: after ?? null };\n if (after === undefined) return { action: \"delete\", before, after: null };\n return { action: \"update\", before, after };\n}\n\n/** Derive guard contexts for an ordered collection of net document images. */\nexport function mutationContexts(images: Iterable<MutationImages>): MutationGuardImageContext[] {\n return [...images].flatMap((image) => {\n const context = mutationContext(image);\n return context === undefined ? [] : [context];\n });\n}\n\n/** Evaluate every net context before rejecting once with all returned denials. */\nexport async function enforceMutationGuards(\n guard: MutationGuard | undefined,\n contexts: readonly MutationGuardContext[],\n request: PlatformRequest,\n): Promise<void> {\n if (guard === undefined) return;\n const denials: MutationGuardDenial[] = [];\n for (const context of contexts) {\n const denied = await guard(context);\n if (denied !== undefined) denials.push(...denied);\n }\n if (denials.length > 0) throw new GuardDeniedError(denials, request);\n}\n","import type { SanityDocument } from \"@sanity/types\";\nimport { fakeError } from \"./errors.ts\";\n\n/** Human-readable type of a value, for \"expected an array but got …\" errors. */\nfunction describeType(value: unknown): string {\n if (value === null) return \"null\";\n if (Array.isArray(value)) return \"an array\";\n return `a ${typeof value}`;\n}\n\n/**\n * Patch operations the test client supports.\n *\n * Subset of `@sanity/client`'s PatchOperations. Each entry is one\n * mutation step applied in order at commit time.\n */\nexport type PatchOp =\n | { kind: \"set\"; props: Record<string, unknown> }\n | { kind: \"setIfMissing\"; props: Record<string, unknown> }\n | { kind: \"unset\"; paths: string[] }\n | { kind: \"inc\"; props: Record<string, number> }\n | { kind: \"dec\"; props: Record<string, number> }\n | {\n kind: \"insert\";\n position: \"before\" | \"after\" | \"replace\";\n anchor: string;\n items: unknown[];\n };\n\nexport interface PatchHandle {\n set: (props: Record<string, unknown>) => PatchHandle;\n setIfMissing: (props: Record<string, unknown>) => PatchHandle;\n unset: (paths: string[]) => PatchHandle;\n inc: (props: Record<string, number>) => PatchHandle;\n dec: (props: Record<string, number>) => PatchHandle;\n insert: (\n position: \"before\" | \"after\" | \"replace\",\n anchor: string,\n items: unknown[],\n ) => PatchHandle;\n ifRevisionId: (rev: string) => PatchHandle;\n commit: (options?: PatchCommitOptions) => Promise<SanityDocument>;\n /** Test-only introspection — not on real Patch */\n toJSON: () => { id: string; ops: PatchOp[]; ifRev?: string };\n}\n\n/** Options for `PatchHandle.commit()`. Only `tag` is meaningful to the fake. */\nexport interface PatchCommitOptions {\n /** Request tag, recorded on the mutation log entry (`null` = explicit suppression). */\n tag?: string | null;\n /** Accepted for signature parity; not simulated. */\n visibility?: \"sync\" | \"async\" | \"deferred\";\n}\n\nexport interface PatchExecutor {\n apply: (\n documentId: string,\n ops: PatchOp[],\n ifRev: string | undefined,\n options: PatchCommitOptions | undefined,\n ) => Promise<SanityDocument>;\n}\n\nexport function createPatch(documentId: string, executor: PatchExecutor): PatchHandle {\n const ops: PatchOp[] = [];\n let ifRev: string | undefined;\n\n const handle: PatchHandle = {\n set: (props) => {\n ops.push({ kind: \"set\", props });\n return handle;\n },\n setIfMissing: (props) => {\n ops.push({ kind: \"setIfMissing\", props });\n return handle;\n },\n unset: (paths) => {\n ops.push({ kind: \"unset\", paths });\n return handle;\n },\n inc: (props) => {\n ops.push({ kind: \"inc\", props });\n return handle;\n },\n dec: (props) => {\n ops.push({ kind: \"dec\", props });\n return handle;\n },\n insert: (position, anchor, items) => {\n ops.push({ kind: \"insert\", position, anchor, items });\n return handle;\n },\n ifRevisionId: (rev) => {\n ifRev = rev;\n return handle;\n },\n commit: (options) => executor.apply(documentId, ops, ifRev, options),\n toJSON: () => ({\n id: documentId,\n ops: [...ops],\n ...(ifRev !== undefined ? { ifRev } : {}),\n }),\n };\n\n return handle;\n}\n\n/**\n * Apply an ordered list of patch ops to a document, returning the new doc.\n *\n * The path semantics are deliberately a small subset:\n *\n * - **Dotted paths** (`a.b.c`) for nested object fields.\n * - **Array index** (`a.b[0]`) for positional access into arrays.\n * - **Keyed lookup** (`a.b[_key==\"x\"]`) for finding an array entry by `_key`.\n *\n * Anything more exotic (recursive `..`, GROQ filter expressions, multi-match)\n * is intentionally out of scope and will throw — the workflow engine only\n * needs the three forms above.\n */\nexport function applyPatchOps(doc: SanityDocument, ops: PatchOp[]): SanityDocument {\n let next: SanityDocument = structuredClone(doc);\n for (const op of ops) {\n next = applyOne(next, op);\n }\n return next;\n}\n\nfunction applyOne(doc: SanityDocument, op: PatchOp): SanityDocument {\n switch (op.kind) {\n case \"set\":\n for (const [path, value] of Object.entries(op.props)) {\n setAtPath(doc, path, value);\n }\n return doc;\n case \"setIfMissing\":\n for (const [path, value] of Object.entries(op.props)) {\n if (getAtPath(doc, path) === undefined) {\n setAtPath(doc, path, value);\n }\n }\n return doc;\n case \"unset\":\n for (const path of op.paths) {\n unsetAtPath(doc, path);\n }\n return doc;\n case \"inc\":\n for (const [path, delta] of Object.entries(op.props)) {\n const current = getAtPath(doc, path);\n const base = typeof current === \"number\" ? current : 0;\n setAtPath(doc, path, base + delta);\n }\n return doc;\n case \"dec\":\n for (const [path, delta] of Object.entries(op.props)) {\n const current = getAtPath(doc, path);\n const base = typeof current === \"number\" ? current : 0;\n setAtPath(doc, path, base - delta);\n }\n return doc;\n case \"insert\": {\n insertIntoArray(doc, op.position, op.anchor, op.items);\n return doc;\n }\n }\n}\n\ninterface PathSegment {\n key: string;\n // Optional array selector. Either a numeric index or a key=value match.\n selector?: { kind: \"index\"; index: number } | { kind: \"keyed\"; field: string; value: string };\n}\n\nfunction parsePath(path: string): PathSegment[] {\n // Examples:\n // \"currentStageId\"\n // \"taskStatus[0].status\"\n // \"taskStatus[_key==\\\"approve\\\"].status\"\n // \"context[key==\\\"releaseId\\\"].value\"\n const segments: PathSegment[] = [];\n for (const raw of path.split(\".\")) {\n const match = /^([A-Za-z_$][\\w$]*)(?:\\[(.+)\\])?$/.exec(raw);\n if (!match) {\n throw fakeError(\n `Unsupported patch path segment \"${raw}\" (in path \"${path}\"). This fake supports dotted ` +\n `field access (\"a.b.c\"), array index (\"items[0]\"), and keyed lookup (\\`items[_key==\"x\"]\\`). ` +\n `Recursive descent (\"..\") and GROQ filter expressions are not supported.`,\n );\n }\n const key = match[1] as string;\n const selectorRaw = match[2];\n if (selectorRaw === undefined) {\n segments.push({ key });\n continue;\n }\n const indexMatch = /^(\\d+)$/.exec(selectorRaw);\n if (indexMatch) {\n segments.push({ key, selector: { kind: \"index\", index: Number(indexMatch[1]) } });\n continue;\n }\n const keyedMatch = /^([A-Za-z_$][\\w$]*)\\s*==\\s*\"([^\"]*)\"$/.exec(selectorRaw);\n if (keyedMatch) {\n segments.push({\n key,\n selector: { kind: \"keyed\", field: keyedMatch[1] as string, value: keyedMatch[2] as string },\n });\n continue;\n }\n throw fakeError(\n `Unsupported array selector \"[${selectorRaw}]\" in patch path \"${path}\". Supported selectors ` +\n `are a numeric index (e.g. \"[0]\") and a single keyed match (e.g. \\`[_key==\"abc\"]\\`). ` +\n `Multi-key matches and arbitrary GROQ filters are not supported.`,\n );\n }\n return segments;\n}\n\nfunction resolveSelector(array: unknown[], selector: NonNullable<PathSegment[\"selector\"]>): number {\n if (selector.kind === \"index\") return selector.index;\n const idx = array.findIndex(\n (entry) =>\n typeof entry === \"object\" &&\n entry !== null &&\n (entry as Record<string, unknown>)[selector.field] === selector.value,\n );\n if (idx === -1) {\n throw fakeError(\n `Patch path matched no array entry where ${selector.field}==\"${selector.value}\". The array has ` +\n `${array.length} item(s); check the key exists (and that you're patching the right document).`,\n );\n }\n return idx;\n}\n\nfunction getAtPath(doc: SanityDocument, path: string): unknown {\n const segments = parsePath(path);\n let cursor: unknown = doc;\n for (const seg of segments) {\n if (cursor === undefined || cursor === null) return undefined;\n cursor = (cursor as Record<string, unknown>)[seg.key];\n if (seg.selector !== undefined) {\n if (!Array.isArray(cursor)) return undefined;\n const idx = resolveSelector(cursor, seg.selector);\n cursor = cursor[idx];\n }\n }\n return cursor;\n}\n\nfunction setAtPath(doc: SanityDocument, path: string, value: unknown): void {\n const segments = parsePath(path);\n if (segments.length === 0)\n throw fakeError(`Empty patch path — a set/setIfMissing op needs a target field.`);\n let cursor: Record<string, unknown> = doc as unknown as Record<string, unknown>;\n for (let i = 0; i < segments.length - 1; i++) {\n const seg = segments[i] as PathSegment;\n let next = cursor[seg.key];\n if (next === undefined) {\n next = seg.selector?.kind === \"index\" || seg.selector?.kind === \"keyed\" ? [] : {};\n cursor[seg.key] = next;\n }\n if (seg.selector !== undefined) {\n if (!Array.isArray(next)) {\n throw fakeError(\n `Patch path \"${path}\" applies an array selector to field \"${seg.key}\", but that field ` +\n `holds ${describeType(next)}, not an array.`,\n );\n }\n const idx = resolveSelector(next, seg.selector);\n cursor = next[idx] as Record<string, unknown>;\n } else {\n cursor = next as Record<string, unknown>;\n }\n }\n const last = segments[segments.length - 1] as PathSegment;\n if (last.selector !== undefined) {\n const arr = cursor[last.key];\n if (!Array.isArray(arr)) {\n throw fakeError(\n `Patch path \"${path}\" applies an array selector to field \"${last.key}\", but that field ` +\n `holds ${describeType(arr)}, not an array.`,\n );\n }\n const idx = resolveSelector(arr, last.selector);\n arr[idx] = value;\n } else {\n cursor[last.key] = value;\n }\n}\n\nfunction unsetAtPath(doc: SanityDocument, path: string): void {\n const segments = parsePath(path);\n if (segments.length === 0) return;\n let cursor: Record<string, unknown> = doc as unknown as Record<string, unknown>;\n for (let i = 0; i < segments.length - 1; i++) {\n const seg = segments[i] as PathSegment;\n let next = cursor[seg.key];\n if (next === undefined || next === null) return;\n if (seg.selector !== undefined) {\n if (!Array.isArray(next)) return;\n const idx = resolveSelector(next, seg.selector);\n next = next[idx];\n }\n cursor = next as Record<string, unknown>;\n }\n const last = segments[segments.length - 1] as PathSegment;\n if (last.selector !== undefined) {\n const arr = cursor[last.key];\n if (!Array.isArray(arr)) return;\n const idx = resolveSelector(arr, last.selector);\n arr.splice(idx, 1);\n } else {\n delete cursor[last.key];\n }\n}\n\nfunction insertIntoArray(\n doc: SanityDocument,\n position: \"before\" | \"after\" | \"replace\",\n anchor: string,\n items: unknown[],\n): void {\n // anchor is a path that must terminate at an array entry (selector required).\n // For \"replace\", we replace the entry; for \"before\"/\"after\", we insert\n // adjacent to it.\n const segments = parsePath(anchor);\n if (segments.length === 0)\n throw fakeError(`Empty insert anchor — insert() needs an array path to anchor against.`);\n const last = segments[segments.length - 1] as PathSegment;\n if (last.selector === undefined) {\n throw fakeError(\n `Insert anchor \"${anchor}\" must end with an array selector (an index like \"[0]\" or a keyed ` +\n `match like \\`[_key==\"x\"]\\`) so the fake knows where in the array to insert.`,\n );\n }\n\n let cursor: Record<string, unknown> = doc as unknown as Record<string, unknown>;\n for (let i = 0; i < segments.length - 1; i++) {\n const seg = segments[i] as PathSegment;\n const next = cursor[seg.key];\n if (next === undefined || next === null) {\n throw fakeError(\n `Insert anchor \"${anchor}\" traverses field \"${seg.key}\", which is missing on the document. ` +\n `setIfMissing the parent first, or anchor against an existing array.`,\n );\n }\n if (seg.selector !== undefined) {\n if (!Array.isArray(next)) {\n throw fakeError(\n `Insert anchor \"${anchor}\" applies an array selector to field \"${seg.key}\", but it holds ` +\n `${describeType(next)}, not an array.`,\n );\n }\n cursor = next[resolveSelector(next, seg.selector)] as Record<string, unknown>;\n } else {\n cursor = next as Record<string, unknown>;\n }\n }\n const arr = cursor[last.key];\n if (!Array.isArray(arr)) {\n throw fakeError(\n `Insert anchor \"${anchor}\" targets field \"${last.key}\", but it holds ${describeType(arr)}, ` +\n `not an array.`,\n );\n }\n const idx = resolveSelector(arr, last.selector);\n if (position === \"replace\") {\n arr.splice(idx, 1, ...items);\n } else if (position === \"before\") {\n arr.splice(idx, 0, ...items);\n } else {\n arr.splice(idx + 1, 0, ...items);\n }\n}\n","/**\n * Perspective layering — read-side filtering that mirrors Sanity's\n * `perspective` query/client option.\n *\n * The store always holds raw documents. Each fetch builds a *view* of\n * the store filtered by the active perspective before passing the\n * dataset to `groq-js`.\n *\n * Reference: https://www.sanity.io/docs/content-lake/perspectives\n */\n\nimport type { SanityDocument } from \"@sanity/types\";\n\n/**\n * `raw`, `published`, `drafts`, a release stack, or a single release name\n * (`\"rSpring\"`) — which the Content Lake reads as a one-release stack.\n */\nexport type Perspective = \"raw\" | \"published\" | \"drafts\" | (string & {}) | string[];\n\nconst DRAFTS_PREFIX = \"drafts.\";\nconst VERSIONS_PREFIX = \"versions.\";\nconst SYSTEM_PREFIX = \"_.\";\nconst RELEASE_DOC_PREFIX = \"_.releases.\";\n\n/** Strip `drafts.` / `versions.<name>.` prefix to recover the published id. */\nexport function publishedIdOf(id: string): string {\n if (id.startsWith(DRAFTS_PREFIX)) return id.slice(DRAFTS_PREFIX.length);\n if (id.startsWith(VERSIONS_PREFIX)) {\n // versions.<name>.<id> — split off the prefix + name\n const rest = id.slice(VERSIONS_PREFIX.length);\n const dot = rest.indexOf(\".\");\n if (dot === -1) return rest;\n return rest.slice(dot + 1);\n }\n return id;\n}\n\n/** Extract the release name from a `versions.<name>.<id>` id, else undefined. */\nexport function releaseNameOf(id: string): string | undefined {\n if (!id.startsWith(VERSIONS_PREFIX)) return undefined;\n const rest = id.slice(VERSIONS_PREFIX.length);\n const dot = rest.indexOf(\".\");\n if (dot === -1) return undefined;\n return rest.slice(0, dot);\n}\n\nexport const isDraftId = (id: string): boolean => id.startsWith(DRAFTS_PREFIX);\nexport const isVersionId = (id: string): boolean => id.startsWith(VERSIONS_PREFIX);\nexport const isSystemId = (id: string): boolean => id.startsWith(SYSTEM_PREFIX);\nexport const isReleaseDocId = (id: string): boolean => id.startsWith(RELEASE_DOC_PREFIX);\n\n/**\n * Apply a perspective to a raw set of documents and return the view\n * groq-js should evaluate against.\n *\n * For non-raw perspectives, each output document for a non-system id\n * gets `_id` projected to its published id and `_originalId` carrying\n * the source id (matching the wire shape of the real Content Lake).\n */\nexport function applyPerspective(\n docs: readonly SanityDocument[],\n requested: Perspective,\n): SanityDocument[] {\n const perspective = normalizePerspective(requested);\n if (perspective === \"raw\") return [...docs];\n\n if (perspective === \"published\") {\n return docs.filter((d) => !isDraftId(d._id) && !isVersionId(d._id) && !isSystemId(d._id));\n }\n\n if (perspective === \"drafts\") {\n return mergeStack(docs, [], { includeDrafts: true, includeReleaseDocs: false });\n }\n\n // string[] — release stack with optional \"drafts\" entry.\n const stack: string[] = [];\n let includeDrafts = false;\n for (const entry of perspective) {\n if (entry === \"drafts\") {\n includeDrafts = true;\n } else {\n stack.push(entry);\n }\n }\n return mergeStack(docs, stack, { includeDrafts, includeReleaseDocs: true });\n}\n\n/**\n * The perspective as the Content Lake receives it: `previewDrafts` is the\n * deprecated name of `drafts`, and any other string is a comma-joined\n * stack (`\"rA\"`, `\"drafts,rA\"`), the form `@sanity/client` sends.\n */\nfunction normalizePerspective(perspective: Perspective): \"raw\" | \"published\" | \"drafts\" | string[] {\n if (Array.isArray(perspective)) return perspective;\n switch (perspective) {\n case \"raw\":\n return \"raw\";\n case \"published\":\n return \"published\";\n case \"drafts\":\n case \"previewDrafts\":\n return \"drafts\";\n default:\n return perspective.split(\",\");\n }\n}\n\ninterface MergeOptions {\n includeDrafts: boolean;\n includeReleaseDocs: boolean;\n}\n\n/**\n * Walk the raw docs once, group by published id, then for each group\n * pick the highest-priority source according to `releaseStack` and\n * `includeDrafts`. Project the chosen doc to the published id.\n */\nfunction mergeStack(\n docs: readonly SanityDocument[],\n releaseStack: readonly string[],\n options: MergeOptions,\n): SanityDocument[] {\n // Group docs by published id. Track which sources exist.\n type Group = {\n published?: SanityDocument;\n draft?: SanityDocument;\n versions: Map<string, SanityDocument>;\n };\n const groups = new Map<string, Group>();\n const systemReleases: SanityDocument[] = [];\n\n for (const doc of docs) {\n if (isReleaseDocId(doc._id)) {\n if (options.includeReleaseDocs) systemReleases.push(doc);\n continue;\n }\n if (isSystemId(doc._id)) continue; // other system docs are hidden\n const pubId = publishedIdOf(doc._id);\n let group = groups.get(pubId);\n if (group === undefined) {\n group = { versions: new Map() };\n groups.set(pubId, group);\n }\n if (isDraftId(doc._id)) {\n group.draft = doc;\n } else if (isVersionId(doc._id)) {\n const releaseName = releaseNameOf(doc._id);\n if (releaseName !== undefined) group.versions.set(releaseName, doc);\n } else {\n group.published = doc;\n }\n }\n\n const out: SanityDocument[] = [];\n for (const [pubId, group] of groups) {\n // Walk the release stack first, then drafts (if enabled, in its\n // slot — here we model \"drafts after the stack\" matching the\n // typical `[r1, r2, \"drafts\"]` shape), then fall through to\n // published.\n let chosen: SanityDocument | undefined;\n for (const releaseName of releaseStack) {\n const v = group.versions.get(releaseName);\n if (v !== undefined) {\n // version.unpublish stub: hide the doc entirely\n if (isUnpublishStub(v)) {\n chosen = undefined;\n break;\n }\n chosen = v;\n break;\n }\n }\n if (chosen === undefined && options.includeDrafts && group.draft !== undefined) {\n chosen = group.draft;\n }\n if (chosen === undefined && group.published !== undefined) {\n // skip if any release in the stack carried an unpublish stub\n const blocked = releaseStack.some((name) => {\n const v = group.versions.get(name);\n return v !== undefined && isUnpublishStub(v);\n });\n if (!blocked) chosen = group.published;\n }\n if (chosen !== undefined) {\n out.push(projectToPublishedId(chosen, pubId));\n }\n }\n\n return [...out, ...systemReleases];\n}\n\nfunction isUnpublishStub(doc: SanityDocument): boolean {\n const sys = (doc as { _system?: { delete?: boolean } })._system;\n return sys?.delete === true;\n}\n\nfunction projectToPublishedId(doc: SanityDocument, pubId: string): SanityDocument {\n if (doc._id === pubId) return doc;\n return { ...doc, _id: pubId, _originalId: doc._id } as SanityDocument;\n}\n\n/**\n * `getDocument(id)` resolution under a perspective. Returns the chosen\n * source's *raw* doc (not projected), so the caller can decide whether\n * to expose `_originalId` etc. For perspective=\"raw\", this is just\n * `docs.find(d => d._id === id)`.\n */\nexport function resolveDocument(\n docs: readonly SanityDocument[],\n id: string,\n perspective: Perspective,\n): SanityDocument | undefined {\n if (perspective === \"raw\") {\n return docs.find((d) => d._id === id);\n }\n // Match the rules of applyPerspective: resolve to the same chosen\n // doc but key by the requested id (which the caller treated as a\n // published id under non-raw perspectives).\n const view = applyPerspective(docs, perspective);\n return view.find((d) => d._id === id);\n}\n","import type { SanityDocument, SanityDocumentLike } from \"@sanity/types\";\nimport { fakeError } from \"./errors.ts\";\n\n/**\n * In-memory document store backing a TestClient. Closure-scoped — created\n * fresh per test, no global state.\n *\n * The shape mirrors what `groq-js` evaluates against: an array of\n * documents. Internally we keep a Map by `_id` for O(1) lookup, and\n * project to an array on each query.\n */\nexport interface TestStore {\n /** Replace the entire dataset with the given documents. Existing docs are dropped. */\n seed: (documents: SanityDocumentLike[]) => void;\n /** Add documents without dropping existing ones. Conflicts (same _id) overwrite. */\n add: (documents: SanityDocumentLike[]) => void;\n /** Remove all documents. */\n reset: () => void;\n /** All documents as an array, in insertion order. */\n all: () => SanityDocument[];\n /** Get one document by _id, or undefined. */\n get: (id: string) => SanityDocument | undefined;\n /** Insert a fully-formed document by _id. Replaces if it already exists. */\n put: (doc: SanityDocument) => void;\n /** Remove a document by _id. Returns the removed doc or undefined. */\n remove: (id: string) => SanityDocument | undefined;\n /** Snapshot of all docs, useful for assertions. */\n snapshot: () => readonly SanityDocument[];\n}\n\n/**\n * Create a fresh, isolated store. The returned object closes over a private\n * Map; two stores created from two calls do not see each other.\n */\nexport function createTestStore(initial?: SanityDocumentLike[]): TestStore {\n const docs = new Map<string, SanityDocument>();\n\n const promote = (doc: SanityDocumentLike): SanityDocument => {\n if (typeof doc._id !== \"string\" || doc._id.length === 0) {\n throw fakeError(\n `Cannot seed/store a document without a non-empty string \\`_id\\` (got ${JSON.stringify(doc._id)}). ` +\n `This fake never auto-generates ids — give every document an explicit _id. Document: ${JSON.stringify(doc)}`,\n );\n }\n if (typeof doc._type !== \"string\" || doc._type.length === 0) {\n throw fakeError(\n `Document \"${doc._id}\" is missing a non-empty string \\`_type\\` (got ${JSON.stringify(doc._type)}). ` +\n `Every Sanity document needs a _type.`,\n );\n }\n const now = new Date().toISOString();\n return {\n ...doc,\n _id: doc._id,\n _type: doc._type,\n _rev: doc._rev ?? cryptoRandomKey(),\n _createdAt: doc._createdAt ?? now,\n _updatedAt: doc._updatedAt ?? now,\n } as SanityDocument;\n };\n\n const seed = (documents: SanityDocumentLike[]) => {\n docs.clear();\n for (const d of documents) {\n const promoted = promote(d);\n docs.set(promoted._id, promoted);\n }\n };\n\n if (initial) seed(initial);\n\n return {\n seed,\n add: (documents) => {\n for (const d of documents) {\n const promoted = promote(d);\n docs.set(promoted._id, promoted);\n }\n },\n reset: () => docs.clear(),\n all: () => [...docs.values()],\n get: (id) => docs.get(id),\n put: (doc) => {\n docs.set(doc._id, doc);\n },\n remove: (id) => {\n const doc = docs.get(id);\n if (doc) docs.delete(id);\n return doc;\n },\n snapshot: () => [...docs.values()],\n };\n}\n\n/**\n * Generate a stable-ish random key without pulling in a dependency.\n * Use cases: synthesising `_rev` for newly-stored documents.\n */\nexport function cryptoRandomKey(length = 12): string {\n const bytes = new Uint8Array(length);\n // globalThis.crypto is available in Node 22+ and all modern browsers.\n globalThis.crypto.getRandomValues(bytes);\n return [...bytes]\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\")\n .slice(0, length);\n}\n","/**\n * Action handlers for `client.action()` — Content Releases, versions, and\n * document publish/unpublish — plus the `client.releases.*` and\n * `client.createVersion` / `client.unpublishVersion` helpers.\n *\n * Mirrors enough of the real `@sanity/client` action surface for tests\n * to exercise release-aware workflows. Transient states\n * (`scheduling`/`publishing`/`unscheduling`) are skipped — releases\n * transition synchronously between settled states.\n */\n\nimport type { SanityDocument, SanityDocumentLike } from \"@sanity/types\";\n\nimport { fakeError } from \"./errors.ts\";\nimport { PlatformHttpError, type PlatformRequest } from \"./resources.ts\";\nimport { mutationContexts, type MutationGuardImageContext } from \"./mutation-guard.ts\";\nimport { applyPatchOps, type PatchOp } from \"./patch.ts\";\nimport { publishedIdOf } from \"./perspective.ts\";\nimport type { TestStore } from \"./store.ts\";\nimport { cryptoRandomKey } from \"./store.ts\";\n\n/**\n * Document body accepted by `version.create` / `createVersion`. The id\n * is derived from `releaseId + publishedId` so callers may omit `_id`\n * here. `version.replace` requires `_id` since it identifies the doc\n * to replace.\n */\nexport interface DocumentBody {\n _id?: string;\n _type: string;\n [field: string]: unknown;\n}\n\n/** A version copied from a base: `_system.base` names the base and the revision copied. */\ntype VersionFromBase = SanityDocumentLike & {\n _system: { base: { id: string; rev: string | undefined } };\n};\n\nexport type ReleaseState = \"active\" | \"scheduled\" | \"published\" | \"archived\";\n\nexport type ReleaseType = \"asap\" | \"scheduled\" | \"undecided\";\n\nexport interface ReleaseMetadata {\n title?: string;\n description?: string;\n releaseType?: ReleaseType;\n intendedPublishAt?: string;\n}\n\nexport interface ReleaseDocument extends SanityDocument {\n _type: \"system.release\";\n name: string;\n state: ReleaseState;\n metadata: ReleaseMetadata;\n publishAt?: string;\n}\n\nexport type ReleaseAction =\n | {\n actionType: \"sanity.action.release.create\";\n releaseId: string;\n metadata?: Partial<ReleaseMetadata>;\n }\n | {\n actionType: \"sanity.action.release.edit\";\n releaseId: string;\n patch: { set?: Record<string, unknown>; unset?: string[] };\n }\n | { actionType: \"sanity.action.release.schedule\"; releaseId: string; publishAt: string }\n | { actionType: \"sanity.action.release.unschedule\"; releaseId: string }\n | { actionType: \"sanity.action.release.publish\"; releaseId: string }\n | { actionType: \"sanity.action.release.archive\"; releaseId: string }\n | { actionType: \"sanity.action.release.unarchive\"; releaseId: string }\n | { actionType: \"sanity.action.release.delete\"; releaseId: string };\n\nexport type VersionAction =\n | {\n actionType: \"sanity.action.document.version.create\";\n publishedId: string;\n document: DocumentBody;\n releaseId?: string; // inferred from document._id when omitted\n }\n | {\n /** Copy the document at `baseId` into `versionId`, as `@sanity/client` sends `createVersion({ baseId })`. */\n actionType: \"sanity.action.document.version.create\";\n publishedId: string;\n baseId: string;\n versionId: string;\n ifBaseRevisionId?: string;\n }\n | {\n actionType: \"sanity.action.document.version.replace\";\n document: SanityDocumentLike;\n }\n | {\n actionType: \"sanity.action.document.version.discard\";\n versionId: string;\n }\n | {\n actionType: \"sanity.action.document.version.unpublish\";\n versionId: string;\n publishedId: string;\n };\n\n/**\n * Draft↔published document actions — `sanity.action.document.publish`\n * promotes the draft onto the published id (and deletes the draft);\n * `sanity.action.document.unpublish` retracts the published document into a\n * draft (keeping an existing draft untouched) and deletes the published one.\n */\nexport type DocumentAction =\n | {\n actionType: \"sanity.action.document.publish\";\n draftId: string;\n publishedId: string;\n ifDraftRevisionId?: string;\n ifPublishedRevisionId?: string;\n }\n | {\n actionType: \"sanity.action.document.unpublish\";\n draftId: string;\n publishedId: string;\n };\n\nexport type Action = ReleaseAction | VersionAction | DocumentAction;\n\n/**\n * Client-layer interception for document actions. `gate` checks access on\n * each document a publish/unpublish or `version.create` writes, given its net\n * image across the batch: before (`undefined` when created) and after\n * (`undefined` when deleted). A rejection rolls back the whole batch. `guard` receives\n * the batch's net draft and published mutations after staging; a rejection\n * rolls back the whole batch. `request` is the actions request the batch was\n * sent on, which a refused precondition's error names.\n */\nexport interface DocumentActionHooks {\n request?: PlatformRequest;\n gate?: (\n prev: SanityDocument | undefined,\n next: SanityDocument | undefined,\n ) => void | Promise<void>;\n guard?: (contexts: readonly MutationGuardImageContext[]) => void | Promise<void>;\n}\n\nexport interface ActionResult {\n transactionId: string;\n}\n\nconst RELEASE_DOC_PREFIX = \"_.releases.\";\nconst VERSIONS_PREFIX = \"versions.\";\n\nconst releaseDocId = (releaseId: string): string => `${RELEASE_DOC_PREFIX}${releaseId}`;\nconst versionDocId = (releaseId: string, publishedId: string): string =>\n `${VERSIONS_PREFIX}${releaseId}.${publishedId}`;\n\n/** Parse `versions.<release>.<id>` → `{ releaseId, publishedId }`. Throws otherwise. */\nfunction parseVersionId(versionId: string): { releaseId: string; publishedId: string } {\n if (!versionId.startsWith(VERSIONS_PREFIX)) {\n throw fakeError(\n `\"${versionId}\" is not a version document id. Version ids look like ` +\n `\"versions.<releaseId>.<publishedId>\" (e.g. \"versions.summer-drop.product-1\").`,\n );\n }\n const rest = versionId.slice(VERSIONS_PREFIX.length);\n const dot = rest.indexOf(\".\");\n if (dot === -1) {\n throw fakeError(\n `\"${versionId}\" is not a version document id. Version ids look like ` +\n `\"versions.<releaseId>.<publishedId>\" (e.g. \"versions.summer-drop.product-1\").`,\n );\n }\n return { releaseId: rest.slice(0, dot), publishedId: rest.slice(dot + 1) };\n}\n\nfunction getRelease(store: TestStore, releaseId: string): ReleaseDocument {\n const doc = store.get(releaseDocId(releaseId)) as ReleaseDocument | undefined;\n if (doc === undefined) {\n throw fakeError(\n `Release \"${releaseId}\" not found. Create it first with ` +\n `client.releases.create({ releaseId: \"${releaseId}\" }) (or a ` +\n `\"sanity.action.release.create\" action) before editing, scheduling, or adding versions to it.`,\n );\n }\n return doc;\n}\n\nfunction maybeRelease(store: TestStore, releaseId: string): ReleaseDocument | undefined {\n return store.get(releaseDocId(releaseId)) as ReleaseDocument | undefined;\n}\n\nfunction nowIso(): string {\n return new Date().toISOString();\n}\n\nfunction promote(doc: SanityDocumentLike): SanityDocument {\n const now = nowIso();\n return {\n ...doc,\n _id: doc._id as string,\n _type: doc._type as string,\n _rev: cryptoRandomKey(),\n _createdAt: doc._createdAt ?? now,\n _updatedAt: now,\n } as SanityDocument;\n}\n\nfunction bumpRev(doc: SanityDocument, patch: Partial<SanityDocument>): SanityDocument {\n return {\n ...doc,\n ...patch,\n _rev: cryptoRandomKey(),\n _updatedAt: nowIso(),\n } as SanityDocument;\n}\n\nfunction requireActive(release: ReleaseDocument): void {\n if (release.state !== \"active\") {\n throw fakeError(\n `Release \"${release.name}\" is \"${release.state}\", but only \"active\" releases accept version ` +\n `writes (create / replace / discard / unpublish). Unschedule or unarchive it back to ` +\n `\"active\" first, or target a different release.`,\n );\n }\n}\n\n/**\n * Apply a single action to the store. Returns a synthetic transactionId.\n * Throws on any invariant violation — callers wrap multi-action arrays\n * with a snapshot/rollback to keep atomicity. {@link applyActions} checks\n * access and evaluates guard contexts after staging the complete batch.\n */\nexport async function applyAction(\n store: TestStore,\n action: Action,\n request: PlatformRequest = ACTIONS_REQUEST,\n): Promise<ActionResult> {\n switch (action.actionType) {\n case \"sanity.action.release.create\": {\n if (maybeRelease(store, action.releaseId) !== undefined) {\n throw fakeError(\n `Release \"${action.releaseId}\" already exists. release.create expects a new releaseId; ` +\n `use release.edit to change its metadata, or pick a different releaseId.`,\n );\n }\n const now = nowIso();\n const metadata: ReleaseMetadata = {\n releaseType: \"undecided\",\n ...action.metadata,\n };\n const doc: ReleaseDocument = {\n _id: releaseDocId(action.releaseId),\n _type: \"system.release\",\n _rev: cryptoRandomKey(),\n _createdAt: now,\n _updatedAt: now,\n name: action.releaseId,\n state: \"active\",\n metadata,\n };\n store.put(doc);\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.edit\": {\n const release = getRelease(store, action.releaseId);\n const patchOps: PatchOp[] = [];\n if (action.patch.set !== undefined) {\n patchOps.push({ kind: \"set\", props: action.patch.set });\n }\n if (action.patch.unset !== undefined) {\n patchOps.push({ kind: \"unset\", paths: action.patch.unset });\n }\n const patched = applyPatchOps(release, patchOps);\n store.put(bumpRev(patched, {}));\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.schedule\": {\n const release = getRelease(store, action.releaseId);\n if (release.state !== \"active\") {\n throw fakeError(\n `Cannot schedule release \"${action.releaseId}\": it is \"${release.state}\", and only ` +\n `\"active\" releases can be scheduled.`,\n );\n }\n store.put(\n bumpRev(release, {\n state: \"scheduled\",\n publishAt: action.publishAt,\n } as Partial<SanityDocument>),\n );\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.unschedule\": {\n const release = getRelease(store, action.releaseId);\n if (release.state !== \"scheduled\") {\n throw fakeError(\n `Cannot unschedule release \"${action.releaseId}\": it is \"${release.state}\", and only ` +\n `\"scheduled\" releases can be unscheduled.`,\n );\n }\n const next: ReleaseDocument = { ...release, state: \"active\" };\n delete (next as { publishAt?: string }).publishAt;\n store.put(bumpRev(next, {}));\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.publish\": {\n const release = getRelease(store, action.releaseId);\n if (release.state === \"published\") {\n // Idempotent.\n return { transactionId: cryptoRandomKey() };\n }\n if (release.state !== \"active\" && release.state !== \"scheduled\") {\n throw fakeError(\n `Cannot publish release \"${action.releaseId}\": it is \"${release.state}\", and only ` +\n `\"active\" or \"scheduled\" releases can be published.`,\n );\n }\n // Materialise versions onto published ids, then drop the versions.\n const prefix = `${VERSIONS_PREFIX}${action.releaseId}.`;\n const sources = store.all().filter((d) => d._id.startsWith(prefix));\n for (const v of sources) {\n const pubId = v._id.slice(prefix.length);\n const isUnpublishStub = (v as { _system?: { delete?: boolean } })._system?.delete === true;\n if (isUnpublishStub) {\n store.remove(pubId);\n } else {\n const body: SanityDocumentLike = {\n ...(v as SanityDocumentLike),\n _id: pubId,\n };\n // Strip transient fields we projected through the version.\n delete (body as { _originalId?: string })._originalId;\n delete (body as { _system?: unknown })._system;\n const promoted = promote(body);\n // Preserve _createdAt of the existing published doc if present.\n const existing = store.get(pubId);\n if (existing !== undefined) {\n promoted._createdAt = existing._createdAt;\n }\n store.put(promoted);\n }\n store.remove(v._id);\n }\n store.put(bumpRev(release, { state: \"published\" } as Partial<SanityDocument>));\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.archive\": {\n const release = getRelease(store, action.releaseId);\n if (release.state !== \"active\" && release.state !== \"scheduled\") {\n throw fakeError(\n `Cannot archive release \"${action.releaseId}\": it is \"${release.state}\", and only ` +\n `\"active\" or \"scheduled\" releases can be archived.`,\n );\n }\n store.put(bumpRev(release, { state: \"archived\" } as Partial<SanityDocument>));\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.unarchive\": {\n const release = getRelease(store, action.releaseId);\n if (release.state !== \"archived\") {\n throw fakeError(\n `Cannot unarchive release \"${action.releaseId}\": it is \"${release.state}\", and only ` +\n `\"archived\" releases can be unarchived.`,\n );\n }\n store.put(bumpRev(release, { state: \"active\" } as Partial<SanityDocument>));\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.release.delete\": {\n const release = getRelease(store, action.releaseId);\n if (release.state !== \"archived\" && release.state !== \"published\") {\n throw fakeError(\n `Cannot delete release \"${action.releaseId}\": it is \"${release.state}\", and only ` +\n `\"archived\" or \"published\" releases can be deleted. Archive it first ` +\n `(sanity.action.release.archive) or publish it.`,\n );\n }\n // Clean up any leftover version docs (archived releases keep them around).\n const prefix = `${VERSIONS_PREFIX}${action.releaseId}.`;\n for (const v of store.all()) {\n if (v._id.startsWith(prefix)) store.remove(v._id);\n }\n store.remove(release._id);\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.document.version.create\": {\n if (\"baseId\" in action) return createVersionFromBase(store, action, request);\n const releaseId =\n action.releaseId ??\n (action.document._id !== undefined\n ? parseVersionId(action.document._id as string).releaseId\n : undefined);\n if (releaseId === undefined) {\n throw fakeError(\n `version.create needs a release to attach to: pass \\`releaseId\\`, or a \\`document._id\\` ` +\n `shaped \"versions.<releaseId>.<publishedId>\" so the release can be inferred.`,\n );\n }\n const release = getRelease(store, releaseId);\n requireActive(release);\n const vid = versionDocId(releaseId, action.publishedId);\n const doc: SanityDocumentLike = {\n ...action.document,\n _id: vid,\n };\n const existing = store.get(vid);\n const promoted = promote(doc);\n if (existing !== undefined) {\n promoted._createdAt = existing._createdAt;\n }\n store.put(promoted);\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.document.version.replace\": {\n const id = action.document._id;\n if (typeof id !== \"string\") {\n throw fakeError(\n `version.replace needs \\`document._id\\` (a version id like ` +\n `\"versions.<releaseId>.<publishedId>\") to identify which version document to replace.`,\n );\n }\n const { releaseId } = parseVersionId(id);\n const release = getRelease(store, releaseId);\n requireActive(release);\n const existing = store.get(id);\n const promoted = promote(action.document);\n if (existing !== undefined) {\n promoted._createdAt = existing._createdAt;\n }\n store.put(promoted);\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.document.version.discard\": {\n const { releaseId } = parseVersionId(action.versionId);\n const release = getRelease(store, releaseId);\n requireActive(release);\n store.remove(action.versionId);\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.document.version.unpublish\": {\n const { releaseId } = parseVersionId(action.versionId);\n const release = getRelease(store, releaseId);\n requireActive(release);\n const doc: SanityDocumentLike = {\n _id: action.versionId,\n _type: \"stub\",\n _system: { delete: true },\n } as SanityDocumentLike;\n store.put(promote(doc));\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.document.publish\": {\n requireDraftOf(action);\n const draft = store.get(action.draftId);\n if (draft === undefined) {\n throw fakeError(\n `Cannot publish \"${action.publishedId}\": draft \"${action.draftId}\" not found. ` +\n `document.publish promotes an existing draft onto the published id — create the ` +\n `draft first (e.g. client.createOrReplace({ _id: \"${action.draftId}\", ... })).`,\n );\n }\n const published = store.get(action.publishedId);\n requireRevisionMatch(request, action.draftId, draft._rev, action.ifDraftRevisionId);\n requireRevisionMatch(\n request,\n action.publishedId,\n published?._rev,\n action.ifPublishedRevisionId,\n );\n const promoted = promote({ ...draft, _id: action.publishedId });\n if (published !== undefined) {\n promoted._createdAt = published._createdAt;\n }\n store.put(promoted);\n store.remove(action.draftId);\n return { transactionId: cryptoRandomKey() };\n }\n\n case \"sanity.action.document.unpublish\": {\n requireDraftOf(action);\n const published = store.get(action.publishedId);\n if (published === undefined) {\n throw fakeError(\n `Cannot unpublish \"${action.publishedId}\": no published document with that _id exists. ` +\n `document.unpublish retracts a live published document into \"${action.draftId}\".`,\n );\n }\n // The lake keeps an existing draft untouched; only a missing one is\n // materialised from the published content.\n if (store.get(action.draftId) === undefined) {\n const draft = promote({ ...published, _id: action.draftId });\n draft._createdAt = published._createdAt;\n store.put(draft);\n }\n store.remove(action.publishedId);\n return { transactionId: cryptoRandomKey() };\n }\n }\n}\n\n/** Throw unless the action's `draftId` is the draft of its `publishedId`. */\nfunction requireDraftOf(action: {\n actionType: string;\n draftId: string;\n publishedId: string;\n}): void {\n if (publishedIdOf(action.draftId) === action.publishedId && action.draftId !== action.publishedId)\n return;\n throw fakeError(\n `${action.actionType}: draftId \"${action.draftId}\" is not the draft of publishedId ` +\n `\"${action.publishedId}\" — expected draftId \"drafts.${action.publishedId}\".`,\n );\n}\n\n/**\n * `version.create` with `baseId`: the platform copies the base document's\n * content, `_type` included, into `versionId`, which names a version of\n * `publishedId`, and records the base in `_system.base`. Only the write on\n * the version is access-checked: gradient `version_create.go` preloads the\n * base read-only, and the transaction store checks permissions on written\n * documents alone.\n */\nasync function createVersionFromBase(\n store: TestStore,\n action: Extract<VersionAction, { baseId: string }>,\n request: PlatformRequest,\n): Promise<ActionResult> {\n const { releaseId, publishedId } = parseVersionId(action.versionId);\n if (publishedId !== action.publishedId) {\n throw fakeError(\n `version.create: versionId \"${action.versionId}\" is not a version of publishedId ` +\n `\"${action.publishedId}\" (expected \"${versionDocId(releaseId, action.publishedId)}\").`,\n );\n }\n requireActive(getRelease(store, releaseId));\n const base = store.get(action.baseId);\n if (base === undefined) {\n throw fakeError(\n `Cannot create version \"${action.versionId}\": base document \"${action.baseId}\" not found. ` +\n `version.create with \\`baseId\\` copies an existing document; pass \\`document\\` instead ` +\n `when there is nothing to copy.`,\n );\n }\n requireRevisionMatch(request, action.baseId, base._rev, action.ifBaseRevisionId);\n // A new revision and timestamps: the version is a new document unless it replaces one.\n const existing = store.get(action.versionId);\n const copy: VersionFromBase = {\n ...base,\n _id: action.versionId,\n _createdAt: existing?._createdAt ?? nowIso(),\n _system: { base: { id: base._id, rev: base._rev } },\n };\n const promoted = promote(copy);\n store.put(promoted);\n return { transactionId: cryptoRandomKey() };\n}\n\n/** The request an action error names when its caller gives none. */\nconst ACTIONS_REQUEST: PlatformRequest = {\n method: \"POST\",\n path: \"/data/actions\",\n url: \"/data/actions\",\n};\n\n/**\n * Enforce an `ifDraftRevisionId` / `ifPublishedRevisionId` / `ifBaseRevisionId`\n * precondition when given. A mismatch is the Content Lake's 409\n * `documentRevisionIDDoesNotMatchError`, and a missing document its 404\n * `documentNotFoundError` (gradient `pkg/documents/actions/publish.go`\n * `publishExecute`, `pkg/documents/errors.go`, `pkg/documents/mutation/patch.go`).\n */\nfunction requireRevisionMatch(\n request: PlatformRequest,\n documentId: string,\n actualRev: string | undefined,\n expectedRev: string | undefined,\n): void {\n if (expectedRev === undefined || actualRev === expectedRev) return;\n if (actualRev === undefined) {\n throw new PlatformHttpError({\n ...request,\n statusCode: 404,\n statusMessage: \"Not Found\",\n body: {\n error: {\n description: `The document with the ID ${JSON.stringify(documentId)} was not found`,\n id: documentId,\n type: \"documentNotFoundError\",\n },\n },\n });\n }\n throw new PlatformHttpError({\n ...request,\n statusCode: 409,\n statusMessage: \"Conflict\",\n body: {\n error: {\n currentRevisionID: actualRev,\n description:\n `Document ${JSON.stringify(documentId)} has unexpected revision ID ` +\n `(${JSON.stringify(actualRev)}), expected ${JSON.stringify(expectedRev)}`,\n expectedRevisionID: expectedRev,\n type: \"documentRevisionIDDoesNotMatchError\",\n },\n },\n });\n}\n\n/**\n * Apply an action or a list of actions atomically. On any failure all\n * mutations within the batch are rolled back to the pre-batch snapshot.\n * `hooks` intercepts document publish/unpublish actions\n * ({@link DocumentActionHooks}) and evaluates their net changes as one batch.\n */\nexport async function applyActions(\n store: TestStore,\n actions: Action | Action[],\n hooks?: DocumentActionHooks,\n): Promise<ActionResult> {\n const list = Array.isArray(actions) ? actions : [actions];\n const snapshot = new Map<string, SanityDocument | undefined>();\n const guardedIds = new Set<string>();\n // Documents whose net change `hooks.gate` checks, in check order.\n const gatedIds = new Set<string>();\n const remember = (id: string) => {\n if (!snapshot.has(id)) snapshot.set(id, store.get(id));\n };\n\n try {\n let last: ActionResult = { transactionId: cryptoRandomKey() };\n for (const a of list) {\n // Pre-record everything we might touch so rollback is precise.\n switch (a.actionType) {\n case \"sanity.action.release.create\":\n case \"sanity.action.release.edit\":\n case \"sanity.action.release.schedule\":\n case \"sanity.action.release.unschedule\":\n case \"sanity.action.release.archive\":\n case \"sanity.action.release.unarchive\":\n remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);\n break;\n case \"sanity.action.release.delete\": {\n remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);\n // Deleting a release also removes the version documents it keeps.\n const prefix = `${VERSIONS_PREFIX}${a.releaseId}.`;\n for (const d of store.all()) {\n if (d._id.startsWith(prefix)) remember(d._id);\n }\n break;\n }\n case \"sanity.action.release.publish\": {\n remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);\n const prefix = `${VERSIONS_PREFIX}${a.releaseId}.`;\n for (const d of store.all()) {\n if (d._id.startsWith(prefix)) {\n remember(d._id);\n remember(d._id.slice(prefix.length));\n }\n }\n break;\n }\n case \"sanity.action.document.version.create\": {\n if (\"baseId\" in a) {\n remember(a.versionId);\n gatedIds.add(a.versionId);\n break;\n }\n // version.create derives its id from releaseId + publishedId,\n // so we don't rely on document._id being set.\n const releaseId =\n a.releaseId ??\n (typeof a.document._id === \"string\"\n ? parseVersionId(a.document._id).releaseId\n : undefined);\n if (releaseId !== undefined) {\n remember(versionDocId(releaseId, a.publishedId));\n gatedIds.add(versionDocId(releaseId, a.publishedId));\n }\n break;\n }\n case \"sanity.action.document.version.replace\":\n if (typeof a.document._id === \"string\") remember(a.document._id);\n break;\n case \"sanity.action.document.version.discard\":\n case \"sanity.action.document.version.unpublish\":\n remember(a.versionId);\n break;\n case \"sanity.action.document.publish\":\n remember(a.draftId);\n remember(a.publishedId);\n // Preserve the lake-facing order: published write, then draft delete.\n guardedIds.add(a.publishedId);\n guardedIds.add(a.draftId);\n gatedIds.add(a.publishedId);\n gatedIds.add(a.draftId);\n break;\n case \"sanity.action.document.unpublish\":\n remember(a.draftId);\n remember(a.publishedId);\n // Preserve the lake-facing order: draft create, then published delete.\n guardedIds.add(a.draftId);\n guardedIds.add(a.publishedId);\n gatedIds.add(a.publishedId);\n gatedIds.add(a.draftId);\n break;\n }\n last = await applyAction(store, a, hooks?.request);\n }\n // Like the Content Lake, check each document's net change once the whole\n // batch is staged: a draft unpublished and published again is never written.\n for (const id of gatedIds) {\n const before = snapshot.get(id);\n const after = store.get(id);\n if (before !== after) await hooks?.gate?.(before, after);\n }\n const contexts = mutationContexts(\n [...guardedIds].map((id) => ({ before: snapshot.get(id), after: store.get(id) })),\n );\n await hooks?.guard?.(contexts);\n return last;\n } catch (err) {\n for (const [id, doc] of snapshot) {\n if (doc === undefined) {\n store.remove(id);\n } else {\n store.put(doc);\n }\n }\n throw err;\n }\n}\n","import type { SanityDocument, SanityDocumentLike } from \"@sanity/types\";\nimport type { PatchHandle, PatchOp } from \"./patch.ts\";\n\n/**\n * A transaction collects mutations and applies them atomically at commit\n * time. Mutations are applied in declaration order; if any one fails, the\n * whole transaction throws and no writes land in the store.\n *\n * Subset of `@sanity/client`'s Transaction. Same shape, narrower surface.\n */\nexport type TxnMutation =\n | { kind: \"create\"; doc: SanityDocumentLike }\n | { kind: \"createOrReplace\"; doc: SanityDocumentLike }\n | { kind: \"createIfNotExists\"; doc: SanityDocumentLike }\n | { kind: \"delete\"; id: string }\n | { kind: \"patch\"; documentId: string; ops: PatchOp[]; ifRev?: string };\n\nexport interface TransactionHandle {\n create: (doc: SanityDocumentLike) => TransactionHandle;\n createOrReplace: (doc: SanityDocumentLike) => TransactionHandle;\n createIfNotExists: (doc: SanityDocumentLike) => TransactionHandle;\n delete: (id: string) => TransactionHandle;\n /** Accept either a Patch builder (drained at commit) or an inline doc id + ops. */\n patch: (\n documentIdOrPatch: string | PatchHandle,\n ops?: PatchOp[],\n ifRev?: string,\n ) => TransactionHandle;\n commit: (options?: TxnCommitOptions) => Promise<TxnCommitResult>;\n /** Test-only introspection — not on real Transaction. */\n toJSON: () => TxnMutation[];\n}\n\nexport interface TxnCommitOptions {\n /**\n * Shape the resolved value (mirrors `@sanity/client`):\n * - default (both unset) → {@link MultipleMutationResult}\n * - `returnDocuments: true` → the resulting document(s)\n * - `returnFirst: true` with `returnDocuments` → a single document\n * - `returnDocuments: false` → an id-shaped mutation result\n */\n returnDocuments?: boolean;\n returnFirst?: boolean;\n /** Override the synthetic transaction id (mirrors `client.transaction(undefined, { transactionId })`). */\n transactionId?: string;\n /**\n * Request tag, recorded on the mutation log entry for assertions.\n * `null` mirrors `@sanity/client`'s explicit-suppression form and is\n * recorded as-is. No other behavior is simulated.\n */\n tag?: string | null;\n}\n\n/** The operation recorded per affected document, matching `@sanity/client`. */\nexport type MutationOperation = \"create\" | \"update\" | \"delete\" | \"none\";\n\n/** Result of a mutation when `returnFirst` resolves true and ids are requested. */\nexport interface SingleMutationResult {\n transactionId: string;\n documentId: string;\n results: { id: string; operation: MutationOperation }[];\n}\n\n/** Result of a mutation when ids are requested for all affected documents. */\nexport interface MultipleMutationResult {\n transactionId: string;\n documentIds: string[];\n results: { id: string; operation: MutationOperation }[];\n}\n\nexport type TxnCommitResult =\n | MultipleMutationResult\n | SingleMutationResult\n | SanityDocument\n | SanityDocument[];\n\nexport interface TxnExecutor {\n apply: (mutations: TxnMutation[], options?: TxnCommitOptions) => Promise<TxnCommitResult>;\n}\n\nexport function createTransaction(executor: TxnExecutor): TransactionHandle {\n const mutations: TxnMutation[] = [];\n\n const handle: TransactionHandle = {\n create: (doc) => {\n mutations.push({ kind: \"create\", doc });\n return handle;\n },\n createOrReplace: (doc) => {\n mutations.push({ kind: \"createOrReplace\", doc });\n return handle;\n },\n createIfNotExists: (doc) => {\n mutations.push({ kind: \"createIfNotExists\", doc });\n return handle;\n },\n delete: (id) => {\n mutations.push({ kind: \"delete\", id });\n return handle;\n },\n patch: (documentIdOrPatch, ops, ifRev) => {\n if (typeof documentIdOrPatch === \"string\") {\n mutations.push({\n kind: \"patch\",\n documentId: documentIdOrPatch,\n ops: ops ?? [],\n ...(ifRev !== undefined ? { ifRev } : {}),\n });\n } else {\n const json = documentIdOrPatch.toJSON();\n mutations.push({\n kind: \"patch\",\n documentId: json.id,\n ops: json.ops,\n ...(json.ifRev !== undefined ? { ifRev: json.ifRev } : {}),\n });\n }\n return handle;\n },\n commit: (options) => executor.apply(mutations, options),\n toJSON: () => [...mutations],\n };\n\n return handle;\n}\n","/**\n * The URL `@sanity/client` requests, built the way it builds it, so that a\n * fake error's `response.url` is the URL the real client's error carries\n * (`url: res.url || reqUrl`, @sanity/client 8.16.0 `src/http/errors.ts:47-54`).\n *\n * - Base: `<protocol>://<projectId>.<host>/v<apiVersion>` on the project\n * host, `<apiHost>/v<apiVersion>` otherwise; the CDN base swaps in\n * `apicdn.sanity.io` for the default API host (`src/config.ts:258-267`).\n * - Path: `<base>/<uri without its leading slash>` (`src/data/dataMethods.ts`\n * `_getUrl`).\n * - Query: the request's `query` object appended after the URL's own query\n * string with `&`, or after `?`, skipping `undefined` values (get-it 9\n * `buildFetchArgs`, the transport of @sanity/client 8).\n */\n\nimport { fakeError } from \"./errors.ts\";\n\n/** `@sanity/client`'s default `apiHost`. */\nexport const DEFAULT_API_HOST = \"https://api.sanity.io\";\n\n/** `@sanity/client`'s default `apiVersion`, which it warns about. */\nexport const DEFAULT_API_VERSION = \"1\";\n\nconst DEFAULT_CDN_HOST = \"apicdn.sanity.io\";\n\n/**\n * Encoded GROQ query strings this long or longer are sent as a `POST` body\n * instead of in the URL (`getQuerySizeLimit`).\n */\nexport const QUERY_SIZE_LIMIT = 11264;\n\n/** A request's `query` object, in the key order `@sanity/client` builds it. */\nexport type QueryEntries = readonly (readonly [string, string | number | boolean | undefined])[];\n\n/**\n * `requestTagPrefix` as `@sanity/client` keeps it (8.17.0 `src/config.ts:239-244`):\n * unset when empty, else with its trailing dots dropped, so a prefix of only\n * dots stays `\"\"`. An empty prefix adds nothing to a tag\n * (`src/data/dataMethods.ts:1043-1046`).\n */\nexport function normalizeTagPrefix(prefix: string | undefined): string | undefined {\n return prefix ? prefix.replace(/\\.+$/, \"\") : undefined;\n}\n\n/**\n * `apiVersion` as `@sanity/client` keeps it: without a leading `v`, and\n * `1`, `X` or a `YYYY-MM-DD` date, else the error `createClient` throws.\n */\nexport function normalizeApiVersion(apiVersion: string): string {\n const version = `${apiVersion}`.replace(/^v/, \"\");\n if (version === \"1\" || version === \"X\") return version;\n const date = new Date(version);\n if (!/^\\d{4}-\\d{2}-\\d{2}$/.test(version) || !(date.getTime() > 0)) {\n throw fakeError(\n `Invalid API version string, expected \\`1\\` or date in format \\`YYYY-MM-DD\\` (got ${JSON.stringify(apiVersion)}), ` +\n \"the same rule @sanity/client applies to `apiVersion`.\",\n );\n }\n return version;\n}\n\n/** The API base URLs of a handle, as `@sanity/client`'s `config()` sets `url` and `cdnUrl`. */\nexport function apiBaseUrls(args: {\n apiHost: string;\n apiVersion: string;\n /** The project whose host the handle addresses, or `undefined` for the global host. */\n projectId: string | undefined;\n}): { url: string; cdnUrl: string } {\n const { apiHost, apiVersion, projectId } = args;\n if (projectId === undefined) {\n const url = `${apiHost}/v${apiVersion}`;\n return { url, cdnUrl: url };\n }\n const [protocol, host] = apiHost.split(\"://\", 2) as [string, string];\n const cdnHost = apiHost === DEFAULT_API_HOST ? DEFAULT_CDN_HOST : host;\n return {\n url: `${protocol}://${projectId}.${host}/v${apiVersion}`,\n cdnUrl: `${protocol}://${projectId}.${cdnHost}/v${apiVersion}`,\n };\n}\n\n/** `<base>/<uri>`, with `query` merged into the URL as the transport merges it: before a fragment. */\nexport function requestUrl(base: string, uri: string, query: QueryEntries = []): string {\n const url = `${base}/${uri.replace(/^\\//, \"\")}`;\n const params = new URLSearchParams();\n for (const [key, value] of query) {\n if (value !== undefined) params.append(key, `${value}`);\n }\n const search = params.toString();\n if (search === \"\") return url;\n const fragmentStart = url.indexOf(\"#\");\n const beforeFragment = fragmentStart === -1 ? url : url.slice(0, fragmentStart);\n const fragment = fragmentStart === -1 ? \"\" : url.slice(fragmentStart);\n return `${beforeFragment}${beforeFragment.includes(\"?\") ? \"&\" : \"?\"}${search}${fragment}`;\n}\n\n/**\n * The `?query=…&$param=…` string a GROQ query travels in, as\n * `encodeQueryString` builds it for `fetch()`.\n */\nexport function encodeGroqQuery(query: string, params: Record<string, unknown> = {}): string {\n const search = new URLSearchParams();\n search.append(\"query\", query);\n for (const [key, value] of Object.entries(params)) {\n if (value !== undefined) search.append(`$${key}`, JSON.stringify(value));\n }\n return `?${search}`;\n}\n","/**\n * Internal tokens: JWTs a Sanity service signs with its own key, which the\n * Content Lake verifies itself instead of looking up a session.\n *\n * Gradient parses the bearer of every request it serves\n * (`pkg/server/httpapi/middlewares/sessions/session.go:70-84`):\n *\n * 1. `JWTSigningAuthority.ParseClaimFromToken`\n * (`pkg/security/jwt_signing_authority.go:74-91`) checks the JWT with\n * `github.com/golang-jwt/jwt` v3.2.2 against its one RS256 public key;\n * the header's `kid` is not read. A failure answers 400 `httpBadRequest`,\n * `invalid JWT token \"<token>\": <jwt error>`.\n * 2. `NewClaimsFromMap` (`pkg/security/tokens.go:60-176`) reads the claims:\n * `i` the identity, `g` the grants as `{ s: path, p: permissions }`, `x`\n * the expiry in epoch seconds. A grant it cannot parse answers 400\n * `httpBadRequest` with the grant's own message, without the\n * `invalid JWT token` prefix.\n * 3. `Claims.IsExpired` (`tokens.go:178-180`): an `x` at or before now\n * answers 403 `permissionDeniedError`,\n * `Token has expired (at <time>)`. A token without `x` does not expire.\n *\n * What the fake does not model throws a fake error instead of a guessed\n * answer: the claims `tokenType`, `guid`, `s`, `z`, `audit`, `act`, `exp`,\n * `iat` and `nbf`; an `x` that is not a number; a `g` that is not a list of\n * `{ s: string, p: string[] }`; a header or claims segment that is not a\n * JSON object. A missing or empty `i` and a `g…` global user id (other than\n * `god`) are legal to gradient up to its claims transfer, so their fake\n * error comes on a request, after the 400 and 403 checks.\n */\n\nimport { createPublicKey, verify, type KeyObject } from \"node:crypto\";\n\nimport type { TestClientIdentity } from \"./access-control.ts\";\nimport { fakeError } from \"./errors.ts\";\nimport { PlatformHttpError, type PlatformRequest } from \"./resources.ts\";\nimport { badRequest, grantsError, quoted } from \"./token-grants.ts\";\n\n/** What gradient makes of a bearer it verified: the identity and its expiry, or its 400. */\nexport type InternalTokenCheck =\n | {\n identity: TestClientIdentity;\n /** The `x` claim, epoch seconds; `undefined` when the token does not expire. */\n expiresAt: number | undefined;\n /** Why the fake cannot act as the identity once gradient admits the token, if it cannot. */\n unmodelled?: string;\n }\n | { refused: string };\n\n/** The signing methods jwt-go v3.2.2 registers (`hmac.go`, `rsa.go`, `rsa_pss.go`, `ecdsa.go`, `ed25519.go`, `none.go`). */\nconst SIGNING_METHODS = new Set([\n ...[\"HS\", \"RS\", \"PS\", \"ES\"].flatMap((family) => [\"256\", \"384\", \"512\"].map((n) => family + n)),\n \"EdDSA\",\n \"none\",\n]);\n\n/** `base64.RawURLEncoding`'s alphabet. */\nconst BASE64_URL = /^[A-Za-z0-9_-]$/;\n\n/** The claims jwt-go's `MapClaims.Valid` checks against the clock (`map_claims.go:96-119`), which the fake does not model. */\nconst TIMED_CLAIMS = [\"exp\", \"iat\", \"nbf\"];\n\n/** The claims `NewClaimsFromMap` reads that the fake does not model. */\nconst UNMODELLED_CLAIMS = [\"tokenType\", \"guid\", \"s\", \"z\", \"audit\", \"act\"];\n\n/** Parse `internalTokenKeys` PEMs once, at client creation. */\nexport function parseInternalTokenKeys(pems: readonly string[]): KeyObject[] {\n return pems.map((pem, index) => {\n let key: KeyObject | undefined;\n try {\n key = createPublicKey(pem);\n } catch {\n key = undefined;\n }\n // Gradient reads its key with `jwt.ParseRSAPublicKeyFromPEM`.\n if (key?.asymmetricKeyType !== \"rsa\") {\n throw fakeError(\n `createTestClient(): \\`internalTokenKeys[${index}]\\` is not an RSA public key PEM.`,\n );\n }\n return key;\n });\n}\n\n/** Whether `token` has a JWT's three segments: the only bearers the fake verifies as internal tokens. */\nexport function isJwtShaped(token: string): boolean {\n return token.split(\".\").length === 3;\n}\n\n/**\n * `DecodeSegment`, `base64.RawURLEncoding.DecodeString`: the bytes, or Go's\n * `CorruptInputError` text (`encoding/base64/base64.go:303-305`) at the\n * first byte it cannot decode. Line breaks are skipped, as Go skips them.\n */\nfunction decodeSegment(segment: string): Buffer | string {\n const bytes = Buffer.from(segment, \"utf8\");\n let digits = 0;\n for (const [index, byte] of bytes.entries()) {\n const char = String.fromCharCode(byte);\n if (char === \"\\n\" || char === \"\\r\") continue;\n if (!BASE64_URL.test(char)) return `illegal base64 data at input byte ${index}`;\n digits++;\n }\n // One digit left over cannot hold a byte (`decodeQuantum`, `j == 1`).\n if (digits % 4 === 1) return `illegal base64 data at input byte ${bytes.length - 1}`;\n return Buffer.from(segment.replace(/[\\r\\n]/g, \"\"), \"base64url\");\n}\n\n/** The source text of each number by the object holding it, as `UseJSONNumber` keeps it (`jwt_signing_authority.go:70-72`). */\nconst NUMBER_SOURCES = new WeakMap<object, Map<string, string>>();\n\n/** A segment's JSON object; a fake error for anything else, whose Go decoder text the fake does not model. */\nfunction jsonObject(bytes: Buffer, segment: string): Record<string, unknown> {\n let value: unknown;\n try {\n value = JSON.parse(bytes.toString(\"utf8\"), function (\n this: unknown,\n key: string,\n parsed: unknown,\n context?: { source?: string },\n ) {\n if (typeof parsed === \"number\" && context?.source !== undefined) {\n const holder = this as object;\n const sources = NUMBER_SOURCES.get(holder) ?? new Map<string, string>();\n sources.set(key, context.source);\n NUMBER_SOURCES.set(holder, sources);\n }\n return parsed;\n } as (key: string, value: unknown) => unknown);\n } catch {\n value = undefined;\n }\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n throw fakeError(\n `internal token: its ${segment} is not a JSON object. Gradient answers with Go's JSON ` +\n \"decoder error there, which the fake does not model.\",\n );\n }\n return value as Record<string, unknown>;\n}\n\n/**\n * `asInt64` of a `json.Number` (`tokens.go:342-347`): `strconv.ParseInt(s, 10, 64)`,\n * whose errors read `strconv.ParseInt: parsing \"<s>\": <reason>` (Go\n * `strconv/number.go:258-260`, `internal/strconv/atoi.go:25-31`).\n */\nfunction int64Of(source: string): number | string {\n const failed = (reason: string) =>\n `invalid expiry: expected int64: strconv.ParseInt: parsing ${quoted(source)}: ${reason}`;\n if (!/^[+-]?[0-9]+$/.test(source)) return failed(\"invalid syntax\");\n const value = BigInt(source);\n if (value > 2n ** 63n - 1n || value < -(2n ** 63n)) return failed(\"value out of range\");\n return Number(value);\n}\n\n/** Whether `signature` signs `signed` with any of `keys`, RSASSA-PKCS1-v1_5 over SHA-256. */\nfunction signedByAny(signed: string, signature: Buffer, keys: readonly KeyObject[]): boolean {\n return keys.some((key) => {\n try {\n return verify(\"RSA-SHA256\", Buffer.from(signed), key, signature);\n } catch {\n return false;\n }\n });\n}\n\n/**\n * `ParseClaimFromToken`: jwt-go's `Parser.ParseWithClaims`\n * (`parser.go:23-148`) in its order, then gradient's algorithm check. The\n * error text is the description after `invalid JWT token \"<token>\": `.\n */\nfunction jwtError(\n token: string,\n keys: readonly KeyObject[],\n): { claims: Record<string, unknown> } | { error: string } {\n const [headerSegment, claimsSegment, signatureSegment] = token.split(\".\") as [\n string,\n string,\n string,\n ];\n const headerBytes = decodeSegment(headerSegment);\n if (typeof headerBytes === \"string\") {\n return {\n error: token.toLowerCase().startsWith(\"bearer \")\n ? \"tokenstring should not contain 'bearer '\"\n : headerBytes,\n };\n }\n const header = jsonObject(headerBytes, \"header\");\n const claimsBytes = decodeSegment(claimsSegment);\n if (typeof claimsBytes === \"string\") return { error: claimsBytes };\n const claims = jsonObject(claimsBytes, \"claims segment\");\n const alg = header.alg;\n if (typeof alg !== \"string\") return { error: \"signing method (alg) is unspecified.\" };\n if (!SIGNING_METHODS.has(alg)) return { error: \"signing method (alg) is unavailable.\" };\n // The keyfunc: gradient's signing credentials are RS256 (`deploy/config.yml.sh`).\n if (alg !== \"RS256\") return { error: `token uses algorithm ${quoted(alg)}, expected \"RS256\"` };\n // A signature error replaces a claims-validation error (`parser.go:59-80`), so the\n // unmodelled timed claims matter only on a token that verifies.\n const signature = decodeSegment(signatureSegment);\n if (typeof signature === \"string\") return { error: signature };\n if (!signedByAny(`${headerSegment}.${claimsSegment}`, signature, keys)) {\n return { error: \"crypto/rsa: verification error\" };\n }\n const timed = TIMED_CLAIMS.filter((key) => key in claims);\n if (timed.length > 0) {\n throw fakeError(\n `internal token: the fake does not model jwt-go's ${timed.map((key) => `\"${key}\"`).join(\", \")} ` +\n 'check(s); gradient\\'s tokens expire through \"x\".',\n );\n }\n return { claims };\n}\n\n/** `NewClaimsFromMap` for the claims the fake models: the identity and its grants, or a grant's 400 text. */\nfunction identityOf(claims: Record<string, unknown>): InternalTokenCheck {\n const unmodelledClaims = UNMODELLED_CLAIMS.filter((key) => key in claims);\n if (unmodelledClaims.length > 0) {\n throw fakeError(\n `internal token: the fake does not model the ${unmodelledClaims.map((key) => `\"${key}\"`).join(\", \")} ` +\n 'claim(s); it reads \"i\", \"g\" and \"x\".',\n );\n }\n const { i: id, g: grantList = [], x: rawExpiry } = claims;\n // `NewClaimsFromMap` reads `x`, then `i`, then `g` (`tokens.go:76-127`).\n let expiresAt: number | undefined;\n if (rawExpiry !== undefined && rawExpiry !== null) {\n if (typeof rawExpiry !== \"number\") {\n throw fakeError(\n `internal token: \"x\" must be a number of epoch seconds; got ${JSON.stringify(rawExpiry)}. ` +\n \"Gradient's answer names Go's type for it, which the fake does not model.\",\n );\n }\n const parsed = int64Of(NUMBER_SOURCES.get(claims)?.get(\"x\") ?? String(rawExpiry));\n if (typeof parsed === \"string\") return { refused: parsed };\n expiresAt = parsed;\n }\n if (id !== undefined && id !== null && typeof id !== \"string\") {\n return { refused: 'token field \"i\" must be a string' };\n }\n let unmodelled: string | undefined;\n if (typeof id !== \"string\" || id === \"\") {\n unmodelled = `internal token: \"i\" is ${JSON.stringify(id)}; the fake models a token with an identity only.`;\n } else if (id.startsWith(\"g\") && id !== \"god\") {\n // A global user id is moved to its project user (`claims_transfer.go:66-84`); \"god\" passes through.\n unmodelled =\n `internal token: identity \"${id}\" starts with \"g\", which gradient resolves to a project ` +\n \"user through its claims transfer; the fake does not model that.\";\n }\n if (!Array.isArray(grantList)) {\n throw fakeError(\n `internal token: \"g\" must be a list of { s, p } grants; got ${JSON.stringify(grantList)}.`,\n );\n }\n const grants = grantList.map((entry: unknown) => {\n const { s: path, p: permissions } = (entry ?? {}) as { s?: unknown; p?: unknown };\n if (\n typeof path !== \"string\" ||\n !Array.isArray(permissions) ||\n !permissions.every((permission) => typeof permission === \"string\")\n ) {\n throw fakeError(\n `internal token: each grant must be { s: string, p: string[] }; got ${JSON.stringify(entry)}.`,\n );\n }\n return { path, permissions: permissions as string[] };\n });\n const refused = grantsError(grants);\n if (refused !== undefined) return { refused };\n return {\n identity: { user: { id: typeof id === \"string\" ? id : \"\" }, grants },\n expiresAt,\n ...(unmodelled !== undefined ? { unmodelled } : {}),\n };\n}\n\n/**\n * Verify `token` as gradient does, up to the expiry check, which\n * {@link internalTokenRefusal} makes on each request.\n */\nexport function checkInternalToken(token: string, keys: readonly KeyObject[]): InternalTokenCheck {\n const parsed = jwtError(token, keys);\n if (\"error\" in parsed) return { refused: `invalid JWT token ${quoted(token)}: ${parsed.error}` };\n return identityOf(parsed.claims);\n}\n\n/** Go's `time.Time.String()` of a whole second in UTC: `2006-01-02 15:04:05 +0000 UTC`. */\nfunction goTime(epochSeconds: number): string {\n const date = new Date(epochSeconds * 1000);\n if (Number.isNaN(date.getTime()) || date.getUTCFullYear() < 1000) {\n throw fakeError(\n `internal token: \"x\" ${epochSeconds} is before year 1000; Go's formatting of it is not modelled.`,\n );\n }\n const iso = date.toISOString();\n return `${iso.slice(0, 10)} ${iso.slice(11, 19)} +0000 UTC`;\n}\n\n/** The Content Lake's answer to a request carrying `check`'s token, or `undefined` when it admits it. */\nexport function internalTokenRefusal(\n request: PlatformRequest,\n check: InternalTokenCheck,\n): PlatformHttpError | undefined {\n if (\"refused\" in check) return badRequest(request, check.refused);\n if (check.expiresAt === undefined || Date.now() < check.expiresAt * 1000) {\n if (check.unmodelled !== undefined) throw fakeError(check.unmodelled);\n return undefined;\n }\n return new PlatformHttpError({\n ...request,\n statusCode: 403,\n statusMessage: \"Forbidden\",\n body: {\n error: {\n description: `Token has expired (at ${goTime(check.expiresAt)})`,\n type: \"permissionDeniedError\",\n },\n },\n });\n}\n","/**\n * Graceful-degradation layer for the parts of `@sanity/client` this fake\n * doesn't implement.\n *\n * The client object is a fixed surface. Without a guard, reaching for a\n * member the fake doesn't have (`client.listen`, `client.assets.upload`,\n * a typo) yields a bare `TypeError: x is not a function` — no hint that\n * it's an intentional gap in a *fake*, no pointer to a workaround. The\n * `Proxy` built here turns every such access into an actionable error,\n * and lets tests fill gaps via a stub registry.\n *\n * Resolution order for a property read on the wrapped client:\n * 1. Real implemented member → passed straight through.\n * 2. Thenable / inspection probes (`then`, symbols, …) → pass through\n * so `await client` and `console.log(client)` stay well-behaved.\n * 3. User-registered stub (`client.stub(name, fn)`) → returned.\n * 4. Known-but-unimplemented `@sanity/client` member → a guard that\n * throws a curated, workaround-bearing error on call or deeper access.\n * 5. Anything else (typo / unknown) → a guard that throws a\n * \"no such method\" error listing the supported surface.\n */\n\nconst ISSUES_URL = \"https://github.com/sanity-labs/client-fake-for-test/issues\";\n\n/** Members this fake actually implements — surfaced in error messages. */\nexport const SUPPORTED_SURFACE = [\n \"fetch\",\n \"getDocument\",\n \"getDocuments\",\n \"create\",\n \"createOrReplace\",\n \"createIfNotExists\",\n \"delete\",\n \"patch\",\n \"transaction\",\n \"mutate\",\n \"action\",\n \"releases\",\n \"createVersion\",\n \"unpublishVersion\",\n \"request (only with accessControl or a bound token)\",\n \"getDataUrl\",\n \"config\",\n \"withConfig\",\n] as const;\n\ninterface UnsupportedEntry {\n /** What the real member is. */\n what: string;\n /** What to do instead. */\n workaround: string;\n}\n\n/**\n * Curated map of real `@sanity/client` members the fake deliberately omits.\n * Keyed by the top-level property name; nested access (e.g.\n * `assets.upload`) is reported with the full dotted path.\n */\nexport const UNSUPPORTED_MEMBERS: Record<string, UnsupportedEntry> = {\n listen: {\n what: \"real-time listener (returns an RxJS Observable of mutation events)\",\n workaround:\n \"Test your subscription logic in isolation, or register a fake: \" +\n `client.stub(\"listen\", () => myFakeObservable).`,\n },\n observable: {\n what: \"the RxJS-flavoured mirror of the client API\",\n workaround:\n \"Use the Promise-based methods and wrap them with `from()` if you need an Observable.\",\n },\n clone: {\n what: \"clone the client with the same config\",\n workaround: \"Use `withConfig({})` — it returns a sibling handle into the same store.\",\n },\n getUrl: {\n what: \"build an absolute API URL for a path\",\n workaround: \"Hard-code the expected URL in your test, or stub it.\",\n },\n assets: {\n what: \"asset upload / metadata namespace\",\n workaround: \"Seed asset documents into the store directly with `seed()` / `add()`.\",\n },\n agent: {\n what: \"Agent Actions namespace (generate / transform / …)\",\n workaround: `Stub the call site: client.stub(\"agent\", myFakeAgent).`,\n },\n projects: {\n what: \"project-management namespace\",\n workaround: \"Out of scope for a data-plane fake — stub the call site.\",\n },\n users: {\n what: \"user-management namespace\",\n workaround: \"Out of scope for a data-plane fake — stub the call site.\",\n },\n live: {\n what: \"Live Content API namespace\",\n workaround: \"Out of scope — stub the call site or test the consumer separately.\",\n },\n mediaLibrary: {\n what: \"Media Library namespace\",\n workaround: \"Out of scope — seed documents directly or stub the call site.\",\n },\n request: {\n what: \"generic HTTP request hook — only wired up when the handle has an acting identity\",\n workaround:\n \"Create the client with `accessControl`, or bind a registered token \" +\n \"(`identities` + `token` / `withConfig({ token })`), to enable request() (it serves \" +\n \"/users/me, the ACL path, /projects/<projectId>, /projects/<projectId>/users/<ids>, \" +\n \"/projects/<projectId>/features, the History API, resource lookup and the Access API). \" +\n \"Or register a stub.\",\n },\n dryRun: {\n what: \"not a method — did you mean the `{ dryRun }` mutation option?\",\n workaround: \"Pass `{ dryRun: true }` as the second arg to create / mutate / delete.\",\n },\n};\n\n/**\n * Property names read by promise-resolution, serialization, and test\n * frameworks while probing an unknown object. These must resolve to\n * `undefined` (not a throwing guard) so `await client`,\n * `JSON.stringify(client)`, and matcher diffs stay well-behaved. Members\n * inherited from `Object.prototype` (`toString`, `valueOf`, …) already\n * pass through via the `in` check and don't need listing here.\n */\nconst PROBE_NAMES = new Set([\n \"then\",\n \"catch\",\n \"finally\",\n \"toJSON\",\n \"asymmetricMatch\",\n \"$$typeof\",\n \"nodeType\",\n \"_isMockFunction\",\n]);\n\n/** Build the multi-line error body shared by both guard flavours. */\nfunction guardMessage(path: string, detail: string, workaround: string): string {\n return [\n `[client-fake-for-test] \\`client.${path}\\` ${detail}`,\n \"\",\n \"This is an in-memory fake of @sanity/client. It implements:\",\n ` ${SUPPORTED_SURFACE.join(\", \")}.`,\n \"\",\n `→ ${workaround}`,\n \"\",\n `Register a stub to fill any gap: client.stub(${JSON.stringify(path.split(\".\")[0])}, handler)`,\n `If this should be supported, open an issue: ${ISSUES_URL}`,\n ].join(\"\\n\");\n}\n\n/**\n * A callable, infinitely-chainable guard. Reading any property returns a\n * deeper guard (so `client.assets.upload` reports the full path), and\n * calling at any depth throws the actionable error. `then` / symbols\n * resolve to `undefined` so `await` and inspection don't trip the guard.\n */\nfunction makeGuard(path: string, message: string): unknown {\n const thrower = () => {\n throw new Error(message);\n };\n return new Proxy(thrower, {\n apply: () => thrower(),\n get: (_t, prop) => {\n if (typeof prop === \"symbol\") return undefined;\n if (PROBE_NAMES.has(prop)) return undefined;\n // Re-derive the message for the deeper path so the error names the\n // exact member that was reached for.\n const childPath = `${path}.${String(prop)}`;\n const top = path.split(\".\")[0] as string;\n const entry = UNSUPPORTED_MEMBERS[top];\n const detail = entry ? `is not implemented (${entry.what}).` : \"is not implemented.\";\n const workaround = entry?.workaround ?? \"Register a stub or use a different test strategy.\";\n return makeGuard(childPath, guardMessage(childPath, detail, workaround));\n },\n });\n}\n\n/** Outcome of a recorded call, reported once a (possibly async) result is known. */\nexport type CallSettler = (outcome: { result?: unknown; error?: unknown }) => void;\n\nexport interface GuardConfig {\n /**\n * Resolve a registered stub for an unimplemented member, or `undefined`.\n * Checked only for members the client doesn't already implement, so\n * stubs fill gaps without shadowing core behaviour.\n */\n getStub: (name: string) => unknown;\n /**\n * Called synchronously when a recorded method is invoked, with the\n * method name and its arguments. Returns a settler the wrapper invokes\n * once the result (or rejection) is known. Omit to disable recording.\n */\n onCall?: (method: string, args: unknown[]) => CallSettler;\n /**\n * Top-level member names whose calls are recorded via `onCall`. A\n * recorded member that resolves to a namespace object (e.g. `releases`)\n * has each of its methods recorded as `name.method`.\n */\n recorded?: Set<string>;\n}\n\n/** Wrap a function so each call is reported to `onCall` (args now, outcome on settle). */\nfunction wrapRecorded(\n method: string,\n fn: (...args: unknown[]) => unknown,\n onCall: NonNullable<GuardConfig[\"onCall\"]>,\n): (...args: unknown[]) => unknown {\n return (...args: unknown[]) => {\n const settle = onCall(method, args);\n try {\n const out = fn(...args);\n if (\n out !== null &&\n typeof out === \"object\" &&\n typeof (out as PromiseLike<unknown>).then === \"function\"\n ) {\n return (out as Promise<unknown>).then(\n (result) => {\n settle({ result });\n return result;\n },\n (error: unknown) => {\n settle({ error });\n throw error;\n },\n );\n }\n settle({ result: out });\n return out;\n } catch (error) {\n settle({ error });\n throw error;\n }\n };\n}\n\n/** Wrap a namespace object so its method calls record as `prefix.method`. */\nfunction wrapNamespace(\n ns: Record<string, unknown>,\n prefix: string,\n onCall: NonNullable<GuardConfig[\"onCall\"]>,\n): Record<string, unknown> {\n return new Proxy(ns, {\n get(target, prop, receiver) {\n const value = Reflect.get(target, prop, receiver);\n if (typeof prop === \"string\" && typeof value === \"function\") {\n return wrapRecorded(`${prefix}.${prop}`, value as (...a: unknown[]) => unknown, onCall);\n }\n return value;\n },\n });\n}\n\n/**\n * Wrap a client object so unimplemented members fail informatively,\n * registered stubs are served, and recorded methods report their calls.\n *\n * @param client - the fully-built client object (plain literal).\n * @param config - stub resolver plus optional call-recording hooks.\n */\nexport function wrapWithGuards<T extends object>(client: T, config: GuardConfig): T {\n const { getStub, onCall, recorded } = config;\n return new Proxy(client, {\n get(target, prop, receiver) {\n if (typeof prop === \"symbol\") return Reflect.get(target, prop, receiver);\n if (prop in target) {\n const value = Reflect.get(target, prop, receiver);\n if (onCall !== undefined && recorded?.has(prop) === true) {\n if (typeof value === \"function\") {\n return wrapRecorded(prop, value as (...a: unknown[]) => unknown, onCall);\n }\n if (value !== null && typeof value === \"object\") {\n return wrapNamespace(value as Record<string, unknown>, prop, onCall);\n }\n }\n return value;\n }\n // Promise / serialization / framework probes on a non-thenable object.\n if (PROBE_NAMES.has(prop)) return undefined;\n\n const stub = getStub(prop);\n if (stub !== undefined) return stub;\n\n const entry = UNSUPPORTED_MEMBERS[prop];\n if (entry !== undefined) {\n return makeGuard(\n prop,\n guardMessage(prop, `is not implemented (${entry.what}).`, entry.workaround),\n );\n }\n return makeGuard(\n prop,\n guardMessage(\n prop,\n \"is not a method on this fake (unknown member or unimplemented @sanity/client API).\",\n \"Check for a typo, register a stub, or use a different test strategy.\",\n ),\n );\n },\n });\n}\n","import type { KeyObject } from \"node:crypto\";\n\nimport type { SanityDocument, SanityDocumentLike } from \"@sanity/types\";\nimport { evaluate, parse } from \"groq-js\";\n\nimport {\n canAccess,\n enforceWriteAccess,\n makeRequestImpl,\n projectUserOf,\n type DocumentValuePermission,\n type TestClientAccessControl,\n type TestClientIdentity,\n} from \"./access-control.ts\";\nimport {\n applyActions,\n type Action,\n type ActionResult,\n type DocumentBody,\n type ReleaseDocument,\n type ReleaseMetadata,\n} from \"./actions.ts\";\nimport { fakeError } from \"./errors.ts\";\nimport { createDocumentHistory, type DocumentHistory } from \"./history.ts\";\nimport {\n enforceMutationGuards,\n mutationContexts,\n type MutationGuard,\n type MutationGuardContext,\n type MutationGuardImageContext,\n} from \"./mutation-guard.ts\";\nimport { applyPatchOps, createPatch, type PatchHandle, type PatchOp } from \"./patch.ts\";\nimport { applyPerspective, isSystemId, resolveDocument, type Perspective } from \"./perspective.ts\";\nimport {\n capabilitiesOf,\n dataUrl,\n DEFAULT_WRITE,\n datasetParts,\n projectApiDown,\n sessionHostMismatch,\n sessionNotFound,\n validateResource,\n type PlatformRequest,\n type TestClientResource,\n} from \"./resources.ts\";\nimport { createTestStore, cryptoRandomKey, type TestStore } from \"./store.ts\";\nimport {\n createTransaction,\n type MultipleMutationResult,\n type MutationOperation,\n type SingleMutationResult,\n type TransactionHandle,\n type TxnCommitOptions,\n type TxnCommitResult,\n type TxnMutation,\n} from \"./transaction.ts\";\nimport {\n apiBaseUrls,\n DEFAULT_API_HOST,\n DEFAULT_API_VERSION,\n encodeGroqQuery,\n normalizeApiVersion,\n normalizeTagPrefix,\n QUERY_SIZE_LIMIT,\n requestUrl,\n type QueryEntries,\n} from \"./request-url.ts\";\nimport {\n checkInternalToken,\n internalTokenRefusal,\n isJwtShaped,\n parseInternalTokenKeys,\n type InternalTokenCheck,\n} from \"./internal-token.ts\";\nimport { servedByProjectApi } from \"./platform-api.ts\";\nimport { refusedToken } from \"./token-grants.ts\";\nimport { wrapWithGuards, type CallSettler } from \"./unsupported.ts\";\n\n/**\n * Data-plane subset of `@sanity/client`'s surface, with multi-dataset support.\n *\n * One `createTestClient()` call creates a registry of stores, one per\n * resource: a `(projectId, dataset)` pair, or a Media Library, Canvas or\n * dashboard named by `resource: { type, id }` exactly as `@sanity/client`\n * names it. The returned client points at one of them.\n * `withConfig({ projectId, dataset })` or `withConfig({ resource })` returns\n * a new handle into the same registry but pointing at a different store —\n * useful for workflows where definitions live in one dataset and instances\n * live in another.\n *\n * Two separate `createTestClient()` calls have separate registries, so\n * tests stay isolated from each other.\n *\n * Methods not on the real client:\n *\n * - `seed`, `add`, `reset`, `snapshot`, `store` — bulk-load helpers,\n * scoped to the current dataset.\n * - `simulateConcurrentEdit()` — force the next N revision-guarded writes\n * to a document to 409, for optimistic-locking retry tests.\n * - `datasets()` / `resources()` — list the stores known in this registry.\n *\n * Methods deliberately omitted:\n *\n * - Project / agent / asset / live / listen APIs.\n */\nexport interface FetchOptions {\n /** Override the client's configured perspective for this read. */\n perspective?: Perspective;\n /**\n * When `false`, resolve to a {@link RawQueryResponse} (`{ query, ms,\n * result }`) instead of the bare result — mirrors `@sanity/client`.\n * Defaults to `true`.\n */\n filterResponse?: boolean;\n /**\n * With `filterResponse: false`, set to `false` to omit `query` from the\n * raw response. Ignored when `filterResponse` is not `false`.\n */\n returnQuery?: boolean;\n /**\n * Request tag, recorded on the {@link CallRecord} for assertions.\n * `null` mirrors `@sanity/client`'s explicit-suppression form and is\n * recorded as-is. No other behavior is simulated.\n */\n tag?: string | null;\n /** Accepted for signature parity; not simulated. */\n signal?: AbortSignal;\n}\n\n/**\n * Shape returned by `fetch` when `filterResponse: false`. Matches\n * `@sanity/client`'s `RawQueryResponse`. `query` is omitted when the\n * caller also passes `returnQuery: false`.\n */\nexport interface RawQueryResponse<R> {\n query?: string;\n ms: number;\n result: R;\n}\n\n/**\n * A single mutation in the raw array form accepted by `mutate()` — the\n * same shape `@sanity/client` accepts. The inline `patch` form mirrors\n * the real client's keyed patch object.\n */\nexport type FakeMutation =\n | { create: SanityDocumentLike }\n | { createOrReplace: SanityDocumentLike }\n | { createIfNotExists: SanityDocumentLike }\n | { delete: { id: string } }\n | {\n patch: {\n id: string;\n ifRevisionID?: string;\n set?: Record<string, unknown>;\n setIfMissing?: Record<string, unknown>;\n unset?: string[];\n inc?: Record<string, number>;\n dec?: Record<string, number>;\n insert?: {\n before?: string;\n after?: string;\n replace?: string;\n items: unknown[];\n };\n };\n };\n\n/** Append-only record of one committed transaction (a single mutation is a 1-entry transaction). */\nexport interface MutationLogEntry {\n transactionId: string;\n projectId: string;\n dataset: string;\n /** The resource the transaction targeted, on a `resource`-configured handle. */\n resource?: TestClientResource;\n mutations: TxnMutation[];\n /** Request tag the commit carried, when one was passed (`null` = explicit suppression). */\n tag?: string | null;\n}\n\n/**\n * One recorded invocation of a client method — the spy layer behind\n * `client.calls()` / `client.callCount()`. `result` holds the resolved\n * value (awaited, for async methods); `error` holds the thrown/rejected\n * value. Builder-returning methods (`patch`, `transaction`) record the\n * builder as `result`; their eventual writes show up in the mutation log.\n */\nexport interface CallRecord {\n /** Method name; namespace methods are dotted, e.g. `releases.create`. */\n method: string;\n /** Arguments as passed (stored by reference, like `vi.fn`). */\n args: unknown[];\n /** Resolved/returned value, set once the call settles successfully. */\n result?: unknown;\n /** Thrown or rejected value, set if the call failed. */\n error?: unknown;\n /** Request tag from the call's options, when one was passed (`null` = explicit suppression). */\n tag?: string | null;\n projectId: string;\n dataset: string;\n /** The resource the call targeted, on a `resource`-configured handle. */\n resource?: TestClientResource;\n /**\n * The token of the handle that made the call, after `withConfig`\n * overrides; absent on a handle without one.\n */\n token?: string;\n /** The `apiVersion` of the handle that made the call, as its `config()` reports it. */\n apiVersion?: string;\n}\n\n/**\n * Where each recorded method carries its options bag, for {@link tagFromArgs}.\n * Positional (not \"last object arg\") because `fetch` params may legitimately\n * contain a `tag` key that is a GROQ param, not a request tag. Methods whose\n * tag only arrives at commit time (`patch`, `transaction`) surface it on the\n * {@link MutationLogEntry} instead.\n */\nconst OPTIONS_ARG_POSITION: Record<string, number> = {\n fetch: 2,\n getDocument: 1,\n getDocuments: 1,\n create: 1,\n createOrReplace: 1,\n createIfNotExists: 1,\n delete: 1,\n mutate: 1,\n action: 1,\n request: 0,\n};\n\n/** The request tag a call's options carried — `undefined` when absent. */\nfunction tagFromArgs(method: string, args: unknown[]): string | null | undefined {\n const position = OPTIONS_ARG_POSITION[method];\n if (position === undefined) return undefined;\n const options = args[position];\n if (typeof options !== \"object\" || options === null || !(\"tag\" in options)) return undefined;\n return (options as { tag?: string | null }).tag;\n}\n\n/** Client methods whose calls are recorded for `calls()` / `callCount()`. */\nconst RECORDED_METHODS = new Set([\n \"fetch\",\n \"getDocument\",\n \"getDocuments\",\n \"create\",\n \"createOrReplace\",\n \"createIfNotExists\",\n \"delete\",\n \"patch\",\n \"transaction\",\n \"mutate\",\n \"action\",\n \"releases\",\n \"createVersion\",\n \"unpublishVersion\",\n \"request\",\n]);\n\n/** A platform component {@link TestClient.simulateOutage} can take down: `\"project-api\"` is populus. */\nexport type PlatformComponent = \"project-api\";\n\nexport interface TestClient {\n // --- query ---\n fetch: <T = unknown>(\n query: string,\n params?: Record<string, unknown>,\n options?: FetchOptions,\n ) => Promise<T>;\n getDocument: <T = SanityDocument>(id: string, options?: FetchOptions) => Promise<T | undefined>;\n getDocuments: <T = SanityDocument>(\n ids: string[],\n options?: FetchOptions,\n ) => Promise<(T | null)[]>;\n /**\n * Sanity-style HTTP request hook. Present iff the handle has an\n * acting identity — created with `accessControl`, or bound to a\n * registered token (`identities` + `token`). The engine's access\n * resolver pokes `/users/me` and the configured ACL path through\n * this — without it the engine throws on any verb that needs an\n * actor. Also serves the resource's history, ACL, lookup and Access API\n * endpoints, or the platform's refusal where the resource has none.\n */\n request?: <T>(opts: {\n url?: string;\n uri?: string;\n signal?: AbortSignal;\n tag?: string | null;\n }) => Promise<T>;\n\n /**\n * `@sanity/client`'s `getDataUrl(operation, path)`: `<resource\n * base>/<operation>/<path>` on a `resource`-configured handle,\n * `/data/<operation>/<dataset>/<path>` otherwise. Pass the result to\n * `request()` to reach e.g. the History API.\n */\n getDataUrl: (operation: string, path?: string) => string;\n\n // --- mutate ---\n create: <T extends SanityDocumentLike>(\n doc: T,\n options?: MutationOptions,\n ) => Promise<T & SystemFields>;\n createOrReplace: <T extends SanityDocumentLike>(\n doc: T,\n options?: MutationOptions,\n ) => Promise<T & SystemFields>;\n createIfNotExists: <T extends SanityDocumentLike>(\n doc: T,\n options?: MutationOptions,\n ) => Promise<T & SystemFields>;\n delete: (id: string, options?: MutationOptions) => Promise<{ _id: string } | null>;\n patch: (documentId: string) => PatchHandle;\n transaction: () => TransactionHandle;\n /**\n * Apply a batch of mutations in the raw array form (or a `patch()` /\n * `transaction()` builder) — mirrors `@sanity/client`'s `mutate()`.\n * Atomic: any failure rolls the whole batch back. The resolved shape\n * follows `options.returnDocuments` / `returnFirst` exactly like the\n * real client (default → {@link MultipleMutationResult}).\n */\n mutate: (\n operations: FakeMutation[] | PatchHandle | TransactionHandle,\n options?: TxnCommitOptions,\n ) => Promise<TxnCommitResult>;\n\n // --- actions / releases ---\n /**\n * Dispatch one or more release / version / document actions against the\n * store. Returns a synthetic `transactionId`. Multi-action arrays are\n * applied atomically — any failure rolls back the batch. The document\n * publish/unpublish actions run through the access gate and expose their\n * net draft/published mutations to `mutationGuard`; release/version actions\n * apply unhooked.\n */\n action: (action: Action | Action[], options?: ActionOptions) => Promise<ActionResult>;\n /** Helpers mirroring `@sanity/client`'s `client.releases.*` surface. */\n releases: ReleasesHandle;\n /**\n * Helper for `sanity.action.document.version.create`: writes `document`, or,\n * given `baseId` (as `@sanity/client` takes it), copies that document into\n * `versions.<releaseId>.<publishedId>`.\n */\n createVersion: (\n params:\n | { releaseId: string; publishedId: string; document: DocumentBody }\n | { releaseId: string; publishedId: string; baseId: string; ifBaseRevisionId?: string },\n ) => Promise<ActionResult>;\n /** Helper for `sanity.action.document.version.unpublish`. */\n unpublishVersion: (params: { releaseId: string; publishedId: string }) => Promise<ActionResult>;\n\n // --- config ---\n /**\n * Current `(projectId, dataset)` or `resource`, active perspective, and which API host\n * the handle models (`useProjectHostname`). `token` is present iff the\n * handle is bound to a registered identity.\n */\n config: () => {\n /**\n * Absent on a handle created global-hosted (`useProjectHostname: false`)\n * without a `projectId`, as in `@sanity/client`.\n */\n projectId?: string;\n dataset: string;\n /** Present iff the handle is `resource`-configured. */\n resource?: TestClientResource;\n perspective: Perspective;\n requestTagPrefix?: string;\n token?: string;\n useProjectHostname: boolean;\n /** The API host, `https://api.sanity.io` unless configured. */\n apiHost: string;\n /** The API version, without a leading `v`; `\"1\"` unless configured, as in `@sanity/client`. */\n apiVersion: string;\n /** Whether GET reads of the data API go to the API CDN; `true` unless configured `false`. */\n useCdn: boolean;\n };\n /**\n * Return a new client handle pointing at a different `(projectId, dataset)`\n * within the SAME backing registry. Either field can be omitted to keep\n * the current value. Two clients produced by `withConfig` see each\n * other's writes if they point at the same `(projectId, dataset)`.\n */\n withConfig: (\n config: Partial<{\n projectId: string;\n dataset: string;\n /**\n * Point the sibling at a resource, named as `@sanity/client`'s\n * `resource` config names it. While set, it decides the store (over\n * `projectId` / `dataset`), as in `@sanity/client`. Pass `undefined`\n * to drop it and address `(projectId, dataset)` again.\n */\n resource: TestClientResource | undefined;\n perspective: Perspective;\n /** Prefix composed with request tags and used alone for untagged requests; `undefined` unsets it. */\n requestTagPrefix: string | undefined;\n /**\n * Model the global API host (`false`) instead of the project host\n * (default `true`). A global-hosted sibling's `/users/me` serves the\n * identity's account-global record (`globalUser`, falling back to the\n * project record) — mirroring `@sanity/client`, where a non-project\n * config routes requests to `api.sanity.io`.\n */\n useProjectHostname: boolean;\n /** See {@link CreateTestClientOptions.apiHost}. */\n apiHost: string;\n /** See {@link CreateTestClientOptions.apiVersion}. */\n apiVersion: string;\n /** See {@link CreateTestClientOptions.useCdn}. */\n useCdn: boolean;\n /**\n * Rebind the acting identity (and its grants) on a sibling that shares\n * the same store — the test analogue of a token-scoped client. Lets two\n * actors write the same dataset, e.g. for `identity()`-based guards.\n * Mutually exclusive with `token`.\n */\n accessControl: TestClientAccessControl;\n /**\n * Bind the sibling to a token registered in the family's identity\n * registry (`createTestClient({ identities })` / `addIdentity`): its\n * `/users/me` serves that identity's record for the sibling's project\n * and host, its ACL path serves that identity's grants, and\n * reads/writes are gated through them — the token determines\n * identity, exactly like production. Unknown tokens throw. Mutually\n * exclusive with `accessControl`.\n */\n token: string;\n }>,\n ) => TestClient;\n\n // --- test helpers (not on real SanityClient) ---\n /** Replace the current dataset with the given documents. */\n seed: (documents: SanityDocumentLike[]) => void;\n /** Add documents to the current dataset without dropping existing ones. */\n add: (documents: SanityDocumentLike[]) => void;\n /** Drop all documents in the current dataset. */\n reset: () => void;\n /** Read-only snapshot of all documents in the current dataset. */\n snapshot: () => readonly SanityDocument[];\n /** Direct store access for the current dataset. */\n store: () => TestStore;\n /**\n * Simulate `count` concurrent edits to `id` in the current dataset: the\n * next `count` revision-guarded mutations (those passing `ifRevisionId`)\n * to that document throw a 409-shaped conflict, as if another client\n * committed between the caller's read and write. Mutations that don't\n * pass `ifRevisionId` are unaffected.\n *\n * Reach for this when the code under test reads a document's `_rev` and\n * commits with `ifRevisionId` internally — so you can't stage the race\n * by mutating the document first (the code would just re-read the new\n * revision and its commit would match). The counter is consumed one per\n * guarded commit, so `count` lets a test drive a recover-after-N-losses\n * path or exhaust a retry budget, deterministically and without relying\n * on promise interleaving.\n *\n * Note: this forces the conflict *outcome* — it short-circuits the\n * revision comparison rather than changing the stored `_rev`. The code\n * under test sees the identical 409 error a real lost race produces; the\n * stored revision is left untouched.\n */\n simulateConcurrentEdit: (id: string, count?: number) => void;\n /** List `(projectId, dataset)` pairs known in this registry. */\n datasets: () => { projectId: string; dataset: string }[];\n /** List every resource known in this registry — datasets as `{ type: \"dataset\", id: \"<projectId>.<dataset>\" }`. */\n resources: () => TestClientResource[];\n /**\n * Register a handler for a `@sanity/client` member the fake doesn't\n * implement (e.g. `listen`, `assets`). Accessing that member on the\n * client then returns the handler instead of throwing the\n * \"not implemented\" guard error. Shared across sibling `withConfig`\n * handles. Stubs fill gaps only — they don't shadow implemented methods.\n */\n stub: (name: string, handler: unknown) => void;\n /** The currently-registered stubs, keyed by member name. */\n stubs: () => Record<string, unknown>;\n /**\n * Register (or upsert) a token→identity entry in the family's shared\n * registry. Siblings already bound to `token` resolve lazily, so a\n * re-registration takes effect on them immediately. Registering an\n * identity does not bind it — pass `token` at `createTestClient` or\n * `withConfig({ token })` to act as it.\n */\n addIdentity: (token: string, identity: TestClientIdentity) => void;\n /** Snapshot of the registered token→identity entries. */\n identities: () => Record<string, TestClientIdentity>;\n /**\n * Revoke a registered token, as when it expires or is deleted: it stays\n * registered and still binds, but from the next request on every Content\n * Lake and platform request made with it (on any handle) rejects with the\n * platform's 401 `Session not found`, and nothing is written. Revocation\n * is permanent for the family; `addIdentity` does not restore it. Unknown\n * tokens throw.\n */\n revokeIdentity: (token: string) => void;\n /**\n * Take a platform component down, as in an outage: from the next request\n * on, every request it serves, on every handle of the family (siblings\n * made before and after the call), throws its 5xx answer. `\"project-api\"`\n * is populus: `request()` of `/users/me`, the project, organization and\n * Access APIs, resource lookup and ACLs throws {@link PlatformServerError}\n * 500. The Content Lake (queries, documents, mutations, actions, history)\n * keeps answering. The outage lasts for the rest of the family's life.\n */\n simulateOutage: (component: PlatformComponent) => void;\n /**\n * Every mutation committed through this client's registry, flattened\n * and in order — `create` / `createOrReplace` / `createIfNotExists` /\n * `delete` / `patch`, whether issued directly, via `transaction()`, or\n * via `mutate()`. The spy hook for \"assert exactly what writes my code\n * submitted\".\n */\n mutations: () => TxnMutation[];\n /**\n * Every committed transaction, in order. A direct mutation is recorded\n * as a one-mutation transaction. Each entry carries its synthetic\n * `transactionId` and the `(projectId, dataset)` it targeted.\n */\n transactions: () => MutationLogEntry[];\n /** Clear the recorded mutation / transaction log. */\n clearLog: () => void;\n /**\n * Every recorded method call, in order — the spy layer for \"was `fetch`\n * called, how many times, and with what params\". Records the\n * `@sanity/client`-surface methods (`fetch`, `getDocument`, `create`,\n * `patch`, `mutate`, `releases.*`, …), not the test-only helpers.\n * Pass a method name to filter (e.g. `calls(\"fetch\")`); namespace\n * methods are dotted (`calls(\"releases.create\")`). Shared across\n * `withConfig` siblings.\n */\n calls: (method?: string) => CallRecord[];\n /** Number of recorded calls, optionally filtered by method name. */\n callCount: (method?: string) => number;\n /** Clear the recorded call log. */\n clearCalls: () => void;\n}\n\nexport interface ReleasesHandle {\n create: (params: {\n releaseId: string;\n metadata?: Partial<ReleaseMetadata>;\n }) => Promise<ActionResult & { releaseId: string; metadata: ReleaseMetadata }>;\n edit: (params: {\n releaseId: string;\n patch: { set?: Record<string, unknown>; unset?: string[] };\n }) => Promise<ActionResult>;\n get: (params: { releaseId: string }) => Promise<ReleaseDocument | undefined>;\n fetchDocuments: (params: { releaseId: string }) => Promise<SanityDocument[]>;\n schedule: (params: { releaseId: string; publishAt: string }) => Promise<ActionResult>;\n unschedule: (params: { releaseId: string }) => Promise<ActionResult>;\n publish: (params: { releaseId: string }) => Promise<ActionResult>;\n archive: (params: { releaseId: string }) => Promise<ActionResult>;\n unarchive: (params: { releaseId: string }) => Promise<ActionResult>;\n delete: (params: { releaseId: string }) => Promise<ActionResult>;\n}\n\nexport interface MutationOptions {\n ifRevisionId?: string;\n /**\n * Shape the resolved value, mirroring `@sanity/client`:\n * - default → the mutated document\n * - `returnDocuments: false` → an id-shaped mutation result\n * ({@link SingleMutationResult} / {@link MultipleMutationResult})\n * - `returnFirst: false` with documents → an array\n *\n * The static return type reflects the default (the document); pass a\n * type argument or cast when requesting an id-shaped result.\n */\n returnDocuments?: boolean;\n returnFirst?: boolean;\n /** Override the synthetic transaction id used in the result and log. */\n transactionId?: string;\n /** When `true`, validate + shape the result but don't persist. */\n dryRun?: boolean;\n /** Accepted for signature parity; not simulated. */\n visibility?: \"sync\" | \"async\" | \"deferred\";\n /** Accepted for signature parity; not simulated. */\n autoGenerateArrayKeys?: boolean;\n /**\n * Request tag, recorded on the {@link CallRecord} and the\n * {@link MutationLogEntry} for assertions. `null` mirrors\n * `@sanity/client`'s explicit-suppression form and is recorded as-is.\n * No other behavior is simulated.\n */\n tag?: string | null;\n}\n\n/** Options for `action()`. Only `tag` is meaningful to the fake. */\nexport interface ActionOptions {\n /** Request tag, recorded on the {@link CallRecord} (`null` = explicit suppression). */\n tag?: string | null;\n}\n\nexport interface SystemFields {\n _rev: string;\n _createdAt: string;\n _updatedAt: string;\n}\n\nexport interface CreateTestClientOptions {\n /** Default project id. Defaults to `\"test\"`. */\n projectId?: string;\n /** Default dataset name. Defaults to `\"test\"`. */\n dataset?: string;\n /** Documents to seed into the default `(projectId, dataset)` store. */\n documents?: SanityDocumentLike[];\n /**\n * Optional: seed multiple datasets at once. Keys are `\"projectId:dataset\"`\n * pairs. Useful when workflows span multiple datasets.\n */\n datasets?: Record<string, SanityDocumentLike[]>;\n /**\n * Optional: seed any resource type. Keys are `\"<type>:<id>\"`, the\n * `resource` config's two fields: `\"media-library:ml1\"`, `\"canvas:c1\"`,\n * `\"dashboard:d1\"`, or `\"dataset:<projectId>.<dataset>\"` (the same store\n * as `datasets[\"<projectId>:<dataset>\"]`).\n */\n resources?: Record<string, SanityDocumentLike[]>;\n /**\n * The resource the root handle points at, named exactly as\n * `@sanity/client`'s `resource` config names it. While set, it decides\n * the store (over `projectId` / `dataset`), and each capability the\n * platform lacks on that resource type answers with the platform's\n * refusal. `withConfig({ resource })` mints siblings on other resources.\n */\n resource?: TestClientResource;\n /**\n * The organization every resource of this client belongs to — what\n * `GET /media-libraries/<id>`, `/canvases/<id>`, `/dashboards/<id>` and\n * `/projects/<projectId>` answer as `organizationId`.\n */\n organizationId?: string;\n /**\n * The features each project has, keyed by projectId: what\n * `GET /projects/<projectId>/features` answers, sorted. A project\n * without an entry has none and answers `[]`.\n */\n projectFeatures?: Record<string, string[]>;\n /**\n * Opt-in access-control simulation. When set, the client exposes\n * `request({ url })` that serves `/users/me` (host-aware), the\n * configured ACL path, `/projects/<projectId>` (members),\n * `/projects/<projectId>/users/<ids>`, `/projects/<projectId>/roles`,\n * `/projects/<projectId>/features` and the other platform routes,\n * AND every mutation is gated through the supplied `grants`\n * — writes that the grants don't permit throw a `WriteAccessDeniedError`\n * shaped like Sanity's 403. Lets tests demonstrate the full\n * \"engine soft-gate + lake hard-gate\" contract end-to-end without\n * a real HTTP round-trip.\n *\n * When omitted, `request` is undefined and all writes proceed\n * unconditionally — the legacy behavior.\n */\n accessControl?: TestClientAccessControl;\n /**\n * Token→identity registry, shared by the whole `withConfig` family:\n * multiple identities over one store, keyed by the token a handle is\n * bound to — the token determines identity, exactly like production.\n *\n * ```ts\n * const client = createTestClient({\n * token: \"t-admin\",\n * identities: {\n * \"t-admin\": { user: { id: \"admin\", roles: [{ name: \"administrator\" }] }, grants: WILDCARD },\n * \"t-bob\": { user: { id: \"bob\", roles: [{ name: \"editor\" }] }, grants: READ_ONLY },\n * },\n * });\n * const bob = client.withConfig({ token: \"t-bob\" });\n * // same store; bob's /users/me → bob; bob's ACL path → bob's grants\n * ```\n *\n * A token-bound handle behaves exactly like one configured with\n * `accessControl` derived from its identity (identity serving + grant\n * gating; the ACL path defaults per `(projectId, dataset)`). Register\n * more identities later with `addIdentity`.\n */\n identities?: Record<string, TestClientIdentity>;\n /**\n * The Content Lake's internal-token keys: RSA public key PEMs (SPKI or\n * PKCS#1). A bearer that is not a registered identity, has a JWT's three\n * segments and verifies as RS256 against any of these is an internal\n * token, as a Sanity service mints one: identity `{ user: { id: i },\n * grants: g }`, its `{ s, p }` grants checked like `identities` grants.\n * The Content Lake answers a token that does not verify with 400\n * `httpBadRequest` (`invalid JWT token \"<token>\": <reason>`) and one\n * whose `x` (epoch seconds) has passed with 403 `permissionDeniedError`\n * (`Token has expired (at <time>)`), as gradient does. Shared by the\n * whole `withConfig` family.\n */\n internalTokenKeys?: string[];\n /**\n * Token the root handle is bound to — must name an entry in\n * `identities` (unknown tokens throw), or be a JWT when\n * `internalTokenKeys` is set. Omit to leave the root handle\n * identity-free (a bare factory for token-bound siblings): its\n * `request` is undefined and its reads/writes are ungated. Mutually\n * exclusive with `accessControl`.\n */\n token?: string;\n /**\n * Pluggable write interceptor — runs once for each document's net change in\n * a commit. Document publish/unpublish actions expose their underlying\n * draft/published mutations; release/version actions remain unhooked. Return\n * denial descriptors to reject atomically with `GuardDeniedError`; a\n * thrown callback error propagates unchanged. The client owns no policy.\n * Inherited by `withConfig` siblings. See {@link MutationGuard}.\n */\n mutationGuard?: MutationGuard;\n /**\n * Default perspective applied to reads. Per-fetch `options.perspective`\n * overrides this. `withConfig({ perspective })` returns a sibling\n * handle with a different default. Defaults to `\"raw\"` so existing\n * tests stay green.\n */\n perspective?: Perspective;\n /** Prefix composed with request tags and used alone for untagged requests. */\n requestTagPrefix?: string;\n /**\n * Which API host the root handle models: the project host (default\n * `true`) or the global host (`false`) — same flag as the real\n * `createClient` config. A global-hosted handle's `/users/me` serves\n * the identity's account-global record. `withConfig({ useProjectHostname })`\n * mints siblings on the other host.\n */\n useProjectHostname?: boolean;\n /**\n * The API host, as the real `createClient` config names it (default\n * `https://api.sanity.io`). With {@link apiVersion}, `useCdn` and the\n * host flag it decides the URL a platform error carries as\n * `response.url`: the URL `@sanity/client` would have requested, e.g.\n * `https://p1.api.sanity.io/v2025-02-19/data/doc/production/x?tag=t`.\n */\n apiHost?: string;\n /** The API version (default `\"1\"`, as in `@sanity/client`): `\"YYYY-MM-DD\"`, `\"X\"` or `\"1\"`, `v` prefix optional. */\n apiVersion?: string;\n /**\n * Whether GET reads of the data API (queries, documents, history) go to\n * the API CDN, as in `@sanity/client` (default `true`). Only the URL a\n * platform error names depends on it; the fake serves the same data.\n */\n useCdn?: boolean;\n}\n\nexport type { Perspective } from \"./perspective.ts\";\nexport type { ResourceType, TestClientResource } from \"./resources.ts\";\nexport type {\n Action,\n ActionResult,\n DocumentAction,\n DocumentBody,\n ReleaseAction,\n ReleaseDocument,\n ReleaseMetadata,\n ReleaseState,\n ReleaseType,\n VersionAction,\n} from \"./actions.ts\";\n\nexport type {\n TestClientAccessControl,\n TestClientIdentity,\n DocumentValuePermission,\n} from \"./access-control.ts\";\nexport { WriteAccessDeniedError } from \"./access-control.ts\";\n\nconst DEFAULT_PROJECT_ID = \"test\";\nconst DEFAULT_DATASET = \"test\";\n\n/** One resource's store and the history of every write committed to it. */\ninterface RegistryEntry {\n resource: TestClientResource;\n store: TestStore;\n history: DocumentHistory;\n /**\n * Whether the resource exists for the platform: it was seeded or written\n * to. A handle pointed at it opens its store but does not create it.\n */\n exists: boolean;\n}\n\ntype Registry = Map<string, RegistryEntry>;\n\n/**\n * Shared backing context for one `createTestClient()` call and all the\n * `withConfig` siblings it spawns: the multi-dataset store registry, the\n * append-only mutation log, the stub registry, and the token→identity\n * registry. Sibling handles see each other's writes, log entries, stubs,\n * and registered identities.\n */\ninterface Backend {\n registry: Registry;\n /** See {@link CreateTestClientOptions.organizationId}. */\n organizationId: string | undefined;\n /** See {@link CreateTestClientOptions.projectFeatures}. */\n projectFeatures: Record<string, string[]>;\n log: MutationLogEntry[];\n stubs: Map<string, unknown>;\n calls: CallRecord[];\n /**\n * Pending simulated concurrent edits, keyed per (resource, document id).\n * Each entry is a count of upcoming revision-guarded mutations that\n * should 409 as if another client raced them. See\n * {@link TestClient.simulateConcurrentEdit}.\n */\n concurrentEdits: Map<string, number>;\n /**\n * Token→identity registry. Token-bound handles resolve their acting\n * identity from here on every access, so `addIdentity` upserts take\n * effect on already-minted siblings. See\n * {@link CreateTestClientOptions.identities}.\n */\n identities: Map<string, TestClientIdentity>;\n /** Registered tokens the platform no longer has a session for; see {@link TestClient.revokeIdentity}. */\n revoked: Set<string>;\n /** See {@link CreateTestClientOptions.internalTokenKeys}. */\n internalTokenKeys: KeyObject[];\n /** Each internal token's verification, made once: a token's signature and claims do not change. */\n internalTokens: Map<string, InternalTokenCheck>;\n /** Platform components that are down; see {@link TestClient.simulateOutage}. */\n outages: Set<PlatformComponent>;\n /**\n * The registry-wide commit lock: every write path queues on this chain.\n * The real Content Lake validates and applies a mutation atomically\n * server-side; the fake's write paths await user hooks while staging or\n * committing a write. Without serialization two in-flight guarded writes\n * can both validate against the same `_rev` and the loser silently clobbers\n * the winner.\n */\n writeChain: Promise<void>;\n}\n\n/**\n * Queue one write on the backend's commit lock. Corollary of the lock: a\n * gate / `mutationGuard` hook that issues another write through the same\n * registry deadlocks — hooks are read-only by contract.\n */\nfunction serializeWrite<T>(backend: Backend, run: () => Promise<T>): Promise<T> {\n const result = backend.writeChain.then(run);\n backend.writeChain = result.then(\n () => undefined,\n () => undefined,\n );\n return result;\n}\n\n/**\n * What the Content Lake makes of `token` as an internal token: `undefined`\n * when it is a registered identity, or the family has no internal-token\n * keys, or it is not a JWT.\n */\nfunction internalToken(backend: Backend, token: string): InternalTokenCheck | undefined {\n if (backend.identities.has(token)) return undefined;\n if (backend.internalTokenKeys.length === 0 || !isJwtShaped(token)) return undefined;\n let check = backend.internalTokens.get(token);\n if (check === undefined) {\n check = checkInternalToken(token, backend.internalTokenKeys);\n backend.internalTokens.set(token, check);\n }\n return check;\n}\n\n/**\n * Throw unless `token` names a registered identity or, with\n * `internalTokenKeys`, is a JWT — every token-binding site validates\n * eagerly. A JWT that does not verify still binds: the Content Lake\n * refuses it on each request, as gradient does.\n */\nfunction requireIdentity(backend: Backend, token: string, site: string): void {\n if (backend.identities.has(token)) return;\n if (internalToken(backend, token) !== undefined) return;\n const known = [...backend.identities.keys()];\n throw fakeError(\n `${site}: unknown token \"${token}\" — no identity is registered for it. ` +\n (known.length > 0\n ? `Registered tokens: ${known.map((t) => `\"${t}\"`).join(\", \")}. `\n : \"No identities are registered. \") +\n \"Register identities via createTestClient({ identities }) or client.addIdentity(token, identity)\" +\n (backend.internalTokenKeys.length > 0\n ? \", or pass a JWT for the internal-token keys.\"\n : \", or set createTestClient({ internalTokenKeys }) to verify JWTs.\"),\n );\n}\n\n/** Throw when a single config names both identity mechanisms — a handle acts as exactly one. */\nfunction rejectAmbiguousIdentity(\n site: string,\n config: { token?: string; accessControl?: TestClientAccessControl },\n): void {\n if (config.token !== undefined && config.accessControl !== undefined) {\n throw fakeError(\n `${site}: \\`token\\` and \\`accessControl\\` are mutually exclusive — a handle acts either as ` +\n \"a registered identity (token) or as an inline accessControl config, not both.\",\n );\n }\n}\n\nconst registryKey = (resource: TestClientResource) => `${resource.type}:${resource.id}`;\n\n/** The dataset resource a `(projectId, dataset)` pair names. */\nconst datasetResource = (projectId: string, dataset: string): TestClientResource => ({\n type: \"dataset\",\n id: `${projectId}.${dataset}`,\n});\n\n/** Split a `datasets` seed key, `\"<projectId>:<dataset>\"`. */\nconst parseDatasetKey = (key: string): { projectId: string; dataset: string } => {\n const [projectId, dataset] = key.split(\":\") as [string, string];\n return { projectId, dataset };\n};\n\n/** Split a `resources` seed key, `\"<type>:<id>\"`, and validate it like a `resource` config. */\nconst parseResourceKey = (key: string): TestClientResource => {\n const separator = key.indexOf(\":\");\n const resource = {\n type: key.slice(0, separator),\n id: key.slice(separator + 1),\n } as TestClientResource;\n if (separator <= 0 || resource.id.length === 0) {\n throw fakeError(\n `createTestClient(): \\`resources\\` keys are \"<type>:<id>\" (e.g. \"media-library:ml1\"); got \"${key}\".`,\n );\n }\n validateResource(resource);\n return resource;\n};\n\n/** The registry entry for `resource`, created empty (and not yet existing) on first use. */\nfunction ensureEntry(registry: Registry, resource: TestClientResource): RegistryEntry {\n const key = registryKey(resource);\n let entry = registry.get(key);\n if (entry === undefined) {\n entry = {\n resource,\n store: createTestStore(),\n history: createDocumentHistory(),\n exists: false,\n };\n registry.set(key, entry);\n }\n return entry;\n}\n\n/** Seed `documents` into `resource`'s store; a seeded resource exists. */\nfunction seedEntry(\n registry: Registry,\n resource: TestClientResource,\n documents: SanityDocumentLike[],\n): void {\n const entry = ensureEntry(registry, resource);\n entry.store.seed(documents);\n entry.exists = true;\n}\n\n/**\n * Create a fresh, isolated test client backed by a private multi-dataset\n * registry. `withConfig` returns a sibling handle into the same registry.\n *\n * ```\n * const client = createTestClient({\n * datasets: {\n * \"test:definitions\": [...],\n * \"test:instances\": [...],\n * },\n * });\n * const defs = client.withConfig({ dataset: \"definitions\" });\n * const inst = client.withConfig({ dataset: \"instances\" });\n * ```\n */\nexport function createTestClient(options: CreateTestClientOptions = {}): TestClient {\n const defaultProjectId = options.projectId ?? DEFAULT_PROJECT_ID;\n const defaultDataset = options.dataset ?? DEFAULT_DATASET;\n\n const backend: Backend = {\n registry: new Map(),\n organizationId: options.organizationId,\n projectFeatures: options.projectFeatures ?? {},\n log: [],\n stubs: new Map(),\n calls: [],\n concurrentEdits: new Map(),\n identities: new Map(Object.entries(options.identities ?? {})),\n revoked: new Set(),\n internalTokenKeys: parseInternalTokenKeys(options.internalTokenKeys ?? []),\n internalTokens: new Map(),\n outages: new Set(),\n writeChain: Promise.resolve(),\n };\n const registry = backend.registry;\n\n rejectAmbiguousIdentity(\"createTestClient()\", options);\n if (options.token !== undefined) {\n requireIdentity(backend, options.token, \"createTestClient()\");\n }\n\n if (options.resource !== undefined) validateResource(options.resource);\n\n // Seed defaults. `documents` seeds the root handle's own store.\n if (options.documents) {\n const root = options.resource ?? datasetResource(defaultProjectId, defaultDataset);\n seedEntry(registry, root, options.documents);\n }\n if (options.datasets) {\n for (const [key, docs] of Object.entries(options.datasets)) {\n const { projectId, dataset } = parseDatasetKey(key);\n seedEntry(registry, datasetResource(projectId, dataset), docs);\n }\n }\n if (options.resources) {\n for (const [key, docs] of Object.entries(options.resources)) {\n seedEntry(registry, parseResourceKey(key), docs);\n }\n }\n\n return makeHandle({\n backend,\n projectId: defaultProjectId,\n dataset: defaultDataset,\n resource: options.resource,\n accessControl: options.accessControl,\n perspective: options.perspective ?? \"raw\",\n requestTagPrefix: normalizeTagPrefix(options.requestTagPrefix),\n mutationGuard: options.mutationGuard,\n token: options.token,\n useProjectHostname: options.useProjectHostname ?? true,\n hasProjectId: options.projectId !== undefined,\n apiHost: options.apiHost ?? DEFAULT_API_HOST,\n apiVersion: normalizeApiVersion(options.apiVersion ?? DEFAULT_API_VERSION),\n useCdn: options.useCdn !== false,\n configuredPerspective: options.perspective,\n });\n}\n\n/** Affected ids in order, de-duplicated — the `documentIds` of a result. */\nfunction dedupeIds(results: { id: string }[]): string[] {\n const seen = new Set<string>();\n const out: string[] = [];\n for (const r of results) {\n if (!seen.has(r.id)) {\n seen.add(r.id);\n out.push(r.id);\n }\n }\n return out;\n}\n\n/**\n * Resolve a mutation's return value the way `@sanity/client` does, from\n * already-resolved `returnDocuments` / `returnFirst` booleans (callers\n * apply their own defaults: document-first for create-family,\n * ids-for-all for transaction / mutate).\n */\nfunction formatMutationResult(\n documents: SanityDocument[],\n results: { id: string; operation: MutationOperation }[],\n transactionId: string,\n returnDocuments: boolean,\n returnFirst: boolean,\n): TxnCommitResult {\n if (returnDocuments) {\n return returnFirst ? (documents[0] as SanityDocument) : documents;\n }\n if (returnFirst) {\n return {\n transactionId,\n documentId: results[0]?.id as string,\n results,\n } satisfies SingleMutationResult;\n }\n return {\n transactionId,\n documentIds: dedupeIds(results),\n results,\n } satisfies MultipleMutationResult;\n}\n\n/** Convert one raw `mutate()` operation object into a {@link TxnMutation}. */\nfunction toSingleTxnMutation(op: FakeMutation): TxnMutation {\n if (\"create\" in op) return { kind: \"create\", doc: op.create };\n if (\"createOrReplace\" in op) return { kind: \"createOrReplace\", doc: op.createOrReplace };\n if (\"createIfNotExists\" in op) return { kind: \"createIfNotExists\", doc: op.createIfNotExists };\n if (\"delete\" in op) return { kind: \"delete\", id: op.delete.id };\n\n const p = op.patch;\n const ops: PatchOp[] = [];\n if (p.set !== undefined) ops.push({ kind: \"set\", props: p.set });\n if (p.setIfMissing !== undefined) ops.push({ kind: \"setIfMissing\", props: p.setIfMissing });\n if (p.unset !== undefined) ops.push({ kind: \"unset\", paths: p.unset });\n if (p.inc !== undefined) ops.push({ kind: \"inc\", props: p.inc });\n if (p.dec !== undefined) ops.push({ kind: \"dec\", props: p.dec });\n if (p.insert !== undefined) {\n const { before, after, replace, items } = p.insert;\n const position = before !== undefined ? \"before\" : after !== undefined ? \"after\" : \"replace\";\n const anchor = before ?? after ?? replace;\n if (anchor === undefined) {\n throw fakeError(\n \"mutate(): a `patch.insert` operation must name exactly one anchor — `before`, `after`, \" +\n 'or `replace` — pointing at an array path (e.g. \"tags[-1]\" or `tags[_key==\"x\"]`), ' +\n \"alongside `items`. None was set.\",\n );\n }\n ops.push({ kind: \"insert\", position, anchor, items });\n }\n return {\n kind: \"patch\",\n documentId: p.id,\n ops,\n ...(p.ifRevisionID !== undefined ? { ifRev: p.ifRevisionID } : {}),\n };\n}\n\n/** Normalise `mutate()` input (raw array, Patch, or Transaction) to mutations. */\nfunction toTxnMutations(\n operations: FakeMutation[] | PatchHandle | TransactionHandle,\n): TxnMutation[] {\n if (Array.isArray(operations)) return operations.map(toSingleTxnMutation);\n // Both builders expose toJSON(); a Transaction yields an array, a Patch\n // yields a single `{ id, ops, ifRev? }`.\n const json = operations.toJSON();\n if (Array.isArray(json)) return json;\n return [\n {\n kind: \"patch\",\n documentId: json.id,\n ops: json.ops,\n ...(json.ifRev !== undefined ? { ifRev: json.ifRev } : {}),\n },\n ];\n}\n\ninterface MakeHandleArgs {\n backend: Backend;\n projectId: string;\n dataset: string;\n /** The `resource` config, when set; it decides the store over `(projectId, dataset)`. */\n resource: TestClientResource | undefined;\n accessControl: TestClientAccessControl | undefined;\n perspective: Perspective;\n requestTagPrefix: string | undefined;\n mutationGuard: MutationGuard | undefined;\n token: string | undefined;\n /**\n * Which API host this handle models: the project host (true, default)\n * or the global host (`withConfig({ useProjectHostname: false })`).\n * Decides which user record `/users/me` serves — project-scoped vs\n * account-global — exactly like production host routing.\n */\n useProjectHostname: boolean;\n /**\n * Whether this handle or one it was minted from has a `projectId`:\n * configured, or the default standing in for the one `@sanity/client`\n * requires on the project host. A global-hosted client created without one\n * has none, so `config()` omits it; the handle still addresses the default\n * project's stores.\n */\n hasProjectId: boolean;\n apiHost: string;\n apiVersion: string;\n useCdn: boolean;\n /**\n * The perspective the handle's config names, if any. `perspective`\n * falls back to `\"raw\"`; `@sanity/client` sends a perspective only\n * when one is configured or passed.\n */\n configuredPerspective: Perspective | undefined;\n}\n\n/** `@sanity/client`'s mutation options that become query parameters of `/mutate`. */\ninterface MutationQueryOptions {\n dryRun?: boolean;\n returnDocuments?: boolean;\n visibility?: string;\n autoGenerateArrayKeys?: boolean;\n skipCrossDatasetReferenceValidation?: boolean;\n tag?: string | null;\n}\n\nfunction makeHandle(handleArgs: MakeHandleArgs): TestClient {\n const {\n backend,\n resource,\n accessControl,\n perspective,\n requestTagPrefix,\n mutationGuard,\n token,\n useProjectHostname,\n apiHost,\n apiVersion,\n useCdn,\n configuredPerspective,\n } = handleArgs;\n const { registry } = backend;\n // The \"test\" default stands in only for a project-hosted handle; a resource\n // handle is global-hosted whatever `useProjectHostname` says. A dataset\n // resource names its project; other resources name none, so their reported\n // default is not passed on to a sibling that drops the resource.\n const hasProjectId =\n handleArgs.hasProjectId ||\n (useProjectHostname && resource === undefined) ||\n resource?.type === \"dataset\";\n // A dataset resource names its own project and dataset, as in `@sanity/client`.\n const { projectId, dataset } =\n resource?.type === \"dataset\" ? datasetParts(resource.id) : handleArgs;\n /** The resource this handle reads and writes: its `resource` config, else its dataset. */\n const target = resource ?? datasetResource(projectId, dataset);\n\n /**\n * The handle's acting identity, if any. Token-bound handles resolve\n * from the shared registry on every access — so an `addIdentity`\n * upsert takes effect on already-minted siblings; `accessControl`\n * handles use their fixed config. Registry identities resolve their\n * project-scoped record for THIS handle's project (`usersByProject`\n * fallback `user`), so siblings in different projects see different\n * project-scoped ids for the same token — as in production.\n */\n const activeAccess = (): TestClientAccessControl | undefined => {\n if (token === undefined) return accessControl;\n const verified = internalToken(backend, token);\n const identity =\n verified !== undefined && \"identity\" in verified\n ? verified.identity\n : backend.identities.get(token);\n // An internal token the Content Lake refuses has no identity. It acts\n // as one without grants, so each request reaches `admitToLake` and\n // answers the refusal there, after a route's own 404.\n if (verified !== undefined && \"refused\" in verified) {\n return { currentUser: { id: \"\" }, grants: [] };\n }\n if (identity === undefined) {\n // Bind-time validation makes this unreachable today; guard anyway\n // so a future identity-removal API fails loudly.\n throw fakeError(`no identity is registered for token \"${token}\" (was it removed?).`);\n }\n return {\n // Per-project records apply only where the resource lives in a project.\n currentUser:\n capabilitiesOf(target.type).identity === \"project\"\n ? projectUserOf(identity, projectId)\n : identity.user,\n ...(identity.globalUser !== undefined ? { globalUser: identity.globalUser } : {}),\n grants: identity.grants,\n ...(identity.access !== undefined ? { access: identity.access } : {}),\n };\n };\n\n /** Non-optional view for sites that only exist when an identity is bound. */\n const requireActiveAccess = (): TestClientAccessControl => {\n const access = activeAccess();\n if (access === undefined) {\n throw fakeError(\"this handle has no acting identity (no token or accessControl bound).\");\n }\n return access;\n };\n\n /** Whether the handle addresses its project's API host; `@sanity/client` sends a `resource` client's requests to the global one. */\n const projectHosted = useProjectHostname && resource === undefined;\n\n /**\n * Throw the platform's 401 when it has no session for the handle's token\n * (revoked), or the token is project-scoped to another project than the\n * API host the handle addresses.\n */\n const refuseSession = (request: PlatformRequest): void => {\n // An internal token has no session: the Content Lake verifies it itself.\n if (token === undefined || internal() !== undefined) return;\n if (backend.revoked.has(token)) throw sessionNotFound(request);\n const tokenProjectId = backend.identities.get(token)?.projectId;\n if (projectHosted && tokenProjectId !== undefined && tokenProjectId !== projectId) {\n throw sessionHostMismatch(request);\n }\n };\n\n /** What the Content Lake makes of the handle's token as an internal token; see {@link internalToken}. */\n const internal = (): InternalTokenCheck | undefined =>\n token === undefined ? undefined : internalToken(backend, token);\n\n /** Throw the Content Lake's answer to an internal token it does not admit: 400 unverified, 403 expired. */\n const refuseInternalToken = (request: PlatformRequest): void => {\n const check = internal();\n if (check === undefined) return;\n const refusal = internalTokenRefusal(request, check);\n if (refusal !== undefined) throw refusal;\n };\n\n /**\n * `request()` with the session check first: a refused token reaches no\n * platform route. Then a route of a component that is down answers its 500.\n */\n const withSession =\n (request: NonNullable<TestClient[\"request\"]>): NonNullable<TestClient[\"request\"]> =>\n async (opts) => {\n const sent = rawRequest(opts);\n // The Content Lake's routes (the History API) check an internal token themselves.\n if (internal() !== undefined && servedByProjectApi(sent.path)) {\n throw fakeError(\n `request(): \"${sent.path}\" with an internal token. The project API (populus) verifies ` +\n \"internal tokens with its own key and answers differently from the Content Lake; \" +\n \"the fake does not model that. Use a registered identity's token there.\",\n );\n }\n refuseSession(sent);\n if (backend.outages.has(\"project-api\") && servedByProjectApi(sent.path)) {\n throw projectApiDown(sent);\n }\n return request(opts);\n };\n\n /**\n * Throw the Content Lake's refusal when the acting identity's grants hold\n * one it cannot parse. The Content Lake parses the token on every request\n * it serves (queries, documents, mutations, actions, history), so each of\n * those runs this first; the project API, Access API and ACL do not read\n * a token's grants.\n */\n const admitToLake = (request: PlatformRequest): void => {\n refuseSession(request);\n refuseInternalToken(request);\n const access = activeAccess();\n if (access === undefined) return;\n const refusal = refusedToken(request, access.grants);\n if (refusal !== undefined) throw refusal;\n };\n /** The handle's API base URLs, as `@sanity/client`'s `config()` sets `url` and `cdnUrl`. */\n const baseUrls = apiBaseUrls({\n apiHost,\n apiVersion,\n projectId: projectHosted ? projectId : undefined,\n });\n /** `@sanity/client`'s `getDataUrl(operation, path)` on this handle. */\n const dataPath = (operation: string, path?: string): string =>\n dataUrl({ resource, dataset, operation, path });\n /** Whether `@sanity/client` takes `uri` for the data API `operation` of this handle (`isQuery`, `isDoc`, …). */\n const addresses = (uri: string, operation: string): boolean =>\n (hasProjectId || resource !== undefined) && uri.startsWith(dataPath(operation));\n /** `isData`: a URI the API CDN serves, when `useCdn` is on. */\n const isDataUri = (uri: string): boolean =>\n uri.startsWith(\"/data/\") ||\n [\"query\", \"mutate\", \"doc\", \"listen\", \"history\"].some((operation) => addresses(uri, operation));\n\n /**\n * The request `@sanity/client` sends for `uri` on this handle, its URL\n * built as `_prepareRequest` builds it (@sanity/client 8.16.0\n * `src/data/dataMethods.ts:1020-1120`): the tag first, then on the query\n * endpoint the perspective and `returnQuery`, on the CDN base for a GET of\n * the data API unless the perspective needs fresh drafts.\n */\n const platformRequest = (args: {\n method: PlatformRequest[\"method\"];\n uri: string;\n tag?: string | null | undefined;\n /** The query parameters the calling method sets, in its order. */\n query?: QueryEntries;\n /** As the calling method sets it; otherwise a GET of the data API may use the CDN. */\n canUseCdn?: boolean;\n perspective?: Perspective | undefined;\n returnQuery?: boolean | undefined;\n }): PlatformRequest => {\n const { method, uri, tag } = args;\n const query = [...(args.query ?? [])];\n let cdn = useCdn && (args.canUseCdn ?? (method === \"GET\" && isDataUri(uri)));\n const effectiveTag =\n tag && requestTagPrefix ? `${requestTagPrefix}.${tag}` : tag || requestTagPrefix;\n if (effectiveTag && tag !== null) query.unshift([\"tag\", effectiveTag]);\n if (addresses(uri, \"query\")) {\n const perspectiveOption = args.perspective || configuredPerspective;\n if (perspectiveOption !== undefined) {\n const value = Array.isArray(perspectiveOption)\n ? perspectiveOption.join(\",\")\n : perspectiveOption;\n query.unshift([\"perspective\", value]);\n // Drafts cannot come from the CDN, so `@sanity/client` goes to the API for them.\n if (\n (Array.isArray(perspectiveOption) && perspectiveOption.length > 0) ||\n perspectiveOption === \"previewDrafts\" ||\n perspectiveOption === \"drafts\"\n ) {\n cdn = false;\n }\n }\n if (args.returnQuery === false) query.unshift([\"returnQuery\", \"false\"]);\n }\n const queryStart = uri.indexOf(\"?\");\n return {\n method,\n path: queryStart === -1 ? uri : uri.slice(0, queryStart),\n url: requestUrl(cdn ? baseUrls.cdnUrl : baseUrls.url, uri, query),\n };\n };\n\n /** `getDocument(s)`, `releases.get`: `GET <data>/doc/<ids>`. */\n const docRequest = (ids: string, tag: string | null | undefined): PlatformRequest =>\n platformRequest({ method: \"GET\", uri: dataPath(\"doc\", ids), tag });\n\n /**\n * A GROQ query, sent in the URL while it is short enough and as a `POST`\n * body otherwise (`_dataRequestOptions`, `encodeQueryString`).\n */\n const queryRequest = (args: {\n query: string;\n params: Record<string, unknown> | undefined;\n tag: string | null | undefined;\n perspective: Perspective | undefined;\n returnQuery: boolean | undefined;\n }): PlatformRequest => {\n const encoded = encodeGroqQuery(args.query, args.params);\n const useGet = encoded.length < QUERY_SIZE_LIMIT;\n return platformRequest({\n method: useGet ? \"GET\" : \"POST\",\n uri: dataPath(\"query\", useGet ? encoded : \"\"),\n tag: args.tag,\n canUseCdn: true,\n perspective: args.perspective,\n returnQuery: args.returnQuery,\n });\n };\n\n /** `POST <data>/mutate`, with the query parameters `getMutationQuery` sets. */\n const mutateRequest = (options: MutationQueryOptions = {}): PlatformRequest => {\n const { returnDocuments } = options;\n return platformRequest({\n method: \"POST\",\n uri: dataPath(\"mutate\"),\n tag: options.tag,\n canUseCdn: false,\n query: [\n [\"dryRun\", options.dryRun],\n [\"returnIds\", true],\n [\"returnDocuments\", returnDocuments === false ? undefined : (returnDocuments ?? true)],\n [\"visibility\", options.visibility || \"sync\"],\n [\"autoGenerateArrayKeys\", options.autoGenerateArrayKeys],\n [\"skipCrossDatasetReferenceValidation\", options.skipCrossDatasetReferenceValidation],\n ],\n });\n };\n\n /** `client.request({ url })`: a GET of `url` as given. */\n const rawRequest = (opts: { url?: string; uri?: string; tag?: string | null }): PlatformRequest =>\n platformRequest({ method: \"GET\", uri: opts.uri ?? opts.url ?? \"\", tag: opts.tag });\n /**\n * Every principal the handle's member listings see: the registered\n * identities, plus the handle's own inline `accessControl` actor, which is\n * a member wherever its config gives it roles.\n */\n const members = (): TestClientIdentity[] => [\n ...backend.identities.values(),\n ...(token === undefined && accessControl !== undefined\n ? [\n {\n user: accessControl.currentUser,\n ...(accessControl.globalUser !== undefined\n ? { globalUser: accessControl.globalUser }\n : {}),\n grants: accessControl.grants,\n ...(accessControl.access !== undefined ? { access: accessControl.access } : {}),\n },\n ]\n : []),\n ];\n const capabilities = capabilitiesOf(target.type);\n /** How error messages name the target, e.g. `dataset \"test:test\"`. */\n const targetLabel = capabilities.label(target.id);\n const targetEntry = ensureEntry(registry, target);\n const { store, history } = targetEntry;\n /**\n * The store as `client.store()` hands it out: seeding or writing to it\n * makes the resource exist. Writes through the handle use `store` and\n * make it exist when they commit.\n */\n const publicStore: TestStore = {\n ...store,\n seed: (documents) => {\n store.seed(documents);\n targetEntry.exists = true;\n },\n add: (documents) => {\n store.add(documents);\n targetEntry.exists = true;\n },\n put: (doc) => {\n store.put(doc);\n targetEntry.exists = true;\n },\n };\n\n /** Key into {@link Backend.concurrentEdits} for a doc in this resource. */\n const concurrentEditKey = (id: string): string => registryKey(target) + \" \" + id;\n\n /**\n * The id grant filters see as `identity()` and history records as the\n * author: the project-scoped record on a dataset, the account-global\n * one on every other resource type.\n */\n const actorId = (): string | undefined => {\n const access = activeAccess();\n if (access === undefined) return undefined;\n return capabilities.identity === \"global\"\n ? (access.globalUser ?? access.currentUser).id\n : access.currentUser.id;\n };\n\n /** The store before the write in flight; see {@link write}. */\n let beforeWrite: Map<string, SanityDocument> | undefined;\n /** The request of the write in flight, which a refused write's error names. */\n let writeRequest: PlatformRequest = DEFAULT_WRITE;\n\n /**\n * Queue a write on the commit lock and remember the store's images before\n * it, so {@link commitHistory} can record exactly what the write changed.\n */\n const write = <T>(run: () => Promise<T>, request: PlatformRequest): Promise<T> =>\n serializeWrite(backend, async () => {\n admitToLake(request);\n beforeWrite = new Map(store.all().map((doc) => [doc._id, doc]));\n writeRequest = request;\n try {\n return await run();\n } finally {\n beforeWrite = undefined;\n writeRequest = DEFAULT_WRITE;\n }\n });\n\n /** Record the write in flight in the resource's history, under `transactionId`. */\n const commitHistory = (transactionId: string): void => {\n if (beforeWrite === undefined) return;\n targetEntry.exists = true;\n history.commit({ transactionId, author: actorId(), before: beforeWrite, after: store.all() });\n };\n\n /** Append a committed transaction to the shared log. */\n const record = (transactionId: string, mutations: TxnMutation[], tag?: string | null): void => {\n const effectiveTag = effectiveRequestTag(tag);\n commitHistory(transactionId);\n backend.log.push({\n transactionId,\n projectId,\n dataset,\n ...(resource !== undefined ? { resource } : {}),\n mutations,\n ...(effectiveTag !== undefined ? { tag: effectiveTag } : {}),\n });\n };\n\n const effectiveRequestTag = (tag: string | null | undefined): string | null | undefined => {\n if (!requestTagPrefix) return tag;\n return tag ? `${requestTagPrefix}.${tag}` : requestTagPrefix;\n };\n\n /**\n * Shape a single-document write result. Create-family methods default\n * to returning the document (first), unlike transaction / mutate.\n */\n const writeResult = (\n documents: SanityDocument[],\n results: { id: string; operation: MutationOperation }[],\n transactionId: string,\n opts: MutationOptions | undefined,\n ): TxnCommitResult =>\n formatMutationResult(\n documents,\n results,\n transactionId,\n opts?.returnDocuments ?? true,\n opts?.returnFirst ?? true,\n );\n\n const promote = (doc: SanityDocumentLike): SanityDocument & SystemFields => {\n const now = new Date().toISOString();\n if (typeof doc._id !== \"string\" || doc._id.length === 0) {\n throw fakeError(\n `Cannot store a document without a non-empty string \\`_id\\` (got ${JSON.stringify(doc._id)}). ` +\n `Unlike @sanity/client, this fake never auto-generates ids — give every document an explicit _id.`,\n );\n }\n if (typeof doc._type !== \"string\" || doc._type.length === 0) {\n throw fakeError(\n `Document \"${doc._id}\" is missing a non-empty string \\`_type\\` (got ${JSON.stringify(doc._type)}). ` +\n `Every Sanity document needs a _type.`,\n );\n }\n return {\n ...doc,\n _id: doc._id,\n _type: doc._type,\n _rev: cryptoRandomKey(),\n _createdAt: doc._createdAt ?? now,\n _updatedAt: now,\n } as SanityDocument & SystemFields;\n };\n\n const checkRevision = (id: string, ifRevisionId: string | undefined) => {\n if (ifRevisionId === undefined) return;\n // A pending simulated concurrent edit beats the real revision check —\n // the guarded write loses the race even if its _rev currently matches.\n const editKey = concurrentEditKey(id);\n const pendingEdits = backend.concurrentEdits.get(editKey) ?? 0;\n if (pendingEdits > 0) {\n backend.concurrentEdits.set(editKey, pendingEdits - 1);\n throw fakeError(\n `ifRevisionId check failed: document \"${id}\" was concurrently edited (simulated via ` +\n `simulateConcurrentEdit) — the revision-guarded mutation lost the race; re-read and retry. ` +\n `(This mirrors Sanity's optimistic-locking 409.)`,\n );\n }\n const existing = store.get(id);\n if (existing === undefined) {\n throw fakeError(\n `ifRevisionId check failed: document \"${id}\" does not exist, so its revision cannot match ` +\n `the expected _rev=\"${ifRevisionId}\". Drop ifRevisionId when creating a new document, or ` +\n `seed/create the document before guarding on its revision.`,\n );\n }\n if (existing._rev !== ifRevisionId) {\n throw fakeError(\n `ifRevisionId check failed: document \"${id}\" is at _rev=\"${existing._rev}\" but the mutation ` +\n `required _rev=\"${ifRevisionId}\". The document changed since you read it — re-read it and ` +\n `retry. (This mirrors Sanity's optimistic-locking 409.)`,\n );\n }\n };\n\n /**\n * Gate a written document through the configured `accessControl.grants`.\n * No-op when accessControl is absent (legacy bench behavior). On denial\n * throws `WriteAccessDeniedError` — same failure mode tests get from\n * a real Sanity 403.\n *\n * Pass the document before the write (`undefined` when it is created)\n * and after it (`undefined` when it is deleted). The rule mirrors the\n * Content Lake's permission checker: a create needs `create` on the new\n * document; an update, replace or patch needs `update` on the document\n * before AND after; a delete needs `update` on the document before.\n * System ids (`_.`) need `manage` as well, checked first.\n */\n const gate = async (prev: SanityDocument | undefined, next: SanityDocument | undefined) => {\n const access = activeAccess();\n if (access === undefined) return;\n const id = (prev ?? next)?._id;\n if (id === undefined) return;\n const required: DocumentValuePermission[] = isSystemId(id) ? [\"manage\"] : [];\n required.push(prev === undefined ? \"create\" : \"update\");\n for (const permission of required) {\n for (const document of [prev, next]) {\n if (document === undefined) continue;\n await enforceWriteAccess({\n document,\n grants: access.grants,\n permission,\n userId: actorId() as string,\n request: writeRequest,\n });\n }\n }\n };\n\n const guardGate = async (contexts: readonly MutationGuardImageContext[]): Promise<void> => {\n const identity = actorId();\n await enforceMutationGuards(\n mutationGuard,\n contexts.map((context) => ({ ...context, identity })),\n writeRequest,\n );\n };\n\n const guardOne = async (\n action: MutationGuardContext[\"action\"],\n before: MutationGuardContext[\"before\"],\n after: MutationGuardContext[\"after\"],\n ): Promise<void> => {\n await guardGate([{ action, before, after }]);\n };\n\n /**\n * Read-gate predicate. Sanity surfaces invisible docs as \"doesn't\n * exist\" on the wire (404 / null) rather than throwing 403 — so\n * reads filter rather than refuse.\n *\n * - `fetch` filters the dataset before running GROQ.\n * - `getDocument` / `getDocuments` return null for invisible docs.\n * - `snapshot` is a test-helper that bypasses the read gate\n * intentionally (tests need raw inspection).\n *\n * Returns `true` when accessControl is absent (no gating).\n */\n const canRead = async (\n document: SanityDocumentLike & { _id?: string; _type?: string },\n ): Promise<boolean> => {\n const access = activeAccess();\n if (access === undefined) return true;\n return canAccess({\n document,\n grants: access.grants,\n permission: \"read\",\n userId: actorId() as string,\n });\n };\n\n const applyPatch = async (\n documentId: string,\n ops: PatchOp[],\n ifRev: string | undefined,\n options: { inTransaction?: boolean } = {},\n ): Promise<SanityDocument> => {\n const existing = store.get(documentId);\n if (existing === undefined) {\n throw fakeError(\n `Cannot patch document \"${documentId}\": not found — no document with that _id exists in ` +\n `${targetLabel}. patch() requires the target to already exist; use ` +\n `createOrReplace() / createIfNotExists() to upsert, or seed the document first.`,\n );\n }\n checkRevision(documentId, ifRev);\n const patched = applyPatchOps(existing, ops);\n const now = new Date().toISOString();\n const next: SanityDocument = {\n ...patched,\n _rev: cryptoRandomKey(),\n _updatedAt: now,\n };\n // A transaction checks its net change per document once staged.\n if (options.inTransaction !== true) {\n await gate(existing, next);\n await guardOne(\"update\", existing, next);\n }\n store.put(next);\n return next;\n };\n\n const applyTransaction = async (\n mutations: TxnMutation[],\n txnOptions?: TxnCommitOptions,\n ): Promise<TxnCommitResult> => {\n const transactionId = txnOptions?.transactionId ?? cryptoRandomKey();\n const snapshot = new Map<string, SanityDocument | undefined>();\n const touchedIds: string[] = [];\n const operations: { id: string; operation: MutationOperation }[] = [];\n\n const remember = (id: string) => {\n if (!snapshot.has(id)) snapshot.set(id, store.get(id));\n };\n\n try {\n for (const mut of mutations) {\n switch (mut.kind) {\n case \"create\": {\n remember(mut.doc._id as string);\n const promoted = promote(mut.doc);\n if (store.get(promoted._id) !== undefined) {\n throw fakeError(\n `create() failed: a document with _id \"${promoted._id}\" already exists in ` +\n `${targetLabel}. Use createOrReplace() to overwrite it, or ` +\n `createIfNotExists() to leave the existing one untouched.`,\n );\n }\n store.put(promoted);\n touchedIds.push(promoted._id);\n operations.push({ id: promoted._id, operation: \"create\" });\n break;\n }\n case \"createOrReplace\": {\n remember(mut.doc._id as string);\n const promoted = promote(mut.doc);\n const wasPresent = store.get(promoted._id) !== undefined;\n store.put(promoted);\n touchedIds.push(promoted._id);\n operations.push({ id: promoted._id, operation: wasPresent ? \"update\" : \"create\" });\n break;\n }\n case \"createIfNotExists\": {\n remember(mut.doc._id as string);\n if (store.get(mut.doc._id as string) === undefined) {\n const promoted = promote(mut.doc);\n store.put(promoted);\n touchedIds.push(promoted._id);\n operations.push({ id: promoted._id, operation: \"create\" });\n }\n break;\n }\n case \"delete\": {\n remember(mut.id);\n const removed = store.remove(mut.id);\n if (removed) {\n touchedIds.push(mut.id);\n operations.push({ id: mut.id, operation: \"delete\" });\n }\n break;\n }\n case \"patch\": {\n remember(mut.documentId);\n await applyPatch(mut.documentId, mut.ops, mut.ifRev, { inTransaction: true });\n touchedIds.push(mut.documentId);\n operations.push({ id: mut.documentId, operation: \"update\" });\n break;\n }\n }\n }\n // Like the Content Lake, check each document's net change once the\n // whole transaction is staged, not each mutation on its way there.\n const images = [...snapshot].map(([id, before]) => ({ before, after: store.get(id) }));\n for (const { before, after } of images) {\n if (before !== after) await gate(before, after);\n }\n await guardGate(mutationContexts(images));\n } catch (error) {\n for (const [id, doc] of snapshot) {\n if (doc === undefined) {\n store.remove(id);\n } else {\n store.put(doc);\n }\n }\n throw error;\n }\n\n // A committed transaction is logged whole; a single-mutation form\n // (create / patch / mutate of one op) lands as a one-entry transaction.\n record(transactionId, mutations, txnOptions?.tag);\n\n const documents = touchedIds\n .map((id) => store.get(id))\n .filter((d): d is SanityDocument => d !== undefined);\n // Transaction / mutate default to the id-shaped `MultipleMutationResult`,\n // matching `@sanity/client` (create-family defaults differ).\n return formatMutationResult(\n documents,\n operations,\n transactionId,\n txnOptions?.returnDocuments ?? false,\n txnOptions?.returnFirst ?? false,\n );\n };\n\n /**\n * The action-API write path, on the same commit lock as every mutation.\n * Document publish/unpublish and `version.create` actions run through the\n * access gate; publish/unpublish expose their net draft/published mutations\n * to `mutationGuard`. Other release/version actions apply unhooked.\n */\n const runAction = async (\n actions: Action | Action[],\n tag?: string | null,\n ): Promise<ActionResult> => {\n // Refused before anything applies: a refused token before routing, an\n // unmounted route before the Content Lake reads the token, a refused\n // action type after it does.\n const request = platformRequest({\n method: \"POST\",\n uri: dataPath(\"actions\"),\n tag,\n canUseCdn: false,\n });\n refuseSession(request);\n if (capabilities.actions !== \"served\") throw capabilities.actions(request);\n return write(async () => {\n for (const action of Array.isArray(actions) ? actions : [actions]) {\n const refused = capabilities.refusedActions[action.actionType];\n if (refused !== undefined) throw refused(request);\n }\n const result = await applyActions(store, actions, { gate, guard: guardGate, request });\n commitHistory(result.transactionId);\n return result;\n }, request);\n };\n\n /**\n * Filter the store's docs to only those `canRead` returns true\n * for. When accessControl is unset, returns the full set (no\n * filtering cost). Used by `fetch` so GROQ runs against the\n * actor-visible subset, matching Sanity's per-token query behavior.\n */\n const accessVisibleDocs = async (): Promise<SanityDocument[]> => {\n const all = store.all();\n if (activeAccess() === undefined) return all;\n const out: SanityDocument[] = [];\n for (const doc of all) {\n if (await canRead(doc)) out.push(doc);\n }\n return out;\n };\n\n /**\n * Apply both access-control gating and perspective layering. The order\n * matters: access first (an invisible doc shouldn't even be a candidate\n * for perspective layering), perspective second (which can hide / merge\n * docs that the actor would otherwise see).\n */\n const visibleDocs = async (override?: Perspective): Promise<SanityDocument[]> => {\n const accessible = await accessVisibleDocs();\n return applyPerspective(accessible, override ?? perspective);\n };\n\n const client: TestClient = {\n fetch: async (query, params, opts) => {\n admitToLake(\n queryRequest({\n query,\n params,\n tag: opts?.tag,\n perspective: opts?.perspective,\n returnQuery: opts?.filterResponse === false && opts.returnQuery !== false,\n }),\n );\n const start = Date.now();\n const tree = parse(query, params ? { params } : undefined);\n const value = await evaluate(tree, {\n dataset: await visibleDocs(opts?.perspective),\n ...(params !== undefined ? { params } : {}),\n });\n const result = await value.get();\n if (opts?.filterResponse === false) {\n const ms = Date.now() - start;\n const raw = opts.returnQuery === false ? { ms, result } : { query, ms, result };\n return raw as never;\n }\n return result as never;\n },\n\n getDocument: async (id, opts) => {\n admitToLake(docRequest(id, opts?.tag));\n const effective = opts?.perspective ?? perspective;\n if (effective === \"raw\") {\n const doc = store.get(id);\n if (doc === undefined) return undefined;\n if (!(await canRead(doc))) return undefined;\n return doc as never;\n }\n const view = await visibleDocs(effective);\n const found = resolveDocument(view, id, effective);\n return (found ?? undefined) as never;\n },\n\n getDocuments: async (ids, opts) => {\n admitToLake(docRequest(ids.join(\",\"), opts?.tag));\n const effective = opts?.perspective ?? perspective;\n if (effective === \"raw\") {\n const result: (SanityDocument | null)[] = [];\n for (const id of ids) {\n const doc = store.get(id);\n if (doc === undefined) result.push(null);\n else if (await canRead(doc)) result.push(doc);\n else result.push(null);\n }\n return result as never;\n }\n const view = await visibleDocs(effective);\n return ids.map((id) => resolveDocument(view, id, effective) ?? null) as never;\n },\n\n getDataUrl: dataPath,\n\n create: (doc, opts) =>\n write(async () => {\n checkRevision(doc._id as string, opts?.ifRevisionId);\n if (store.get(doc._id as string) !== undefined) {\n throw fakeError(\n `create() failed: a document with _id \"${doc._id}\" already exists in ` +\n `${targetLabel}. Use createOrReplace() to overwrite it, or ` +\n `createIfNotExists() to leave the existing one untouched.`,\n );\n }\n const promoted = promote(doc);\n await gate(undefined, promoted);\n await guardOne(\"create\", null, promoted);\n const transactionId = opts?.transactionId ?? cryptoRandomKey();\n if (opts?.dryRun !== true) {\n store.put(promoted);\n record(transactionId, [{ kind: \"create\", doc }], opts?.tag);\n }\n return writeResult(\n [promoted],\n [{ id: promoted._id, operation: \"create\" }],\n transactionId,\n opts,\n ) as never;\n }, mutateRequest(opts)),\n\n createOrReplace: (doc, opts) =>\n write(async () => {\n checkRevision(doc._id as string, opts?.ifRevisionId);\n const promoted = promote(doc);\n const existing = store.get(doc._id as string);\n await gate(existing, promoted);\n await guardOne(existing !== undefined ? \"update\" : \"create\", existing ?? null, promoted);\n const transactionId = opts?.transactionId ?? cryptoRandomKey();\n if (opts?.dryRun !== true) {\n store.put(promoted);\n record(transactionId, [{ kind: \"createOrReplace\", doc }], opts?.tag);\n }\n const operation: MutationOperation = existing !== undefined ? \"update\" : \"create\";\n return writeResult(\n [promoted],\n [{ id: promoted._id, operation }],\n transactionId,\n opts,\n ) as never;\n }, mutateRequest(opts)),\n\n createIfNotExists: (doc, opts) =>\n write(async () => {\n checkRevision(doc._id as string, opts?.ifRevisionId);\n const existing = store.get(doc._id as string);\n const transactionId = opts?.transactionId ?? cryptoRandomKey();\n if (existing !== undefined) {\n return writeResult(\n [existing],\n [{ id: existing._id, operation: \"none\" }],\n transactionId,\n opts,\n ) as never;\n }\n const promoted = promote(doc);\n await gate(undefined, promoted);\n await guardOne(\"create\", null, promoted);\n if (opts?.dryRun !== true) {\n store.put(promoted);\n record(transactionId, [{ kind: \"createIfNotExists\", doc }], opts?.tag);\n }\n return writeResult(\n [promoted],\n [{ id: promoted._id, operation: \"create\" }],\n transactionId,\n opts,\n ) as never;\n }, mutateRequest(opts)),\n\n delete: (id, opts) =>\n write(async () => {\n checkRevision(id, opts?.ifRevisionId);\n const existing = store.get(id);\n if (existing !== undefined) await gate(existing, undefined);\n if (existing !== undefined) await guardOne(\"delete\", existing, null);\n const transactionId = opts?.transactionId ?? cryptoRandomKey();\n let removed: SanityDocument | undefined = existing;\n if (opts?.dryRun !== true) {\n removed = store.remove(id);\n if (removed !== undefined) record(transactionId, [{ kind: \"delete\", id }], opts?.tag);\n }\n // Legacy default shape ({ _id } | null) unless the caller asked for\n // a specific return shape via returnDocuments / returnFirst.\n if (opts?.returnDocuments === undefined && opts?.returnFirst === undefined) {\n return (removed !== undefined ? { _id: id } : null) as never;\n }\n const results = removed !== undefined ? [{ id, operation: \"delete\" as const }] : [];\n const docs = removed !== undefined ? [removed] : [];\n return formatMutationResult(\n docs,\n results,\n transactionId,\n opts.returnDocuments ?? true,\n opts.returnFirst ?? true,\n ) as never;\n }, mutateRequest(opts)),\n\n patch: (documentId) =>\n createPatch(documentId, {\n apply: (docId, ops, ifRev, options) =>\n write(async () => {\n const doc = await applyPatch(docId, ops, ifRev);\n record(\n cryptoRandomKey(),\n [\n {\n kind: \"patch\",\n documentId: docId,\n ops,\n ...(ifRev !== undefined ? { ifRev } : {}),\n },\n ],\n options?.tag,\n );\n return doc;\n }, mutateRequest(options)),\n }),\n\n transaction: () =>\n createTransaction({\n apply: (mutations, txnOptions) =>\n write(\n () => applyTransaction(mutations, txnOptions),\n // `Transaction.commit()` asks for ids unless told otherwise.\n mutateRequest({ ...txnOptions, returnDocuments: txnOptions?.returnDocuments ?? false }),\n ),\n }),\n\n mutate: (operations, options) =>\n write(() => applyTransaction(toTxnMutations(operations), options), mutateRequest(options)),\n\n // Synchronous writes still take the lock: an action landing while a\n // guarded patch is parked in its gate would otherwise be clobbered\n // when that patch resumes over its pre-action snapshot.\n action: (action, options) => runAction(action, options?.tag),\n\n releases: {\n create: async ({ releaseId, metadata }) => {\n const result = await runAction({\n actionType: \"sanity.action.release.create\",\n releaseId,\n ...(metadata !== undefined ? { metadata } : {}),\n });\n const doc = store.get(`_.releases.${releaseId}`) as ReleaseDocument | undefined;\n return {\n ...result,\n releaseId,\n metadata: doc?.metadata ?? { releaseType: \"undecided\" },\n };\n },\n edit: ({ releaseId, patch }) =>\n runAction({ actionType: \"sanity.action.release.edit\", releaseId, patch }),\n // `@sanity/client` reads a release by id and its documents by query.\n get: async ({ releaseId }) => {\n admitToLake(docRequest(`_.releases.${releaseId}`, undefined));\n const doc = store.get(`_.releases.${releaseId}`) as ReleaseDocument | undefined;\n return doc !== undefined && (await canRead(doc)) ? doc : undefined;\n },\n fetchDocuments: async ({ releaseId }) => {\n admitToLake(\n queryRequest({\n query: \"*[sanity::partOfRelease($releaseId)]\",\n params: { releaseId },\n tag: undefined,\n perspective: undefined,\n returnQuery: undefined,\n }),\n );\n const prefix = `versions.${releaseId}.`;\n return (await accessVisibleDocs()).filter((d) => d._id.startsWith(prefix));\n },\n schedule: ({ releaseId, publishAt }) =>\n runAction({\n actionType: \"sanity.action.release.schedule\",\n releaseId,\n publishAt,\n }),\n unschedule: ({ releaseId }) =>\n runAction({ actionType: \"sanity.action.release.unschedule\", releaseId }),\n publish: ({ releaseId }) =>\n runAction({ actionType: \"sanity.action.release.publish\", releaseId }),\n archive: ({ releaseId }) =>\n runAction({ actionType: \"sanity.action.release.archive\", releaseId }),\n unarchive: ({ releaseId }) =>\n runAction({ actionType: \"sanity.action.release.unarchive\", releaseId }),\n delete: ({ releaseId }) =>\n runAction({ actionType: \"sanity.action.release.delete\", releaseId }),\n },\n\n createVersion: (params) => {\n const { releaseId, publishedId, document, baseId, ifBaseRevisionId } = params as {\n releaseId: string;\n publishedId: string;\n document?: DocumentBody;\n baseId?: string;\n ifBaseRevisionId?: string;\n };\n // `@sanity/client` copies from `baseId` whenever `document` is falsy.\n if (document) {\n return runAction({\n actionType: \"sanity.action.document.version.create\",\n releaseId,\n publishedId,\n document,\n });\n }\n if (baseId === undefined) {\n throw fakeError(\"createVersion() requires `baseId` when no `document` is provided.\");\n }\n return runAction({\n actionType: \"sanity.action.document.version.create\",\n publishedId,\n baseId,\n versionId: `versions.${releaseId}.${publishedId}`,\n ...(ifBaseRevisionId !== undefined ? { ifBaseRevisionId } : {}),\n });\n },\n\n unpublishVersion: ({ releaseId, publishedId }) =>\n runAction({\n actionType: \"sanity.action.document.version.unpublish\",\n versionId: `versions.${releaseId}.${publishedId}`,\n publishedId,\n }),\n\n ...(token !== undefined || accessControl !== undefined\n ? {\n request: withSession(\n makeRequestImpl({\n getAccess: requireActiveAccess,\n requestFor: rawRequest,\n projectId,\n useProjectHostname: projectHosted,\n organizationId: backend.organizationId,\n listIdentities: members,\n platform: {\n projectId,\n organizationId: backend.organizationId,\n projectFeatures: backend.projectFeatures,\n own: (asked) =>\n registryKey(asked) === registryKey(target)\n ? {\n grants: requireActiveAccess().grants,\n documents: () => store.all(),\n history,\n canRead,\n admitToLake,\n }\n : undefined,\n actorUser: () => {\n const access = requireActiveAccess();\n return access.globalUser ?? access.currentUser;\n },\n actorAccess: () => requireActiveAccess().access,\n actorProjectId: () =>\n token === undefined ? undefined : backend.identities.get(token)?.projectId,\n identities: members,\n // A dataset stands for its project, and every project exists in\n // the fake, so a dataset handle still counts as before.\n resources: () =>\n [...registry.values()]\n .filter((e) => e.exists || e.resource.type === \"dataset\")\n .map((e) => e.resource),\n },\n }),\n ),\n }\n : {}),\n\n config: () => ({\n ...(hasProjectId || resource !== undefined ? { projectId } : {}),\n dataset,\n ...(resource !== undefined ? { resource } : {}),\n perspective,\n ...(requestTagPrefix !== undefined ? { requestTagPrefix } : {}),\n ...(token !== undefined ? { token } : {}),\n useProjectHostname,\n apiHost,\n apiVersion,\n useCdn,\n }),\n\n withConfig: (config) => {\n rejectAmbiguousIdentity(\"withConfig()\", config);\n if (config.token !== undefined) {\n requireIdentity(backend, config.token, \"withConfig()\");\n }\n // Naming either identity mechanism switches the sibling to it\n // wholesale; naming neither inherits this handle's binding.\n const rebinds = config.token !== undefined || config.accessControl !== undefined;\n // `resource: undefined` drops the resource; omitting the key keeps it.\n const nextResource = \"resource\" in config ? config.resource : resource;\n if (nextResource !== undefined) validateResource(nextResource);\n return makeHandle({\n backend,\n projectId: config.projectId ?? projectId,\n dataset: config.dataset ?? dataset,\n resource: nextResource,\n accessControl: rebinds ? config.accessControl : accessControl,\n perspective: config.perspective ?? perspective,\n // `requestTagPrefix: undefined` unsets the prefix, and an inherited\n // one is normalized again, as `@sanity/client` re-runs `initConfig`\n // on the merged config (`SanityClient.ts:1417`, `config.ts:239-244`).\n requestTagPrefix: normalizeTagPrefix(\n \"requestTagPrefix\" in config ? config.requestTagPrefix : requestTagPrefix,\n ),\n mutationGuard,\n token: rebinds ? config.token : token,\n useProjectHostname: config.useProjectHostname ?? useProjectHostname,\n hasProjectId: hasProjectId || config.projectId !== undefined,\n apiHost: config.apiHost ?? apiHost,\n apiVersion:\n config.apiVersion !== undefined ? normalizeApiVersion(config.apiVersion) : apiVersion,\n useCdn: config.useCdn ?? useCdn,\n configuredPerspective: config.perspective ?? configuredPerspective,\n });\n },\n\n seed: (documents) => publicStore.seed(documents),\n add: (documents) => publicStore.add(documents),\n reset: () => store.reset(),\n snapshot: () => store.snapshot(),\n store: () => publicStore,\n simulateConcurrentEdit: (id, count = 1) => {\n const editKey = concurrentEditKey(id);\n backend.concurrentEdits.set(editKey, (backend.concurrentEdits.get(editKey) ?? 0) + count);\n },\n datasets: () =>\n [...registry.values()]\n .filter((entry) => entry.resource.type === \"dataset\")\n .map((entry) => datasetParts(entry.resource.id)),\n resources: () => [...registry.values()].map((entry) => ({ ...entry.resource })),\n stub: (name, handler) => {\n backend.stubs.set(name, handler);\n },\n stubs: () => Object.fromEntries(backend.stubs),\n addIdentity: (identityToken, identity) => {\n backend.identities.set(identityToken, identity);\n },\n identities: () => Object.fromEntries(backend.identities),\n revokeIdentity: (revokedToken) => {\n if (\n !backend.identities.has(revokedToken) &&\n internalToken(backend, revokedToken) !== undefined\n ) {\n throw fakeError(\n \"revokeIdentity(): an internal token has no session to revoke; mint one whose `x` has \" +\n \"passed to get the Content Lake's expired-token answer.\",\n );\n }\n requireIdentity(backend, revokedToken, \"revokeIdentity()\");\n backend.revoked.add(revokedToken);\n },\n simulateOutage: (component) => {\n backend.outages.add(component);\n },\n mutations: () => backend.log.flatMap((entry) => entry.mutations),\n transactions: () => [...backend.log],\n clearLog: () => {\n backend.log.length = 0;\n },\n calls: (method) =>\n method === undefined ? [...backend.calls] : backend.calls.filter((c) => c.method === method),\n callCount: (method) =>\n method === undefined\n ? backend.calls.length\n : backend.calls.filter((c) => c.method === method).length,\n clearCalls: () => {\n backend.calls.length = 0;\n },\n };\n\n // Record an invocation: capture args now, fill the outcome on settle.\n const onCall = (method: string, args: unknown[]): CallSettler => {\n const rec: CallRecord = {\n method,\n args,\n projectId,\n dataset,\n ...(resource !== undefined ? { resource } : {}),\n ...(token !== undefined ? { token } : {}),\n apiVersion,\n };\n const tag =\n OPTIONS_ARG_POSITION[method] === undefined\n ? undefined\n : effectiveRequestTag(tagFromArgs(method, args));\n if (tag !== undefined) rec.tag = tag;\n backend.calls.push(rec);\n return (outcome) => {\n if (\"error\" in outcome) rec.error = outcome.error;\n else rec.result = outcome.result;\n };\n };\n\n // Wrap so unimplemented members fail with actionable errors, registered\n // stubs are served, and surface-method calls are recorded (see\n // ./unsupported.ts).\n return wrapWithGuards(client, {\n getStub: (name) => backend.stubs.get(name),\n onCall,\n recorded: RECORDED_METHODS,\n });\n}\n"],"names":["uri","rest","resource","VERSIONS_PREFIX","RELEASE_DOC_PREFIX","promote"],"mappings":";;;AAWO,MAAM,aAAa;AAGnB,SAAS,UAAU,SAAwB;AAChD,SAAO,IAAI,MAAM,GAAG,UAAU,IAAI,OAAO,EAAE;AAC7C;ACmBO,MAAM,0BAA0B,YAAY;AAAA,EACjD,YAAY,MAA4B;AACtC,UAAM,iBAAiB,IAAI,CAAC,GAC5B,KAAK,OAAO;AAAA,EACd;AACF;AAOO,MAAM,4BAA4B,YAAY;AAAA,EACnD,YAAY,MAA4B;AACtC,UAAM,iBAAiB,IAAI,CAAC,GAC5B,KAAK,OAAO;AAAA,EACd;AACF;AAiBO,SAAS,iBAAiB,MAAmD;AAClF,QAAM,EAAE,QAAQ,KAAK,YAAY,eAAe,SAAS;AAEzD,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,SAAS,EAAE,gBAAgB,GALT,OAAO,QAAS,WAAW,cAAc,kBAKlB,kBAAA;AAAA,IACzC;AAAA,IACA;AAAA,EAAA;AAEJ;AAOO,SAAS,mBACd,SACA,OACuB;AACvB,SAAO,iBAAiB;AAAA,IACtB,GAAG;AAAA,IACH,YAAY;AAAA,IACZ,eAAe;AAAA,IACf,MAAM;AAAA,MACJ,OAAO;AAAA,QACL,aAAa,uBAAuB,MAAM,WAAW;AAAA,QACrD,OAAO,CAAC,EAAE,OAAO,OAAO,OAAO,GAAG;AAAA,QAClC,MAAM;AAAA,MAAA;AAAA,IACR;AAAA,EACF,CACD;AACH;AAmBO,MAAM,gBAAiC;AAAA,EAC5C,QAAQ;AAAA,E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