mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-12-01 21:05:42 +09:00
Staging work for publish diagnostics. This PR pairs with webstudio-is/webstudio-saas#681. No production release is authorized. ## What changes People can open Diagnostics from a domain or static export to see warnings, errors, and publishes from the current Builder session. Each publish has its own collapsible report and copy control. A failed publish keeps a short reason and links to its own log when that publish was accepted. Builder, CLI, and MCP use the same publish reference and log access path. Status and logs stay separate. Builder keeps session reports in memory, and Edge retrieves available output from the current publish runner. Reloading Builder loses session reports. An unavailable log does not change publish success or failure. Log pages still require runner retrieval and transfer through Edge and Builder; there is no Webstudio log archive in this PR. The UI now keeps publish state in its owning component, with separate site and static actions, focused diagnostics views, and one domain status control. Both actions share completion handling while retaining their different request and download behavior. Late best-effort checks cannot erase a final rejection. A new rejected request does not make an older publish look like its log. Site errors stay scoped to the selected domains. The existing site and asset publish paths are unchanged. ## Checklist - [x] Keep confirmed session success ahead of stale Build timeout during popover reconciliation; cover an old Build timestamp. - [x] Retry a temporarily pending or failed publish-log read before marking it unavailable; cover session behavior. - [x] Verify CI on the rebased PR head and confirm review threads remain resolved. - [x] Let a matching session's confirmed result clear stale pending controls for site and static; cover success and failure. - [x] Remove redundant static-export loading icon while keeping the export button's pending state and diagnostics access. - [x] Prefer confirmed runner success over a timeout inferred from a stale matching Build; cover site and static publishes. - [x] Keep MCP publish input valid when a target union is converted to tool JSON Schema; cover both the adapter and real CLI contract. - [x] Finish an accepted publish by its build ID even if another publish becomes the project's latest; cover site and static behavior. - [x] Review docs impact, verify local checks and CI, and reply to the new review threads. - [x] Keep current validation findings ahead of an older publish success notice; cover rejected requests and manual validation. - [x] Scope accepted-publish status errors to the domains in that publish; cover another domain's Diagnostics. - [x] Make success, warning, and error publish notices distinct and readable in dark mode; use the existing PanelBanner variants and leave light mode unchanged.\n- [x] Review documentation impact and run focused tests, typecheck, lint, and CI for these fixes. The publishing guide needs no wording change. - [x] Resolve the four Builder review comments and push the focused fix. - [x] Split Diagnostics into findings, detail, publish history, and dialog shell without new copy or icons. - [x] Keep Warnings and Errors separate from Publish logs; preserve current-session history, log copying, and access checks. - [x] Consolidate site/static flow and quota reads; narrow UI access to flow internals. - [x] Preserve completed findings, final rejection, target/domain isolation, and accepted-build log links across both completion orders. - [x] Review the remaining publish UI; share the duplicated domain status control and leave cohesive baseline domain/export UI in place. - [x] Review authoritative publishing docs for this UI change. - [x] Correct the guide's statement that all validation errors block the request; the approved wording is in the publishing guide. - [x] Audit names introduced by this PR against their runtime values and existing repository terminology; fix misleading local names without changing stored or public contracts. - [x] Review docs impact of naming-only changes and verify tests, types, lint, and CI. Docs need no change; 179 publish tests, Builder typecheck, root lint, formatting, diff checks, and all non-visual CI on the previous commit passed. Visual approval remains open. - [x] Remove single-use publish hooks; keep their state/effects in the owning component and test behavior through that component. - [x] Split site and static publish actions into clear component-owned flows without the target-discriminated publish function. - [x] Review docs impact of the structural change. No user-facing copy or behavior changed, so no further docs change is needed. - [x] Verify migrated tests, coverage, Builder core tests, typecheck, lint, formatting, and package boundaries. - [x] Simplify publish component tests: share the tRPC test transport and use named Diagnostics options without losing behavior coverage. - [x] Replace the global fetch test stub with a scoped tRPC request interceptor for publish component tests. - [x] Simplify publish component tests again: share only reusable browser setup and interactions, remove duplicate scenarios and incidental assertions, and preserve behavior coverage. - [x] Review `trpc-interface` package exports and packed-package consumers. Keep the explicit source subpaths; no manifest change is needed. - [x] Review every test changed in this PR for avoidable module mocking; replace only clear cases without reducing behavior coverage. - [x] Update and validate the global testing skill with the decision learned from that review. - [x] Re-run focused tests and relevant checks for the module-mock cleanup. - [x] Finish non-visual CI on the current commit. - [x] Audit Builder-wide global fetch replacements and no-request spies; keep injected fakes and valid narrow spies. - [x] Replace backend-style global fetch stubs and the Storybook fixture override with scoped request handlers. - [x] Preserve success, HTTP/network failure, abort, cleanup, and no-request coverage with red/green checks. - [x] Run focused and broader Builder tests, typecheck, lint, formatting, and package-boundary checks for the fetch-test cleanup. - [x] Review docs impact of the test-only cleanup; no user-facing documentation changes are needed. - [x] Make user database tests verify ownership filters, inserted values, and workspace linkage. - [x] Make patch-service tests prove the intended patch was applied and remove vacuous cases. - [x] Add focused DomainCheckbox integration coverage while keeping useful helper cases. - [x] Make publish test transport fail on unexpected requests and distinguish protocol errors from network failures. - [x] Remove duplicate permission cases and run selected DOM-free tests in Node. - [x] Review documentation impact and run relevant red/green, test, type, lint, and config checks. - [x] Show the latest successful site publish and static export in their popovers with a See logs link to the Publish tab. - [x] Cover success, new-attempt reset, Free-plan access, and both flows in component tests. - [x] Review publishing docs and verify focused tests, types, lint, formatting, and diff. - [x] Review the three expected visual differences and approve the CI rerun; visual regression passed on a0c2ca73f. ## Verification Local: 177 publish tests passed after simplifying the component suites. Two duplicate scenarios were removed; the dedicated suites still cover network failures, paged logs, plan changes, and stale responses. The latest test cleanup removes 174 net lines. Builder production code is unchanged. The earlier test cleanup removed 135 net lines across seven files. Coverage increased from 84.2% statements / 83.3% branches to 88.63% / 86.98%. Builder core passed 1,698 tests with one skip. Builder typecheck, root lint, package-boundary check, formatting, and diff checks passed. Representative migrated tests were shown failing when project scoping, late-check isolation, session eviction, validation reset, or static download behavior was temporarily broken, then passing after restoration. The current commit passed the main workflow, lint, and submodule checks. Visual approval remains open for three previously identified story differences. The prior visual report had the same three known story differences; baseline approval remains open. The visual approval gate remains open for the previously identified story differences; no new local Storybook capture was run at the user's request. The agent evaluation suite remains 8/9 because of an unrelated Form-authoring failure; baselines were not changed. The Builder-wide test cleanup replaces 11 global fetch stubs and one Storybook override. It keeps two narrow no-send spies for a pure preview function. Focused checks passed (43 Node tests, 16 browser tests, typecheck, lint, formatting, package boundaries). Full local core and app runs each had one failure in an unchanged test; the app test passed alone, and the core failure reproduced in its unchanged file. CI will provide the full-matrix check. Local request-interception work remains; no product behavior changes. A follow-up review checked all 30 changed test files for module mocks. Two unnecessary wrappers were removed: the resource-preview call counter and the cross-origin guard fake. The real HTTP boundary and a cross-site request now carry those assertions. The narrow Email preview exception fake remains because malformed real inputs do not reach that catch reliably; seven older Preview-route mocks remain tied to auth, database, protected fetch, and environment setup. This test-only follow-up passed 41 focused tests, Builder typecheck, targeted lint/formatting, diff checks, and independent review. The global testing skill was updated and validated outside this repository. The previous commit's complete main CI passed; the visual baseline approval gate remains open. Commitd340a5225is pushed to `pub.staging`. It strengthens the user, patch, permission, checkbox, and publish-transport tests; it also shows successful site publishes and static exports in a shared notice with **See logs**. A newer attempt hides an older success, and static export does not claim “Downloaded” without a download filename. The publishing and export guides describe the new notices. New behavior assertions failed before their fixes and passed afterward. The latest local Builder core run passed 1,755 tests (one skipped); 82 focused browser tests, Builder typecheck, root lint, formatting, and diff checks also passed. No fixture inputs or outputs changed. Visual checks were not run locally at the user's request. The current visual gate passed on run 43725523044 after reviewing the two Diagnostics stories and the Activity icon. Earlier notes about pending visual approval describe previous commits. Commit cae33d877e fixes the two current Builder review findings and adds a dark-mode edge to success, warning, and error banners without changing light mode. The new rejection and domain-scope tests failed before their fixes and passed afterward. Local checks passed: 185 publish tests, 287 design-system tests, Builder and design-system typechecks, lint, formatting, package boundaries, and diff checks. Main CI and the approved visual rerun passed. The first visual run failed on the same three previously reviewed light-theme differences; no new dark-theme screenshot was taken at the user's request.\n\nCommit db5984eb95 fixes two later review findings. MCP now permits branch fields at the root of a composed tool schema; the target branches and API still validate values. Completion uses the accepted build's job status when another build becomes the project's latest. The new focused tests failed before the fixes and passed afterward. Local checks passed: 189 publish tests, 146 MCP adapter tests, 74 CLI MCP tests, relevant typechecks, lint, formatting, package boundaries, and generated MCP docs. Main CI and the approved visual regression run passed on b15eebaab3. The initial unapproved visual run showed only the three previously reviewed differences.\n\nLive staging MCP smoke check: the CLI started a staging publish, its job succeeded, and `get-publish-log` returned one complete page (6,077 characters) in the same MCP session. No log text or share token was saved in the PR. The stale-Build regression failed beforea04d6c287cand passed afterward. All 193 publish tests, Builder typecheck, formatting, targeted lint, and diff checks pass. The refreshed popover trusts a matching confirmed session result but still clears a retained failure when the Build reports `PUBLISHED`. Main CI and the approved visual rerun passed ona04d6c287c. The initial visual run showed only the same three previously reviewed differences. All review threads are resolved. ## Before release - Verify a live staging publish once both staging PRs are deployed. - Release and pin the existing Builder contract package before removing the temporary copied contract in the SaaS PR. Local packed-package typecheck, Worker bundle, and tests passed; registry installation is not yet verified. - Production deployment and package publication remain user-run steps. Ref #3550 Closes #6693
2734 lines
84 KiB
TypeScript
2734 lines
84 KiB
TypeScript
import { access } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
import { z } from "zod";
|
|
import {
|
|
createTestServer,
|
|
db as postgrestDb,
|
|
json,
|
|
testContext,
|
|
} from "@webstudio-is/postgrest/testing";
|
|
import { defaultPlanFeatures } from "@webstudio-is/plans";
|
|
import { createDefaultPages } from "@webstudio-is/project-build";
|
|
import { parseWebstudioJsxFragment } from "@webstudio-is/project-build/transfer/server";
|
|
import { applyBuilderNamespacePatches } from "@webstudio-is/project-build/state";
|
|
import { createPublishedProjectBundleFixture } from "@webstudio-is/protocol/fixtures";
|
|
import * as projectBuild from "@webstudio-is/project-build/server";
|
|
import { PrePublishAuditError } from "@webstudio-is/project-build/runtime";
|
|
import * as projectApi from "@webstudio-is/project/index.server";
|
|
import * as domainApi from "@webstudio-is/domain/index.server";
|
|
import {
|
|
buildPatchTransaction,
|
|
publicApiContractVersion,
|
|
publicApiOperationRequiresServerSupport,
|
|
publicApiOperations,
|
|
} from "@webstudio-is/protocol";
|
|
import {
|
|
authorizeProject,
|
|
type AppContext,
|
|
} from "@webstudio-is/trpc-interface/index.server";
|
|
import { db as authDb } from "@webstudio-is/authorization-token/index.server";
|
|
import {
|
|
blockBodyComponent,
|
|
blockComponent,
|
|
blockTemplateComponent,
|
|
} from "@webstudio-is/sdk";
|
|
import { AssetIndexRevisionError } from "@webstudio-is/content-engine";
|
|
import { DocumentSourceDiagnosticsError } from "@webstudio-is/content-engine/compiler";
|
|
import * as assetUploader from "@webstudio-is/asset-uploader/server";
|
|
import * as apiRuntime from "./api-runtime.server";
|
|
import * as issueReportService from "./github-issue-report.server";
|
|
import { apiRouter, __testing__ } from "./api-router.server";
|
|
import * as assetQueryPreview from "./asset-query-preview.server";
|
|
import * as formSimulation from "./simulate-form.server";
|
|
import * as db from "~/shared/db";
|
|
import {
|
|
getApiRouterProcedures,
|
|
getProcedureInputSchemaMetadata,
|
|
getProcedurePublicApiPermit,
|
|
} from "./api-router-introspection.server";
|
|
import {
|
|
assertApiProjectPermit,
|
|
assertApiTokenPermit,
|
|
getTokenPermits,
|
|
} from "./api-permits.server";
|
|
|
|
const servicesDir = new URL(".", import.meta.url);
|
|
const server = createTestServer();
|
|
|
|
import { assertContentOrBuildTransactions } from "./api-content-permissions.server";
|
|
const { assertApiPublishDomains } = __testing__;
|
|
|
|
const createContext = (
|
|
allowAdditionalPermissions: boolean,
|
|
authorization: AppContext["authorization"] = {
|
|
type: "token",
|
|
authToken: "secret-token",
|
|
ownerId: "user-1",
|
|
},
|
|
maxContactEmailsPerProject = 5,
|
|
ownerMaxContactEmailsPerProject = maxContactEmailsPerProject
|
|
) =>
|
|
({
|
|
...testContext,
|
|
authorization,
|
|
planFeatures: {
|
|
allowAdditionalPermissions,
|
|
maxContactEmailsPerProject,
|
|
},
|
|
getOwnerPlanFeatures: async () => ({
|
|
...defaultPlanFeatures,
|
|
maxContactEmailsPerProject: ownerMaxContactEmailsPerProject,
|
|
}),
|
|
}) as unknown as AppContext;
|
|
|
|
const createToken = (
|
|
overrides: Partial<Awaited<ReturnType<typeof authDb.getTokenInfo>>> = {}
|
|
) =>
|
|
({
|
|
token: "token-1",
|
|
projectId: "project-1",
|
|
name: "Token",
|
|
relation: "builders",
|
|
createdAt: "2024-01-01T00:00:00.000Z",
|
|
canClone: true,
|
|
canCopy: true,
|
|
canPublish: false,
|
|
canUseApi: true,
|
|
...overrides,
|
|
}) as Awaited<ReturnType<typeof authDb.getTokenInfo>>;
|
|
|
|
type RuntimeCallerProcedure = (input: unknown) => Promise<unknown>;
|
|
type RuntimeApiCaller = Record<string, Record<string, RuntimeCallerProcedure>>;
|
|
type ApiRouterCaller = ReturnType<typeof apiRouter.createCaller>;
|
|
|
|
const createCaller = (context: AppContext) =>
|
|
apiRouter.createCaller(context) as ApiRouterCaller & RuntimeApiCaller;
|
|
|
|
const createEditableContentBuild = () =>
|
|
({
|
|
id: "build-1",
|
|
projectId: "project-1",
|
|
version: 3,
|
|
pages: createDefaultPages({ rootInstanceId: "root" }),
|
|
breakpoints: [],
|
|
styles: [],
|
|
styleSources: [],
|
|
styleSourceSelections: [],
|
|
props: [],
|
|
dataSources: [],
|
|
resources: [],
|
|
instances: [
|
|
{
|
|
type: "instance",
|
|
id: "root",
|
|
component: "ws:element",
|
|
tag: "body",
|
|
children: [
|
|
{ type: "id", value: "block" },
|
|
{ type: "id", value: "outside" },
|
|
],
|
|
},
|
|
{
|
|
type: "instance",
|
|
id: "block",
|
|
component: blockComponent,
|
|
children: [
|
|
{ type: "id", value: "text-instance" },
|
|
{ type: "id", value: "other-instance" },
|
|
],
|
|
},
|
|
{
|
|
type: "instance",
|
|
id: "text-instance",
|
|
component: "ws:element",
|
|
tag: "p",
|
|
children: [{ type: "text", value: "Old" }],
|
|
},
|
|
{
|
|
type: "instance",
|
|
id: "other-instance",
|
|
component: "ws:element",
|
|
tag: "p",
|
|
children: [{ type: "text", value: "Other" }],
|
|
},
|
|
{
|
|
type: "instance",
|
|
id: "outside",
|
|
component: "ws:element",
|
|
tag: "p",
|
|
children: [{ type: "text", value: "Old" }],
|
|
},
|
|
],
|
|
marketplaceProduct: {},
|
|
}) as unknown as Awaited<
|
|
ReturnType<typeof projectBuild.loadDevBuildByProjectId>
|
|
>;
|
|
|
|
describe("api router build operation adapters", () => {
|
|
test("Form simulation requires API and build access before invoking the server engine", async () => {
|
|
const simulate = vi
|
|
.spyOn(formSimulation, "simulateFormSubmission")
|
|
.mockResolvedValue({
|
|
success: true,
|
|
status: 200,
|
|
results: [
|
|
{
|
|
status: 202,
|
|
resourceId: "resource-1",
|
|
resourceName: "Private resource name",
|
|
body: "Private response body",
|
|
},
|
|
],
|
|
errors: [],
|
|
});
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
const input = {
|
|
projectId: "project-1",
|
|
formId: "form-1",
|
|
path: "/contact",
|
|
siteOrigin: "https://example.com",
|
|
values: [{ name: "email", value: "test@example.com" }],
|
|
};
|
|
await expect(
|
|
createCaller(createContext(false)).forms.simulateSubmission(input)
|
|
).rejects.toMatchObject({ code: "FORBIDDEN" });
|
|
vi.mocked(authDb.getTokenInfo).mockResolvedValue(
|
|
createToken({ relation: "viewers" })
|
|
);
|
|
await expect(
|
|
createCaller(createContext(true)).forms.simulateSubmission(input)
|
|
).rejects.toThrow("build permission");
|
|
vi.mocked(authDb.getTokenInfo).mockResolvedValue(
|
|
createToken({ projectId: "another-project" })
|
|
);
|
|
await expect(
|
|
createCaller(createContext(true)).forms.simulateSubmission(input)
|
|
).rejects.toThrow("not valid for project");
|
|
vi.mocked(authDb.getTokenInfo).mockResolvedValue(createToken());
|
|
await expect(
|
|
createCaller(createContext(true)).forms.simulateSubmission(input)
|
|
).resolves.toEqual({
|
|
success: true,
|
|
status: 200,
|
|
results: [{ status: 202, resourceId: "resource-1" }],
|
|
errors: [],
|
|
});
|
|
expect(simulate).toHaveBeenCalledTimes(1);
|
|
expect(simulate).toHaveBeenCalledWith(
|
|
expect.objectContaining({ projectId: "project-1", formId: "form-1" })
|
|
);
|
|
simulate.mockResolvedValueOnce({
|
|
success: false,
|
|
status: 502,
|
|
results: [],
|
|
errors: [
|
|
{
|
|
status: 429,
|
|
resourceId: "email-resource",
|
|
code: "E_DAILY_LIMIT_EXCEEDED",
|
|
body: { error: { code: "E_DAILY_LIMIT_EXCEEDED" } },
|
|
message:
|
|
"Cloudflare's daily email sending limit was reached. Try again later.",
|
|
},
|
|
],
|
|
});
|
|
await expect(
|
|
createCaller(createContext(true)).forms.simulateSubmission(input)
|
|
).resolves.toEqual({
|
|
success: false,
|
|
status: 502,
|
|
results: [],
|
|
errors: [
|
|
{
|
|
status: 429,
|
|
resourceId: "email-resource",
|
|
code: "E_DAILY_LIMIT_EXCEEDED",
|
|
message:
|
|
"Cloudflare's daily email sending limit was reached. Try again later.",
|
|
},
|
|
],
|
|
});
|
|
});
|
|
test("enforces plan contact-address limits through project settings updates", async () => {
|
|
server.use(postgrestDb.get("Project", () => json({ userId: "owner-1" })));
|
|
const build = {
|
|
id: "build-1",
|
|
projectId: "project-1",
|
|
version: 1,
|
|
pages: createDefaultPages({ rootInstanceId: "root" }),
|
|
projectSettings: { meta: {}, compiler: {} },
|
|
} as unknown as Awaited<
|
|
ReturnType<typeof projectBuild.loadDevBuildByProjectId>
|
|
>;
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(build);
|
|
const patchBuild = vi
|
|
.spyOn(projectApi, "patchBuild")
|
|
.mockResolvedValue({ status: "ok", version: 2 });
|
|
|
|
const freeCaller = createCaller(createContext(true, undefined, 5, 0));
|
|
await expect(
|
|
freeCaller.projectSettings.update({
|
|
projectId: "project-1",
|
|
meta: { contactEmail: "team@example.com" },
|
|
})
|
|
).rejects.toMatchObject({ code: "BAD_REQUEST" });
|
|
expect(patchBuild).not.toHaveBeenCalled();
|
|
|
|
const paidCaller = createCaller(createContext(true, undefined, 0, 2));
|
|
await expect(
|
|
paidCaller.projectSettings.update({
|
|
projectId: "project-1",
|
|
meta: {
|
|
contactEmail:
|
|
'"Team, West" <team@example.com>, team@example.com, third@example.com',
|
|
},
|
|
})
|
|
).rejects.toMatchObject({ code: "BAD_REQUEST" });
|
|
expect(patchBuild).not.toHaveBeenCalled();
|
|
|
|
await expect(
|
|
paidCaller.projectSettings.update({
|
|
projectId: "project-1",
|
|
meta: {
|
|
contactEmail: '"Team, West" <team@example.com>, team@example.com',
|
|
},
|
|
})
|
|
).resolves.toMatchObject({ version: 2, updated: true });
|
|
expect(patchBuild).toHaveBeenCalledTimes(1);
|
|
|
|
build.projectSettings.meta.contactEmail = "legacy@example.com";
|
|
const downgradedCaller = createCaller(createContext(true, undefined, 0, 0));
|
|
await expect(
|
|
downgradedCaller.projectSettings.update({
|
|
projectId: "project-1",
|
|
meta: {
|
|
contactEmail: "legacy@example.com",
|
|
siteName: "Updated site",
|
|
},
|
|
})
|
|
).resolves.toMatchObject({ version: 2, updated: true });
|
|
expect(patchBuild).toHaveBeenCalledTimes(2);
|
|
});
|
|
test("keeps the CLI contract and adds the server contract to issue reports", async () => {
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
const publishIssueReport = vi
|
|
.spyOn(issueReportService, "publishConfiguredIssueReport")
|
|
.mockResolvedValue({
|
|
status: "created",
|
|
issueNumber: 1,
|
|
issueUrl: "https://example.com/issues/1",
|
|
});
|
|
const caller = createCaller({
|
|
...createContext(true),
|
|
apiClient: {
|
|
type: "cli",
|
|
version: "1.0.0",
|
|
contractVersion: "public-api:client",
|
|
},
|
|
});
|
|
|
|
await caller.reports.issue({
|
|
projectId: "project-1",
|
|
trigger: "user-requested",
|
|
category: "tool-failure",
|
|
deduplicationKey: "contract-report",
|
|
title: "fix: Preserve CLI contract metadata",
|
|
agent: {
|
|
client: "Codex",
|
|
model: "test-model",
|
|
reasoningEffort: "medium",
|
|
},
|
|
runtime: {
|
|
cliVersion: "1.0.0",
|
|
nodeVersion: process.versions.node,
|
|
os: process.platform,
|
|
osVersion: "1",
|
|
architecture: process.arch,
|
|
executionMode: "mcp",
|
|
apiContractVersion: "public-api:client",
|
|
},
|
|
report: {
|
|
userStory: "Report an issue.",
|
|
summary: "The contracts should be distinguishable.",
|
|
attemptedWorkflow: ["Submit the report."],
|
|
expectedBehavior: "Both contract versions are included.",
|
|
actualResult: "The server contract replaced the CLI contract.",
|
|
recoveryAttempts: ["Inspect the issue report."],
|
|
userImpact: "The report is less useful.",
|
|
technicalContext:
|
|
"The client and server may be on different contracts.",
|
|
acceptanceCriteria: ["Both versions are reported separately."],
|
|
},
|
|
});
|
|
|
|
expect(publishIssueReport).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
runtime: expect.objectContaining({
|
|
apiContractVersion: "public-api:client",
|
|
serverApiContractVersion: publicApiContractVersion,
|
|
}),
|
|
})
|
|
);
|
|
});
|
|
|
|
test("submits only configured marketplace products for review", async () => {
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
const loadBuild = vi
|
|
.spyOn(projectBuild, "loadDevBuildByProjectId")
|
|
.mockResolvedValueOnce({ marketplaceProduct: undefined } as never)
|
|
.mockResolvedValueOnce({
|
|
marketplaceProduct: {
|
|
category: "pageTemplates",
|
|
name: "Acme Template",
|
|
thumbnailAssetId: "asset-id",
|
|
author: "Acme Studio",
|
|
email: "hello@example.com",
|
|
website: "https://example.com",
|
|
issues: "",
|
|
description: "Reusable template project for Acme landing pages.",
|
|
},
|
|
} as never);
|
|
const setApprovalStatus = vi
|
|
.spyOn(projectApi, "setMarketplaceApprovalStatus")
|
|
.mockResolvedValue({ marketplaceApprovalStatus: "PENDING" } as never);
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(
|
|
caller.projects.submitMarketplaceProduct({
|
|
projectId: "project-1",
|
|
acknowledgePublicSubmission: true,
|
|
})
|
|
).rejects.toThrow("Complete the marketplace product metadata");
|
|
expect(setApprovalStatus).not.toHaveBeenCalled();
|
|
|
|
await expect(
|
|
caller.projects.submitMarketplaceProduct({
|
|
projectId: "project-1",
|
|
acknowledgePublicSubmission: true,
|
|
})
|
|
).resolves.toEqual({ marketplaceApprovalStatus: "PENDING" });
|
|
expect(loadBuild).toHaveBeenCalledTimes(2);
|
|
expect(setApprovalStatus).toHaveBeenCalledWith(
|
|
{
|
|
projectId: "project-1",
|
|
marketplaceApprovalStatus: "PENDING",
|
|
},
|
|
expect.anything()
|
|
);
|
|
});
|
|
|
|
test("exposes query validation and persisted asset query reads to API clients", async () => {
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue({
|
|
props: [],
|
|
dataSources: [],
|
|
resources: [],
|
|
} as never);
|
|
vi.spyOn(assetQueryPreview, "previewProjectAssetQuery").mockResolvedValue({
|
|
data: {
|
|
items: [{ id: "post" }],
|
|
totalCount: 1,
|
|
hasMore: false,
|
|
},
|
|
__diagnostics__: {
|
|
scope: "query-preview",
|
|
query: {
|
|
usedBytes: 100,
|
|
maxBytes: 512_000,
|
|
unboundedBytes: 100,
|
|
includedDocumentCount: 1,
|
|
omittedDocumentCount: 0,
|
|
truncated: false,
|
|
},
|
|
database: {
|
|
usedBytes: 200,
|
|
maxBytes: 512_000,
|
|
unboundedBytes: 200,
|
|
includedDocumentCount: 2,
|
|
omittedDocumentCount: 0,
|
|
truncated: false,
|
|
},
|
|
},
|
|
} as never);
|
|
vi.spyOn(assetUploader, "loadBuilderAssetFieldCatalog").mockResolvedValue({
|
|
format: "webstudio-builder-asset-field-catalog",
|
|
version: 1,
|
|
canonicalRevision: `sha256:${"a".repeat(64)}`,
|
|
documentCount: 1,
|
|
fields: {
|
|
"properties.slug": {
|
|
types: ["string"],
|
|
occurrences: 1,
|
|
queryPath: ["properties", "slug"],
|
|
},
|
|
},
|
|
});
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(
|
|
caller.assetQueries.validate({
|
|
projectId: "project-1",
|
|
query: {
|
|
where: {
|
|
all: [
|
|
{
|
|
field: ["properties", "slug"],
|
|
operator: "eq",
|
|
value: "post",
|
|
},
|
|
],
|
|
},
|
|
limit: 1,
|
|
},
|
|
})
|
|
).resolves.toMatchObject({
|
|
valid: true,
|
|
referencedFieldPaths: expect.arrayContaining([["properties", "slug"]]),
|
|
filterCount: 1,
|
|
});
|
|
await expect(
|
|
caller.assetQueries.validate({
|
|
projectId: "project-1",
|
|
query: {
|
|
where: {
|
|
all: [
|
|
{
|
|
field: ["properties", "missing"],
|
|
operator: "exists",
|
|
value: true,
|
|
},
|
|
],
|
|
},
|
|
},
|
|
})
|
|
).resolves.toMatchObject({
|
|
valid: true,
|
|
warnings: ["Asset field properties.missing is not currently observed"],
|
|
issues: [
|
|
{
|
|
severity: "warning",
|
|
code: "UNOBSERVED_FIELD",
|
|
path: ["query", "where", "all", "0", "field"],
|
|
message: "Asset field properties.missing is not currently observed",
|
|
},
|
|
],
|
|
});
|
|
await expect(
|
|
caller.assetQueries.preview({
|
|
projectId: "project-1",
|
|
query: { limit: 10 },
|
|
})
|
|
).resolves.toMatchObject({
|
|
data: {
|
|
items: [{ id: "post" }],
|
|
totalCount: 1,
|
|
},
|
|
__diagnostics__: {
|
|
scope: "query-preview",
|
|
query: { truncated: false },
|
|
database: { truncated: false },
|
|
},
|
|
});
|
|
expect(assetQueryPreview.previewProjectAssetQuery).toHaveBeenCalledWith({
|
|
projectId: "project-1",
|
|
request: expect.objectContaining({
|
|
query: expect.objectContaining({ limit: 10 }),
|
|
}),
|
|
context: expect.anything(),
|
|
});
|
|
vi.mocked(assetQueryPreview.previewProjectAssetQuery).mockRejectedValueOnce(
|
|
new AssetIndexRevisionError()
|
|
);
|
|
await expect(
|
|
caller.assetQueries.preview({
|
|
projectId: "project-1",
|
|
query: { limit: 10 },
|
|
indexRevision: `sha256:${"0".repeat(64)}`,
|
|
})
|
|
).rejects.toMatchObject({
|
|
code: "CONFLICT",
|
|
cause: { webstudioCode: "STALE_INDEX" },
|
|
});
|
|
vi.mocked(assetQueryPreview.previewProjectAssetQuery).mockRejectedValueOnce(
|
|
new DocumentSourceDiagnosticsError([
|
|
{
|
|
severity: "warning",
|
|
code: "FRONTMATTER_INVALID",
|
|
message: "Markdown frontmatter contains invalid YAML",
|
|
assetId: "markdown",
|
|
path: "content/broken.md",
|
|
line: 4,
|
|
column: 1,
|
|
},
|
|
{
|
|
severity: "error",
|
|
code: "invalid-mdx",
|
|
message: "Unexpected closing tag",
|
|
assetId: "mdx",
|
|
path: "content/broken.mdx",
|
|
line: 8,
|
|
column: 3,
|
|
reference: "Card",
|
|
nodeType: "mdxJsxFlowElement",
|
|
reason: "unexpected-close-tag",
|
|
sourceRange: {
|
|
start: { line: 8, column: 3, offset: 80 },
|
|
end: { line: 8, column: 10, offset: 87 },
|
|
},
|
|
},
|
|
])
|
|
);
|
|
await expect(
|
|
caller.assetQueries.preview({
|
|
projectId: "project-1",
|
|
query: { limit: 10 },
|
|
})
|
|
).rejects.toMatchObject({
|
|
code: "BAD_REQUEST",
|
|
cause: {
|
|
webstudioCode: "INVALID_REQUEST",
|
|
issues: [
|
|
{
|
|
code: "FRONTMATTER_INVALID",
|
|
path: ["content/broken.md"],
|
|
message: "Markdown frontmatter contains invalid YAML",
|
|
constraint: "FRONTMATTER_INVALID",
|
|
detail:
|
|
"Line 4, column 1. Scope: query. Severity: warning. Asset: markdown",
|
|
},
|
|
{
|
|
code: "invalid-mdx",
|
|
path: ["content/broken.mdx"],
|
|
message: "Unexpected closing tag",
|
|
constraint: "invalid-mdx",
|
|
detail:
|
|
"Line 8, column 3. Scope: query. Severity: error. Asset: mdx",
|
|
severity: "error",
|
|
scope: "query",
|
|
phase: "source",
|
|
assetId: "mdx",
|
|
file: "content/broken.mdx",
|
|
line: 8,
|
|
column: 3,
|
|
reference: "Card",
|
|
nodeType: "mdxJsxFlowElement",
|
|
reason: "unexpected-close-tag",
|
|
sourceRange: {
|
|
start: { line: 8, column: 3, offset: 80 },
|
|
end: { line: 8, column: 10, offset: 87 },
|
|
},
|
|
},
|
|
],
|
|
},
|
|
});
|
|
await expect(
|
|
caller.assetQueries.validate({
|
|
projectId: "project-1",
|
|
query: {
|
|
where: {
|
|
all: [
|
|
{ field: ["width"], operator: "contains", value: 1 },
|
|
{ field: ["height"], operator: "startsWith", value: "1" },
|
|
{
|
|
field: ["properties", "missing"],
|
|
operator: "eq",
|
|
value: true,
|
|
},
|
|
],
|
|
},
|
|
},
|
|
})
|
|
).rejects.toMatchObject({
|
|
code: "BAD_REQUEST",
|
|
cause: {
|
|
webstudioCode: "INVALID_REQUEST",
|
|
issues: [
|
|
expect.objectContaining({
|
|
code: "INCOMPATIBLE_OPERATOR",
|
|
path: ["query", "where", "all", "0", "operator"],
|
|
message: "Operator contains is incompatible with width",
|
|
}),
|
|
expect.objectContaining({
|
|
code: "INCOMPATIBLE_OPERATOR",
|
|
path: ["query", "where", "all", "1", "operator"],
|
|
message: "Operator startsWith is incompatible with height",
|
|
}),
|
|
expect.objectContaining({
|
|
code: "UNOBSERVED_FIELD",
|
|
path: ["query", "where", "all", "2", "field"],
|
|
message: "Asset field properties.missing is not currently observed",
|
|
}),
|
|
],
|
|
},
|
|
});
|
|
await expect(
|
|
caller.assetQueries.fieldCatalog({ projectId: "project-1" })
|
|
).resolves.toMatchObject({
|
|
fields: { "properties.slug": { types: ["string"] } },
|
|
});
|
|
});
|
|
|
|
test("allows CLI clients to refresh pages without project settings", async () => {
|
|
const build = {
|
|
id: "build-1",
|
|
projectId: "project-1",
|
|
version: 1,
|
|
pages: createDefaultPages({ rootInstanceId: "root" }),
|
|
projectSettings: { agents: "Project instructions" },
|
|
} as unknown as Awaited<
|
|
ReturnType<typeof projectBuild.loadDevBuildByProjectId>
|
|
>;
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(build);
|
|
|
|
const caller = createCaller({
|
|
...createContext(true),
|
|
apiClient: {
|
|
type: "cli",
|
|
version: "0.276.0",
|
|
contractVersion: undefined,
|
|
},
|
|
});
|
|
|
|
await expect(
|
|
caller.build.get({ projectId: "project-1", include: ["pages"] })
|
|
).resolves.toMatchObject({
|
|
projectId: "project-1",
|
|
buildId: "build-1",
|
|
version: 1,
|
|
pages: expect.any(Array),
|
|
});
|
|
});
|
|
|
|
test("loads assets and folders together for build snapshots", async () => {
|
|
const build = {
|
|
id: "build-1",
|
|
projectId: "project-1",
|
|
version: 1,
|
|
pages: createDefaultPages({ rootInstanceId: "root" }),
|
|
} as unknown as Awaited<
|
|
ReturnType<typeof projectBuild.loadDevBuildByProjectId>
|
|
>;
|
|
const asset = { id: "asset-1", folderId: "folder-1" };
|
|
const folder = { id: "folder-1", name: "Images" };
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(build);
|
|
const loadAssetData = vi
|
|
.spyOn(assetUploader, "loadAssetDataByProject")
|
|
.mockResolvedValue({ assets: [asset], assetFolders: [folder] } as never);
|
|
const loadFolders = vi.spyOn(assetUploader, "loadAssetFoldersByProject");
|
|
|
|
const caller = createCaller({
|
|
...createContext(true),
|
|
apiClient: {
|
|
type: "cli",
|
|
version: "0.276.0",
|
|
contractVersion: publicApiContractVersion,
|
|
},
|
|
});
|
|
|
|
await expect(
|
|
caller.build.get({
|
|
projectId: "project-1",
|
|
include: ["assets", "assetFolders"],
|
|
})
|
|
).resolves.toMatchObject({
|
|
assets: [asset],
|
|
assetFolders: [folder],
|
|
});
|
|
expect(loadAssetData).toHaveBeenCalledOnce();
|
|
expect(loadFolders).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("public operation catalog paths exist on the api router", () => {
|
|
const procedures = getApiRouterProcedures(apiRouter);
|
|
|
|
for (const operation of publicApiOperations) {
|
|
if (operation.path === undefined) {
|
|
continue;
|
|
}
|
|
const procedure = procedures[operation.path.replace(/^api\./, "")] as
|
|
| { _def?: Partial<Record<typeof operation.method, boolean>> }
|
|
| undefined;
|
|
expect(procedure).toBeDefined();
|
|
expect(procedure?._def?.[operation.method]).toBe(true);
|
|
}
|
|
});
|
|
|
|
test("api router procedures are all described by the public operation catalog", () => {
|
|
const procedures = getApiRouterProcedures(apiRouter);
|
|
expect(
|
|
Object.keys(procedures)
|
|
.map((path) => `api.${path}`)
|
|
.sort()
|
|
).toEqual(
|
|
publicApiOperations
|
|
.flatMap((operation) =>
|
|
operation.path === undefined ? [] : [operation.path]
|
|
)
|
|
.sort()
|
|
);
|
|
});
|
|
|
|
test("operation catalog input fields match api router inputs", () => {
|
|
const procedures = getApiRouterProcedures(apiRouter);
|
|
|
|
for (const operation of publicApiOperations) {
|
|
if (operation.path === undefined) {
|
|
continue;
|
|
}
|
|
const procedure = procedures[operation.path.replace(/^api\./, "")];
|
|
const metadata = getProcedureInputSchemaMetadata(procedure);
|
|
expect(metadata.inputFields).toEqual(operation.inputFields);
|
|
expect(metadata.requiredInputFields).toEqual(
|
|
operation.requiredInputFields
|
|
);
|
|
expect(metadata.inputFieldTypes).toEqual(operation.inputFieldTypes);
|
|
expect(metadata.inputJsonSchema).toEqual(operation.inputSchema);
|
|
}
|
|
});
|
|
|
|
test("operation catalog permits match api router metadata", () => {
|
|
const procedures = getApiRouterProcedures(apiRouter);
|
|
|
|
for (const operation of publicApiOperations) {
|
|
if (operation.path === undefined) {
|
|
continue;
|
|
}
|
|
const procedure = procedures[operation.path.replace(/^api\./, "")];
|
|
expect(getProcedurePublicApiPermit(procedure)).toBe(operation.permit);
|
|
}
|
|
});
|
|
|
|
test("does not keep an api-only build operations service", async () => {
|
|
await expect(
|
|
access(join(servicesDir.pathname, "build-operations.server.ts"))
|
|
).rejects.toThrow();
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe("api router permits", () => {
|
|
test.each([
|
|
{ ownerAllowed: false, blocked: true },
|
|
{ ownerAllowed: true, blocked: false },
|
|
])(
|
|
"uses owner diagnostics entitlement for publish findings ($ownerAllowed, $blocked)",
|
|
async ({ ownerAllowed, blocked }) => {
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
server.use(postgrestDb.get("Project", () => json({ userId: "owner-1" })));
|
|
const context = createContext(true, {
|
|
type: "user",
|
|
userId: "collaborator",
|
|
sessionCreatedAt: 0,
|
|
isLoggedInToBuilder: async () => true,
|
|
});
|
|
context.planFeatures.allowPublishLogs = !ownerAllowed;
|
|
const getOwnerPlanFeatures = vi.fn(async () => ({
|
|
...defaultPlanFeatures,
|
|
allowPublishLogs: ownerAllowed,
|
|
}));
|
|
context.getOwnerPlanFeatures = getOwnerPlanFeatures;
|
|
const finding = {
|
|
ruleId: "html-content-model",
|
|
severity: "warning" as const,
|
|
message: "x".repeat(200),
|
|
location: { pageId: "page-1", instanceId: "instance-1" },
|
|
};
|
|
if (blocked) {
|
|
vi.spyOn(domainApi, "publishStaticProject").mockRejectedValue(
|
|
new PrePublishAuditError([finding])
|
|
);
|
|
} else {
|
|
vi.spyOn(domainApi, "publishStaticProject").mockResolvedValue({
|
|
success: true,
|
|
name: "export.zip",
|
|
build: { id: "build-1" },
|
|
logHandle: undefined,
|
|
findings: [finding],
|
|
} as never);
|
|
}
|
|
|
|
const result = await createCaller(context).publish.create({
|
|
projectId: "project-1",
|
|
target: "static",
|
|
templates: ["ssg"],
|
|
});
|
|
expect(result.status).toBe(blocked ? "blocked" : "pending");
|
|
expect(result.findings).toEqual([
|
|
ownerAllowed
|
|
? finding
|
|
: { ...finding, message: "x".repeat(160), location: {} },
|
|
]);
|
|
expect(getOwnerPlanFeatures).toHaveBeenCalledExactlyOnceWith("owner-1");
|
|
}
|
|
);
|
|
|
|
test("keeps hosted audit failures blocked after checking domains", async () => {
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
server.use(postgrestDb.get("Project", () => json({ userId: "owner-1" })));
|
|
const context = createContext(true, {
|
|
type: "user",
|
|
userId: "owner-1",
|
|
sessionCreatedAt: 0,
|
|
isLoggedInToBuilder: async () => true,
|
|
});
|
|
vi.spyOn(projectApi, "loadById").mockResolvedValue({
|
|
domain: "project.wstd.dev",
|
|
domainsVirtual: [],
|
|
} as never);
|
|
vi.spyOn(domainApi, "publishProject").mockRejectedValue(
|
|
new PrePublishAuditError([
|
|
{
|
|
ruleId: "json-prop-value",
|
|
severity: "error",
|
|
message: "Invalid prop",
|
|
location: { propId: "prop-1" },
|
|
},
|
|
])
|
|
);
|
|
|
|
await expect(
|
|
createCaller(context).publish.create({
|
|
projectId: "project-1",
|
|
target: "staging",
|
|
domains: ["project.wstd.dev"],
|
|
})
|
|
).resolves.toMatchObject({
|
|
status: "blocked",
|
|
findings: [{ ruleId: "json-prop-value", severity: "error" }],
|
|
});
|
|
});
|
|
|
|
test("missing publish jobs carry a not-found error marker", async () => {
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
vi.spyOn(domainApi, "getProjectPublishJob").mockResolvedValue(undefined);
|
|
|
|
await expect(
|
|
createCaller(createContext(true)).publish.getJob({
|
|
projectId: "project-1",
|
|
jobId: "missing-job",
|
|
})
|
|
).rejects.toMatchObject({
|
|
code: "NOT_FOUND",
|
|
message: "Publish job not found",
|
|
cause: { webstudioCode: "NOT_FOUND" },
|
|
});
|
|
});
|
|
|
|
test("checks project access before reading a signed publish job", async () => {
|
|
const permit = vi
|
|
.spyOn(authorizeProject, "hasProjectPermit")
|
|
.mockResolvedValue(true);
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
const getJob = vi
|
|
.spyOn(domainApi, "getProjectPublishJob")
|
|
.mockResolvedValue({ status: "pending" } as never);
|
|
|
|
await createCaller(createContext(true)).publish.getJob({
|
|
projectId: "project-1",
|
|
jobId: "build-prod",
|
|
logHandle: "signed-handle",
|
|
});
|
|
expect(getJob).toHaveBeenCalledWith(
|
|
{
|
|
projectId: "project-1",
|
|
jobId: "build-prod",
|
|
logHandle: "signed-handle",
|
|
},
|
|
expect.anything()
|
|
);
|
|
|
|
permit.mockResolvedValue(false);
|
|
|
|
await expect(
|
|
createCaller(createContext(true)).publish.getJob({
|
|
projectId: "project-1",
|
|
jobId: "build-prod",
|
|
logHandle: "signed-handle",
|
|
})
|
|
).rejects.toThrow();
|
|
expect(getJob).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
test("unpublish completes without returning a nonexistent publish job", async () => {
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "editors", canPublish: true })
|
|
);
|
|
vi.spyOn(projectApi, "loadById").mockResolvedValue({
|
|
domain: "project.wstd.dev",
|
|
domainsVirtual: [],
|
|
} as never);
|
|
const unpublish = vi
|
|
.spyOn(domainApi, "unpublishProjectDomains")
|
|
.mockResolvedValue(undefined);
|
|
|
|
await expect(
|
|
createCaller(createContext(true)).publish.unpublish({
|
|
projectId: "project-1",
|
|
target: "production",
|
|
domains: ["custom.example.com"],
|
|
confirm: true,
|
|
})
|
|
).resolves.toEqual({ unpublishedDomains: ["custom.example.com"] });
|
|
expect(unpublish).toHaveBeenCalledWith(
|
|
{ projectId: "project-1", domains: ["custom.example.com"] },
|
|
expect.anything()
|
|
);
|
|
});
|
|
|
|
test("uses content-mode page copying for Editors without changing Builder copies", async () => {
|
|
const getTokenInfo = vi
|
|
.spyOn(authDb, "getTokenInfo")
|
|
.mockResolvedValue(createToken({ relation: "editors" }));
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(
|
|
createEditableContentBuild()
|
|
);
|
|
vi.spyOn(assetUploader, "loadAssetDataByProject").mockResolvedValue({
|
|
assets: [],
|
|
assetFolders: [],
|
|
});
|
|
const execute = vi
|
|
.spyOn(apiRuntime, "executeApiRuntimeMutation")
|
|
.mockResolvedValue({
|
|
noop: true,
|
|
payload: [],
|
|
result: { pageId: "copied-page" },
|
|
} as never);
|
|
const caller = createCaller(createContext(true));
|
|
const input = {
|
|
projectId: "project-1",
|
|
templateId: "template-1",
|
|
name: "Copied page",
|
|
path: "/copied-page",
|
|
};
|
|
|
|
await caller.pageTemplates.createPage(input);
|
|
await caller.pageTemplates.createPage({ ...input, contentMode: false });
|
|
getTokenInfo.mockResolvedValue(createToken({ relation: "builders" }));
|
|
await caller.pageTemplates.createPage(input);
|
|
|
|
expect(execute.mock.calls.map(([{ input }]) => input)).toEqual([
|
|
{ ...input, contentMode: true },
|
|
{ ...input, contentMode: true },
|
|
input,
|
|
]);
|
|
});
|
|
|
|
test("lets Editors save asset folders without granting Viewer access", async () => {
|
|
const getTokenInfo = vi
|
|
.spyOn(authDb, "getTokenInfo")
|
|
.mockResolvedValue(createToken({ relation: "editors" }));
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(
|
|
createEditableContentBuild()
|
|
);
|
|
const loadAssetData = vi
|
|
.spyOn(assetUploader, "loadAssetDataByProject")
|
|
.mockResolvedValue({
|
|
assets: [],
|
|
assetFolders: [],
|
|
});
|
|
const folder = {
|
|
id: "folder-1",
|
|
projectId: "project-1",
|
|
name: "Editor assets",
|
|
parentId: undefined,
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
};
|
|
vi.spyOn(apiRuntime, "executeApiRuntimeMutation").mockResolvedValue({
|
|
payload: [
|
|
{
|
|
namespace: "assetFolders",
|
|
patches: [{ op: "add", path: ["folder-1"], value: folder }],
|
|
},
|
|
],
|
|
result: { folderId: "folder-1" },
|
|
} as never);
|
|
const patchBuild = vi
|
|
.spyOn(projectApi, "patchBuild")
|
|
.mockResolvedValue({ status: "ok", version: 4 });
|
|
const caller = createCaller(createContext(true));
|
|
const input = {
|
|
projectId: "project-1",
|
|
name: "Editor assets",
|
|
confirm: true,
|
|
};
|
|
|
|
await expect(caller.assetFolders.create(input)).resolves.toMatchObject({
|
|
folderId: "folder-1",
|
|
version: 4,
|
|
});
|
|
expect(patchBuild).toHaveBeenCalledTimes(1);
|
|
|
|
loadAssetData.mockResolvedValue({
|
|
assets: [],
|
|
assetFolders: [{ ...folder, createdAt: "2026-01-01T00:00:00Z" }],
|
|
});
|
|
await expect(caller.assetFolders.create(input)).resolves.toMatchObject({
|
|
folderId: "folder-1",
|
|
});
|
|
expect(patchBuild).toHaveBeenCalledTimes(2);
|
|
|
|
loadAssetData.mockResolvedValue({
|
|
assets: [],
|
|
assetFolders: [{ ...folder, name: "Existing folder" }],
|
|
} as never);
|
|
await expect(caller.assetFolders.create(input)).rejects.toMatchObject({
|
|
code: "FORBIDDEN",
|
|
});
|
|
expect(patchBuild).toHaveBeenCalledTimes(2);
|
|
|
|
getTokenInfo.mockResolvedValue(createToken({ relation: "viewers" }));
|
|
await expect(caller.assetFolders.create(input)).rejects.toMatchObject({
|
|
code: "FORBIDDEN",
|
|
});
|
|
expect(patchBuild).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
test("adds api permit only when token and plan both allow it", () => {
|
|
const token = createToken();
|
|
|
|
expect(getTokenPermits(token, createContext(true))).toEqual([
|
|
"view",
|
|
"edit",
|
|
"build",
|
|
"api",
|
|
]);
|
|
expect(getTokenPermits(token, createContext(false))).toEqual([
|
|
"view",
|
|
"edit",
|
|
"build",
|
|
]);
|
|
expect(
|
|
getTokenPermits(
|
|
{
|
|
...token,
|
|
canUseApi: false,
|
|
},
|
|
createContext(true)
|
|
)
|
|
).toEqual(["view", "edit", "build"]);
|
|
});
|
|
|
|
test("maps token relation to project permits", () => {
|
|
expect(
|
|
getTokenPermits(createToken({ relation: "viewers" }), createContext(true))
|
|
).toEqual(["view", "api"]);
|
|
expect(
|
|
getTokenPermits(createToken({ relation: "editors" }), createContext(true))
|
|
).toEqual(["view", "edit", "api"]);
|
|
expect(
|
|
getTokenPermits(
|
|
createToken({ relation: "builders" }),
|
|
createContext(true)
|
|
)
|
|
).toEqual(["view", "edit", "build", "api"]);
|
|
expect(
|
|
getTokenPermits(
|
|
createToken({ relation: "administrators" }),
|
|
createContext(true)
|
|
)
|
|
).toEqual(["view", "edit", "build", "admin", "api"]);
|
|
});
|
|
|
|
test("accepts cookie auth for project-scoped api procedures", async () => {
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
|
|
await expect(
|
|
assertApiProjectPermit(
|
|
createContext(true, {
|
|
type: "user",
|
|
userId: "user-1",
|
|
sessionCreatedAt: 0,
|
|
isLoggedInToBuilder: async () => true,
|
|
}),
|
|
"project-1",
|
|
"view"
|
|
)
|
|
).resolves.toEqual({
|
|
type: "user",
|
|
permits: ["view"],
|
|
});
|
|
});
|
|
|
|
test("rejects cookie auth without the requested project permit", async () => {
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(false);
|
|
|
|
await expect(
|
|
assertApiProjectPermit(
|
|
createContext(true, {
|
|
type: "user",
|
|
userId: "user-1",
|
|
sessionCreatedAt: 0,
|
|
isLoggedInToBuilder: async () => true,
|
|
}),
|
|
"project-1",
|
|
"edit"
|
|
)
|
|
).rejects.toThrow("You don't have access to this project");
|
|
});
|
|
|
|
test("preserves content-only permissions for cookie-authenticated editors", async () => {
|
|
const hasProjectPermit = vi
|
|
.spyOn(authorizeProject, "hasProjectPermit")
|
|
.mockImplementation(async ({ permit }) => permit === "edit");
|
|
const context = createContext(true, {
|
|
type: "user",
|
|
userId: "user-1",
|
|
sessionCreatedAt: 0,
|
|
isLoggedInToBuilder: async () => true,
|
|
});
|
|
|
|
await expect(
|
|
assertApiProjectPermit(context, "project-1", "edit")
|
|
).resolves.toEqual({ type: "user", permits: ["edit"] });
|
|
|
|
hasProjectPermit.mockResolvedValue(true);
|
|
await expect(
|
|
assertApiProjectPermit(context, "project-1", "edit")
|
|
).resolves.toEqual({ type: "user", permits: ["edit", "build"] });
|
|
});
|
|
|
|
test("rejects tokens from another project", async () => {
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ projectId: "project-2" })
|
|
);
|
|
|
|
await expect(
|
|
assertApiProjectPermit(createContext(true), "project-1", "view")
|
|
).rejects.toThrow("Authorization token is not valid for project");
|
|
});
|
|
|
|
test("rejects tokens without api permission", async () => {
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ canUseApi: false })
|
|
);
|
|
|
|
await expect(assertApiTokenPermit(createContext(true))).rejects.toThrow(
|
|
"Authorization token cannot use Builder API"
|
|
);
|
|
await expect(
|
|
assertApiProjectPermit(createContext(true), "project-1", "view")
|
|
).rejects.toThrow("Authorization token cannot use Builder API");
|
|
});
|
|
|
|
test("allows token introspection without api permission", async () => {
|
|
const token = createToken({ canUseApi: false, relation: "editors" });
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(token);
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(caller.auth.me()).resolves.toEqual({
|
|
actor: { type: "token", tokenId: token.token },
|
|
projectId: token.projectId,
|
|
relation: "editors",
|
|
permits: ["view", "edit"],
|
|
});
|
|
await expect(
|
|
caller.projects.permissions({ projectId: token.projectId })
|
|
).resolves.toEqual({
|
|
relation: "editors",
|
|
permits: ["view", "edit"],
|
|
canView: true,
|
|
canEdit: true,
|
|
canBuild: false,
|
|
canAdmin: false,
|
|
canOwn: false,
|
|
canUseApi: false,
|
|
canPublish: false,
|
|
canPublishProjectDomain: false,
|
|
canPublishCustomDomains: false,
|
|
apiContract: {
|
|
version: publicApiContractVersion,
|
|
operationIds: publicApiOperations.flatMap((operation) =>
|
|
publicApiOperationRequiresServerSupport(operation)
|
|
? [operation.id]
|
|
: []
|
|
),
|
|
},
|
|
});
|
|
});
|
|
|
|
test("returns publish capabilities from token permissions", async () => {
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
const caller = createCaller(createContext(true));
|
|
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "editors", canPublish: true })
|
|
);
|
|
await expect(
|
|
caller.projects.permissions({ projectId: "project-1" })
|
|
).resolves.toMatchObject({
|
|
relation: "editors",
|
|
canPublish: true,
|
|
canPublishProjectDomain: true,
|
|
canPublishCustomDomains: true,
|
|
});
|
|
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "builders", canPublish: false })
|
|
);
|
|
await expect(
|
|
caller.projects.permissions({ projectId: "project-1" })
|
|
).resolves.toMatchObject({
|
|
relation: "builders",
|
|
canPublish: false,
|
|
canPublishProjectDomain: true,
|
|
canPublishCustomDomains: false,
|
|
});
|
|
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "administrators", canPublish: true })
|
|
);
|
|
await expect(
|
|
caller.projects.permissions({ projectId: "project-1" })
|
|
).resolves.toMatchObject({
|
|
relation: "administrators",
|
|
canPublish: true,
|
|
canPublishProjectDomain: true,
|
|
canPublishCustomDomains: true,
|
|
});
|
|
});
|
|
|
|
test("rejects tokens when current plan does not allow api permission", async () => {
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
|
|
await expect(
|
|
assertApiProjectPermit(createContext(false), "project-1", "view")
|
|
).rejects.toThrow("Authorization token cannot use Builder API");
|
|
});
|
|
|
|
test("rejects tokens without the required relation permit", async () => {
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "viewers" })
|
|
);
|
|
|
|
await expect(
|
|
assertApiProjectPermit(createContext(true), "project-1", "edit")
|
|
).rejects.toThrow("Authorization token does not have edit permission");
|
|
});
|
|
|
|
test("requires build permission before semantic build mutation handlers", async () => {
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "viewers" })
|
|
);
|
|
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(
|
|
caller.pages.update({
|
|
projectId: "project-1",
|
|
pageId: "page-id",
|
|
values: {},
|
|
})
|
|
).rejects.toThrow("Authorization token does not have build permission");
|
|
});
|
|
|
|
test("checks project authorization after token permission checks", async () => {
|
|
const hasProjectPermit = vi
|
|
.spyOn(authorizeProject, "hasProjectPermit")
|
|
.mockResolvedValue(false);
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
|
|
await expect(
|
|
assertApiProjectPermit(createContext(true), "project-1", "view")
|
|
).rejects.toThrow("You don't have access to this project");
|
|
|
|
expect(hasProjectPermit).toHaveBeenCalledWith(
|
|
{ projectId: "project-1", permit: "view" },
|
|
expect.anything()
|
|
);
|
|
});
|
|
|
|
test("returns token and effective permits when access is allowed", async () => {
|
|
const token = createToken({ relation: "editors" });
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(token);
|
|
|
|
await expect(
|
|
assertApiProjectPermit(createContext(true), "project-1", "edit")
|
|
).resolves.toEqual({
|
|
type: "token",
|
|
token,
|
|
permits: ["view", "edit", "api"],
|
|
});
|
|
});
|
|
|
|
test("requires publish permission for api publish domains", () => {
|
|
const project = { domain: "project.wstd.dev" };
|
|
|
|
expect(() =>
|
|
assertApiPublishDomains({
|
|
auth: { type: "user", permits: ["edit"] },
|
|
domains: ["custom.example.com"],
|
|
project,
|
|
})
|
|
).not.toThrow();
|
|
|
|
expect(() =>
|
|
assertApiPublishDomains({
|
|
auth: {
|
|
type: "token",
|
|
token: createToken({ relation: "editors", canPublish: false }),
|
|
permits: ["view", "edit", "api"],
|
|
},
|
|
domains: ["project.wstd.dev"],
|
|
project,
|
|
})
|
|
).toThrow("Authorization token does not have publish permission");
|
|
|
|
expect(() =>
|
|
assertApiPublishDomains({
|
|
auth: {
|
|
type: "token",
|
|
token: createToken({ relation: "editors", canPublish: true }),
|
|
permits: ["view", "edit", "api"],
|
|
},
|
|
domains: ["custom.example.com"],
|
|
project,
|
|
})
|
|
).not.toThrow();
|
|
|
|
expect(() =>
|
|
assertApiPublishDomains({
|
|
auth: {
|
|
type: "token",
|
|
token: createToken({ relation: "builders", canPublish: false }),
|
|
permits: ["view", "edit", "build", "api"],
|
|
},
|
|
domains: ["project.wstd.dev"],
|
|
project,
|
|
})
|
|
).not.toThrow();
|
|
|
|
expect(() =>
|
|
assertApiPublishDomains({
|
|
auth: {
|
|
type: "token",
|
|
token: createToken({ relation: "builders", canPublish: false }),
|
|
permits: ["view", "edit", "build", "api"],
|
|
},
|
|
domains: ["custom.example.com"],
|
|
project,
|
|
})
|
|
).toThrow("Authorization token does not have publish permission");
|
|
|
|
expect(() =>
|
|
assertApiPublishDomains({
|
|
auth: {
|
|
type: "token",
|
|
token: createToken({
|
|
relation: "administrators",
|
|
canPublish: true,
|
|
}),
|
|
permits: ["view", "edit", "build", "admin", "api"],
|
|
},
|
|
domains: ["custom.example.com"],
|
|
project,
|
|
})
|
|
).not.toThrow();
|
|
});
|
|
|
|
test.each([
|
|
{
|
|
target: "staging" as const,
|
|
domains: ["project.wstd.dev"],
|
|
expected: ["project.wstd.dev"],
|
|
},
|
|
{
|
|
target: "production" as const,
|
|
domains: ["custom.example.com"],
|
|
expected: ["custom.example.com"],
|
|
},
|
|
{
|
|
target: "production" as const,
|
|
domains: undefined,
|
|
expected: ["project.wstd.dev", "custom.example.com"],
|
|
},
|
|
])(
|
|
"validates $target publish domains $domains and content without publishing",
|
|
async ({ target, domains, expected }) => {
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "editors", canPublish: true })
|
|
);
|
|
vi.spyOn(projectApi, "loadById").mockResolvedValue({
|
|
domain: "project.wstd.dev",
|
|
domainsVirtual: [
|
|
{ domain: "custom.example.com", status: "ACTIVE", verified: true },
|
|
],
|
|
} as never);
|
|
const loadBuild = vi
|
|
.spyOn(projectBuild, "loadDevBuildByProjectId")
|
|
.mockResolvedValue({
|
|
pages: createDefaultPages({ rootInstanceId: "root" }),
|
|
breakpoints: [],
|
|
styles: [],
|
|
styleSources: [],
|
|
styleSourceSelections: [],
|
|
dataSources: [],
|
|
props: [],
|
|
resources: [],
|
|
instances: [],
|
|
} as never);
|
|
vi.spyOn(assetUploader, "loadAssetDataByProject").mockResolvedValue({
|
|
assets: [],
|
|
assetFolders: [],
|
|
});
|
|
const loadBundle = vi
|
|
.spyOn(db, "loadProjectBundleByProjectId")
|
|
.mockResolvedValue(createPublishedProjectBundleFixture());
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(
|
|
caller.publish.validate({
|
|
projectId: "project-1",
|
|
target,
|
|
domains,
|
|
})
|
|
).resolves.toMatchObject({
|
|
valid: true,
|
|
target,
|
|
domains: expected,
|
|
diagnostics: { mdxOmissions: [], mdxErrors: [] },
|
|
cssValid: true,
|
|
});
|
|
expect(loadBuild).toHaveBeenCalledWith(expect.anything(), "project-1");
|
|
expect(loadBundle).toHaveBeenCalledWith(
|
|
"project-1",
|
|
expect.anything(),
|
|
expect.objectContaining({
|
|
build: expect.objectContaining({ instances: [] }),
|
|
onMdxTemplateOmissions: expect.any(Function),
|
|
})
|
|
);
|
|
}
|
|
);
|
|
|
|
test("warns before publishing when generated CSS cannot build", async () => {
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ canPublish: true })
|
|
);
|
|
vi.spyOn(projectApi, "loadById").mockResolvedValue({
|
|
domain: "project.wstd.dev",
|
|
domainsVirtual: [],
|
|
} as never);
|
|
const build = createEditableContentBuild();
|
|
build.breakpoints = [{ id: "base", label: "Base" }];
|
|
build.styleSourceSelections = [{ instanceId: "root", values: ["source"] }];
|
|
build.styles = [
|
|
{
|
|
styleSourceId: "source",
|
|
breakpointId: "base",
|
|
state: "/*",
|
|
property: "color",
|
|
value: { type: "keyword", value: "red" },
|
|
},
|
|
];
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(build);
|
|
vi.spyOn(assetUploader, "loadAssetDataByProject").mockResolvedValue({
|
|
assets: [],
|
|
assetFolders: [],
|
|
});
|
|
vi.spyOn(db, "loadProjectBundleByProjectId").mockResolvedValue(
|
|
createPublishedProjectBundleFixture()
|
|
);
|
|
|
|
await expect(
|
|
createCaller(createContext(true)).publish.validate({
|
|
projectId: "project-1",
|
|
target: "staging",
|
|
})
|
|
).resolves.toMatchObject({ valid: true, cssValid: false });
|
|
build.styles[0].state = ":hover";
|
|
await expect(
|
|
createCaller(createContext(true)).publish.validate({
|
|
projectId: "project-1",
|
|
target: "staging",
|
|
})
|
|
).resolves.toMatchObject({ valid: true, cssValid: true });
|
|
build.styles[0].value = { type: "unparsed", value: "red }" };
|
|
await expect(
|
|
createCaller(createContext(true)).publish.validate({
|
|
projectId: "project-1",
|
|
target: "staging",
|
|
})
|
|
).resolves.toMatchObject({ valid: true, cssValid: true });
|
|
build.styles[0].value = { type: "keyword", value: "red" };
|
|
build.breakpoints[0].condition = "(prefers-reduced-motion: reduce)";
|
|
await expect(
|
|
createCaller(createContext(true)).publish.validate({
|
|
projectId: "project-1",
|
|
target: "staging",
|
|
})
|
|
).resolves.toMatchObject({ valid: true, cssValid: true });
|
|
for (const condition of [
|
|
"(orientation: portrait) or (hover: hover)",
|
|
"(orientation: portrait) and (hover: hover)",
|
|
]) {
|
|
build.breakpoints[0].condition = condition;
|
|
await expect(
|
|
createCaller(createContext(true)).publish.validate({
|
|
projectId: "project-1",
|
|
target: "staging",
|
|
})
|
|
).resolves.toMatchObject({ valid: true, cssValid: true });
|
|
}
|
|
});
|
|
|
|
test.each(
|
|
[
|
|
[],
|
|
["unknown.example.com"],
|
|
["unverified.example.com"],
|
|
["inactive.example.com"],
|
|
["project.wstd.dev", "unknown.example.com"],
|
|
].map((domains) => ({ domains }))
|
|
)(
|
|
"rejects unusable publish domains $domains before loading content",
|
|
async ({ domains }) => {
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ canPublish: true })
|
|
);
|
|
vi.spyOn(projectApi, "loadById").mockResolvedValue({
|
|
domain: "project.wstd.dev",
|
|
domainsVirtual: [
|
|
{
|
|
domain: "unverified.example.com",
|
|
status: "ACTIVE",
|
|
verified: false,
|
|
},
|
|
{ domain: "inactive.example.com", status: "PENDING", verified: true },
|
|
],
|
|
} as never);
|
|
const loadBuild = vi.spyOn(projectBuild, "loadDevBuildByProjectId");
|
|
const loadBundle = vi
|
|
.spyOn(db, "loadProjectBundleByProjectId")
|
|
.mockResolvedValue(createPublishedProjectBundleFixture());
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(
|
|
caller.publish.validate({
|
|
projectId: "project-1",
|
|
target: "production",
|
|
domains,
|
|
})
|
|
).rejects.toMatchObject({ code: "BAD_REQUEST" });
|
|
expect(loadBuild).not.toHaveBeenCalled();
|
|
expect(loadBundle).not.toHaveBeenCalled();
|
|
}
|
|
);
|
|
|
|
test.each(["variable", "prop"])(
|
|
"rejects a missing resource referenced by a %s before diagnostics",
|
|
async (source) => {
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
vi.spyOn(projectApi, "loadById").mockResolvedValue({
|
|
domain: "project.wstd.dev",
|
|
domainsVirtual: [],
|
|
} as never);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue({
|
|
resources: [],
|
|
instances: [],
|
|
dataSources:
|
|
source === "variable"
|
|
? [
|
|
{
|
|
id: "broken",
|
|
type: "resource",
|
|
name: "Broken",
|
|
scopeInstanceId: "body",
|
|
resourceId: "missing-resource",
|
|
},
|
|
]
|
|
: [],
|
|
props:
|
|
source === "prop"
|
|
? [
|
|
{
|
|
id: "broken",
|
|
instanceId: "body",
|
|
name: "data",
|
|
type: "resource",
|
|
value: "missing-resource",
|
|
},
|
|
]
|
|
: [],
|
|
} as never);
|
|
const loadBundle = vi
|
|
.spyOn(db, "loadProjectBundleByProjectId")
|
|
.mockResolvedValue(createPublishedProjectBundleFixture());
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(
|
|
caller.publish.validate({
|
|
projectId: "project-1",
|
|
target: "staging",
|
|
})
|
|
).rejects.toMatchObject({ code: "BAD_REQUEST" });
|
|
expect(loadBundle).not.toHaveBeenCalled();
|
|
}
|
|
);
|
|
|
|
test("does not run publish diagnostics when the selected domains are forbidden", async () => {
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "builders", canPublish: false })
|
|
);
|
|
vi.spyOn(projectApi, "loadById").mockResolvedValue({
|
|
domain: "project.wstd.dev",
|
|
} as never);
|
|
const loadBundle = vi.spyOn(db, "loadProjectBundleByProjectId");
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(
|
|
caller.publish.validate({
|
|
projectId: "project-1",
|
|
target: "production",
|
|
domains: ["custom.example.com"],
|
|
})
|
|
).rejects.toThrow("Authorization token does not have publish permission");
|
|
expect(loadBundle).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("allows editor api tokens to commit content-mode payloads only", () => {
|
|
const build = createEditableContentBuild();
|
|
const editorAuth = {
|
|
type: "token",
|
|
token: createToken({ relation: "editors" }),
|
|
permits: ["view", "edit", "api"],
|
|
} satisfies Awaited<ReturnType<typeof assertApiProjectPermit>>;
|
|
const builderAuth = {
|
|
type: "token",
|
|
token: createToken({ relation: "builders" }),
|
|
permits: ["view", "edit", "build", "api"],
|
|
} satisfies Awaited<ReturnType<typeof assertApiProjectPermit>>;
|
|
const contentPayload: z.infer<typeof buildPatchTransaction>["payload"] = [
|
|
{
|
|
namespace: "instances",
|
|
patches: [
|
|
{
|
|
op: "replace",
|
|
path: ["text-instance", "children", 0],
|
|
value: { type: "text", value: "New" },
|
|
},
|
|
],
|
|
},
|
|
];
|
|
const designPayload: z.infer<typeof buildPatchTransaction>["payload"] = [
|
|
{
|
|
namespace: "styles",
|
|
patches: [
|
|
{
|
|
op: "add",
|
|
path: ["style"],
|
|
value: {
|
|
breakpointId: "base",
|
|
property: "color",
|
|
styleSourceId: "local",
|
|
value: { type: "keyword", value: "red" },
|
|
},
|
|
},
|
|
],
|
|
},
|
|
];
|
|
|
|
expect(() =>
|
|
assertContentOrBuildTransactions({
|
|
auth: editorAuth,
|
|
build,
|
|
transactions: [{ id: "content", payload: contentPayload }],
|
|
})
|
|
).not.toThrow();
|
|
expect(() =>
|
|
assertContentOrBuildTransactions({
|
|
auth: editorAuth,
|
|
build,
|
|
transactions: [{ id: "design", payload: designPayload }],
|
|
})
|
|
).toThrow("content mode");
|
|
expect(() =>
|
|
assertContentOrBuildTransactions({
|
|
auth: builderAuth,
|
|
build,
|
|
transactions: [{ id: "design", payload: designPayload }],
|
|
})
|
|
).not.toThrow();
|
|
|
|
const emptyTransactions = Array.from({ length: 11 }, (_, index) => ({
|
|
id: `empty-${index}`,
|
|
payload: [],
|
|
}));
|
|
expect(() =>
|
|
assertContentOrBuildTransactions({
|
|
auth: editorAuth,
|
|
build,
|
|
scope: "text-and-props",
|
|
transactions: [
|
|
{
|
|
id: "append",
|
|
payload: [
|
|
{
|
|
namespace: "instances",
|
|
patches: [
|
|
{
|
|
op: "add",
|
|
path: ["text-instance", "children", 1],
|
|
value: { type: "text", value: "Appended" },
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
id: "edit-appended",
|
|
payload: [
|
|
{
|
|
namespace: "instances",
|
|
patches: [
|
|
{
|
|
op: "replace",
|
|
path: ["text-instance", "children", 1],
|
|
value: { type: "text", value: "Edited" },
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
})
|
|
).not.toThrow();
|
|
expect(() =>
|
|
assertContentOrBuildTransactions({
|
|
auth: editorAuth,
|
|
build,
|
|
transactions: emptyTransactions.slice(0, 10),
|
|
})
|
|
).not.toThrow();
|
|
expect(() =>
|
|
assertContentOrBuildTransactions({
|
|
auth: editorAuth,
|
|
build,
|
|
transactions: emptyTransactions,
|
|
})
|
|
).toThrow("You don't have permission to build this project.");
|
|
expect(() =>
|
|
assertContentOrBuildTransactions({
|
|
auth: builderAuth,
|
|
build,
|
|
transactions: emptyTransactions,
|
|
})
|
|
).not.toThrow();
|
|
const repeatedPayload = Array.from(
|
|
{ length: 101 },
|
|
() => contentPayload[0]
|
|
);
|
|
expect(() =>
|
|
assertContentOrBuildTransactions({
|
|
auth: editorAuth,
|
|
build,
|
|
transactions: [{ id: "repeated", payload: repeatedPayload }],
|
|
})
|
|
).toThrow("You don't have permission to build this project.");
|
|
expect(() =>
|
|
assertContentOrBuildTransactions({
|
|
auth: editorAuth,
|
|
build,
|
|
transactions: [
|
|
{ id: "boundary", payload: repeatedPayload.slice(0, 100) },
|
|
],
|
|
})
|
|
).not.toThrow();
|
|
expect(() =>
|
|
assertContentOrBuildTransactions({
|
|
auth: builderAuth,
|
|
build,
|
|
transactions: [{ id: "repeated", payload: repeatedPayload }],
|
|
})
|
|
).not.toThrow();
|
|
for (const split of [false, true]) {
|
|
for (const count of [1000, 1001]) {
|
|
const patches = Array.from(
|
|
{ length: count },
|
|
() => contentPayload[0].patches[0]
|
|
);
|
|
const transactions = (
|
|
split ? [patches.slice(0, 500), patches.slice(500)] : [patches]
|
|
).map((patches, index) => ({
|
|
id: `patch-limit-${index}`,
|
|
payload: [{ namespace: "instances" as const, patches }],
|
|
}));
|
|
const assertEditor = () =>
|
|
assertContentOrBuildTransactions({
|
|
auth: editorAuth,
|
|
build,
|
|
transactions,
|
|
});
|
|
if (count === 1000) {
|
|
expect(assertEditor).not.toThrow();
|
|
} else {
|
|
expect(assertEditor).toThrow(
|
|
"You don't have permission to build this project."
|
|
);
|
|
}
|
|
expect(() =>
|
|
assertContentOrBuildTransactions({
|
|
auth: builderAuth,
|
|
build,
|
|
transactions,
|
|
})
|
|
).not.toThrow();
|
|
}
|
|
}
|
|
});
|
|
|
|
test("preserves stale Editor retries without bypassing current or future patch validation", async () => {
|
|
const build = createEditableContentBuild();
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "editors" })
|
|
);
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(build);
|
|
const patchBuild = vi
|
|
.spyOn(projectApi, "patchBuild")
|
|
.mockResolvedValue({ status: "ok", version: 3 });
|
|
const caller = createCaller(createContext(true));
|
|
const input = {
|
|
projectId: "project-1",
|
|
baseVersion: 2,
|
|
transactions: [
|
|
{
|
|
id: "saved",
|
|
payload: [
|
|
{
|
|
namespace: "props" as const,
|
|
patches: [{ op: "remove" as const, path: ["already-removed"] }],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
await expect(caller.build.patch(input)).resolves.toEqual({ version: 3 });
|
|
expect(patchBuild).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
clientVersion: 2,
|
|
transactions: input.transactions,
|
|
}),
|
|
expect.anything()
|
|
);
|
|
patchBuild.mockResolvedValue({
|
|
status: "version_mismatched",
|
|
errors: "Stale version",
|
|
});
|
|
await expect(caller.build.patch(input)).rejects.toMatchObject({
|
|
code: "CONFLICT",
|
|
});
|
|
patchBuild.mockClear();
|
|
for (const baseVersion of [3, 4]) {
|
|
await expect(
|
|
caller.build.patch({ ...input, baseVersion })
|
|
).rejects.toThrow();
|
|
}
|
|
expect(patchBuild).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("rejects a future Editor version before persistence can catch up", async () => {
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "editors" })
|
|
);
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(
|
|
createEditableContentBuild()
|
|
);
|
|
// Persistence could observe version 4 after a concurrent Builder save
|
|
// moves text-instance outside the editable Content Block.
|
|
const patchBuild = vi
|
|
.spyOn(projectApi, "patchBuild")
|
|
.mockResolvedValue({ status: "ok", version: 5 });
|
|
await expect(
|
|
createCaller(createContext(true)).build.patch({
|
|
projectId: "project-1",
|
|
baseVersion: 4,
|
|
transactions: [
|
|
{
|
|
id: "future-edit",
|
|
payload: [
|
|
{
|
|
namespace: "instances",
|
|
patches: [
|
|
{
|
|
op: "replace",
|
|
path: ["text-instance", "children", 0],
|
|
value: { type: "text", value: "Changed" },
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
})
|
|
).rejects.toMatchObject({ code: "CONFLICT" });
|
|
expect(patchBuild).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("commits content-mode mutations with editor api tokens", async () => {
|
|
const build = createEditableContentBuild();
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "editors" })
|
|
);
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(build);
|
|
const patchBuild = vi
|
|
.spyOn(projectApi, "patchBuild")
|
|
.mockResolvedValue({ status: "ok", version: 4 });
|
|
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(
|
|
caller.instances.updateText({
|
|
projectId: "project-1",
|
|
instanceId: "text-instance",
|
|
childIndex: 0,
|
|
text: "New",
|
|
mode: "text",
|
|
})
|
|
).resolves.toMatchObject({
|
|
version: 4,
|
|
instanceId: "text-instance",
|
|
childIndex: 0,
|
|
mode: "text",
|
|
});
|
|
|
|
expect(patchBuild).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
buildId: "build-1",
|
|
projectId: "project-1",
|
|
clientVersion: 3,
|
|
}),
|
|
expect.objectContaining({
|
|
authorization: expect.objectContaining({ type: "token" }),
|
|
})
|
|
);
|
|
expect(patchBuild).toHaveBeenCalledTimes(1);
|
|
const committed = patchBuild.mock.calls[0][0];
|
|
const changes = committed.transactions.flatMap(({ payload }) => payload);
|
|
expect(changes.map(({ namespace }) => namespace)).toEqual(["instances"]);
|
|
const saved = applyBuilderNamespacePatches(
|
|
new Map(build.instances.map((instance) => [instance.id, instance])),
|
|
changes.flatMap(({ patches }) => patches)
|
|
);
|
|
expect(saved.get("text-instance")?.children).toEqual([
|
|
{ type: "text", value: "New" },
|
|
]);
|
|
expect(saved.get("outside")).toEqual(
|
|
build.instances.find(({ id }) => id === "outside")
|
|
);
|
|
expect(
|
|
build.instances.find(({ id }) => id === "text-instance")?.children
|
|
).toEqual([{ type: "text", value: "Old" }]);
|
|
});
|
|
|
|
test("rejects Editor mutations inside protected Content Block templates", async () => {
|
|
const build = createEditableContentBuild();
|
|
const block = build.instances.find(({ id }) => id === "block")!;
|
|
block.children = [
|
|
{ type: "id", value: "block-body" },
|
|
{ type: "id", value: "template-container" },
|
|
];
|
|
build.instances.push({
|
|
type: "instance",
|
|
id: "block-body",
|
|
component: blockBodyComponent,
|
|
children: [
|
|
{ type: "id", value: "text-instance" },
|
|
{ type: "id", value: "other-instance" },
|
|
],
|
|
});
|
|
const nestedBody = build.instances.find(
|
|
({ id }) => id === "other-instance"
|
|
)!;
|
|
nestedBody.tag = "div";
|
|
nestedBody.children = [{ type: "id", value: "nested-text" }];
|
|
build.instances.push({
|
|
type: "instance",
|
|
id: "nested-text",
|
|
component: "ws:element",
|
|
tag: "p",
|
|
children: [{ type: "text", value: "Nested body" }],
|
|
});
|
|
build.instances.push(
|
|
{
|
|
type: "instance",
|
|
id: "template-container",
|
|
component: blockTemplateComponent,
|
|
children: [{ type: "id", value: "template-paragraph" }],
|
|
},
|
|
{
|
|
type: "instance",
|
|
id: "template-paragraph",
|
|
component: "Paragraph",
|
|
children: [{ type: "text", value: "Template default" }],
|
|
}
|
|
);
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "editors" })
|
|
);
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(build);
|
|
vi.spyOn(assetUploader, "loadAssetDataByProject").mockResolvedValue({
|
|
assets: [],
|
|
assetFolders: [],
|
|
});
|
|
const patchBuild = vi
|
|
.spyOn(projectApi, "patchBuild")
|
|
.mockResolvedValue({ status: "ok", version: 4 });
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(
|
|
caller.instances.insertFragment({
|
|
projectId: "project-1",
|
|
parentInstanceId: "block-body",
|
|
fragment: await parseWebstudioJsxFragment(
|
|
"<p>New editable content</p>"
|
|
),
|
|
contentMode: true,
|
|
})
|
|
).resolves.toMatchObject({ version: 4 });
|
|
|
|
await expect(
|
|
caller.instances.insertFragment({
|
|
projectId: "project-1",
|
|
parentInstanceId: "other-instance",
|
|
fragment: await parseWebstudioJsxFragment(
|
|
"<p>New nested editable content</p>"
|
|
),
|
|
contentMode: true,
|
|
})
|
|
).resolves.toMatchObject({ version: 4 });
|
|
|
|
await expect(
|
|
caller.instances.updateText({
|
|
projectId: "project-1",
|
|
instanceId: "template-paragraph",
|
|
childIndex: 0,
|
|
text: "Changed template default",
|
|
})
|
|
).rejects.toMatchObject({ code: "FORBIDDEN" });
|
|
|
|
const templateFragment = await parseWebstudioJsxFragment(
|
|
"<p>New template child</p>"
|
|
);
|
|
for (const contentMode of [true, false, undefined]) {
|
|
await expect(
|
|
caller.instances.insertFragment({
|
|
projectId: "project-1",
|
|
parentInstanceId: "template-container",
|
|
fragment: templateFragment,
|
|
...(contentMode === undefined ? {} : { contentMode }),
|
|
})
|
|
).rejects.toMatchObject({ code: "FORBIDDEN" });
|
|
}
|
|
expect(patchBuild).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
test("Editor property writes load assets without gaining Builder permissions", async () => {
|
|
const build = createEditableContentBuild();
|
|
const image = build.instances.find(({ id }) => id === "other-instance")!;
|
|
image.component = "Image";
|
|
image.children = [];
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "editors" })
|
|
);
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(build);
|
|
vi.spyOn(assetUploader, "loadAssetDataByProject").mockResolvedValue({
|
|
assets: [
|
|
{
|
|
id: "image",
|
|
projectId: "project-1",
|
|
name: "image.png",
|
|
type: "image",
|
|
format: "png",
|
|
meta: { width: 120, height: 80 },
|
|
size: 100,
|
|
createdAt: "",
|
|
},
|
|
{
|
|
id: "text",
|
|
projectId: "project-1",
|
|
name: "data.txt",
|
|
type: "file",
|
|
format: "txt",
|
|
meta: {},
|
|
size: 1,
|
|
createdAt: "",
|
|
},
|
|
],
|
|
assetFolders: [],
|
|
});
|
|
const patchBuild = vi
|
|
.spyOn(projectApi, "patchBuild")
|
|
.mockResolvedValue({ status: "ok", version: 4 });
|
|
const caller = createCaller(createContext(true));
|
|
await expect(
|
|
caller.instances.updateProps({
|
|
projectId: "project-1",
|
|
updates: [
|
|
{ instanceId: image.id, name: "src", type: "asset", value: "image" },
|
|
],
|
|
})
|
|
).resolves.toMatchObject({ version: 4 });
|
|
const changes = patchBuild.mock.calls[0][0].transactions.flatMap(
|
|
({ payload }) => payload
|
|
);
|
|
expect(changes.map(({ namespace }) => namespace)).toEqual(["props"]);
|
|
const saved = applyBuilderNamespacePatches(
|
|
new Map(build.props.map((prop) => [prop.id, prop])),
|
|
changes[0].patches
|
|
);
|
|
expect(
|
|
[...saved.values()].map(({ name, type, value }) => ({
|
|
name,
|
|
type,
|
|
value,
|
|
}))
|
|
).toEqual(
|
|
["src", "width", "height", "alt"].map((name) => ({
|
|
name,
|
|
type: "asset",
|
|
value: "image",
|
|
}))
|
|
);
|
|
await expect(
|
|
caller.instances.updateProps({
|
|
projectId: "project-1",
|
|
updates: [
|
|
{ instanceId: image.id, name: "src", type: "asset", value: "text" },
|
|
],
|
|
})
|
|
).rejects.toMatchObject({ code: "BAD_REQUEST" });
|
|
await expect(
|
|
caller.instances.updateProps({
|
|
projectId: "project-1",
|
|
updates: [
|
|
{
|
|
instanceId: "outside",
|
|
name: "title",
|
|
type: "string",
|
|
value: "Forbidden",
|
|
},
|
|
],
|
|
})
|
|
).rejects.toMatchObject({ code: "FORBIDDEN" });
|
|
expect(patchBuild).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
test("limits editor api patches to content instances and props", async () => {
|
|
const build = createEditableContentBuild();
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(
|
|
createToken({ relation: "editors" })
|
|
);
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(build);
|
|
const patchBuild = vi
|
|
.spyOn(projectApi, "patchBuild")
|
|
.mockResolvedValue({ status: "ok", version: 4 });
|
|
const caller = createCaller(createContext(true));
|
|
const textPatch = (instanceId: string) => ({
|
|
namespace: "instances" as const,
|
|
patches: [
|
|
{
|
|
op: "replace" as const,
|
|
path: [instanceId, "children", 0],
|
|
value: { type: "text", value: "New" },
|
|
},
|
|
],
|
|
});
|
|
const setTextPatch = {
|
|
namespace: "instances" as const,
|
|
patches: [
|
|
{
|
|
op: "replace" as const,
|
|
path: ["text-instance", "children"],
|
|
value: [
|
|
{ type: "text", value: "Updated" },
|
|
{ type: "text", value: " again" },
|
|
],
|
|
},
|
|
],
|
|
};
|
|
const replaceInstanceTextPatch = {
|
|
namespace: "instances" as const,
|
|
patches: [
|
|
{
|
|
op: "replace" as const,
|
|
path: ["text-instance"],
|
|
value: {
|
|
type: "instance",
|
|
id: "text-instance",
|
|
component: "ws:element",
|
|
tag: "p",
|
|
children: [{ type: "text", value: "Updated" }],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
const replaceInstanceStructurePatch = {
|
|
namespace: "instances" as const,
|
|
patches: [
|
|
{
|
|
op: "replace" as const,
|
|
path: ["block"],
|
|
value: {
|
|
type: "instance",
|
|
id: "block",
|
|
component: blockComponent,
|
|
children: [
|
|
{ type: "id", value: "other-instance" },
|
|
{ type: "id", value: "text-instance" },
|
|
],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
const stylePatch = {
|
|
namespace: "styles" as const,
|
|
patches: [
|
|
{
|
|
op: "replace" as const,
|
|
path: ["style", "value"],
|
|
value: { type: "keyword", value: "red" },
|
|
},
|
|
],
|
|
};
|
|
const expressionPatch = {
|
|
namespace: "instances" as const,
|
|
patches: [
|
|
{
|
|
op: "replace" as const,
|
|
path: ["text-instance", "children", 0],
|
|
value: { type: "expression", value: "someVariable", mode: "read" },
|
|
},
|
|
],
|
|
};
|
|
const assetPatch = {
|
|
namespace: "assets" as const,
|
|
patches: [{ op: "remove" as const, path: ["asset-id"] }],
|
|
};
|
|
const structuralPatch = {
|
|
namespace: "instances" as const,
|
|
patches: [
|
|
{ op: "remove" as const, path: ["block", "children", 0] },
|
|
{ op: "remove" as const, path: ["text-instance"] },
|
|
],
|
|
};
|
|
const reorderPatch = {
|
|
namespace: "instances" as const,
|
|
patches: [
|
|
{
|
|
op: "replace" as const,
|
|
path: ["block", "children"],
|
|
value: [
|
|
{ type: "id", value: "other-instance" },
|
|
{ type: "id", value: "text-instance" },
|
|
],
|
|
},
|
|
],
|
|
};
|
|
const patch = (
|
|
payloads: Array<z.infer<typeof buildPatchTransaction>["payload"]>
|
|
) => ({
|
|
projectId: "project-1",
|
|
baseVersion: 3,
|
|
transactions: payloads.map((payload, index) => ({
|
|
id: `tx-${index}`,
|
|
payload,
|
|
})),
|
|
});
|
|
|
|
for (const allowed of [
|
|
textPatch("text-instance"),
|
|
setTextPatch,
|
|
replaceInstanceTextPatch,
|
|
]) {
|
|
await expect(caller.build.patch(patch([[allowed]]))).resolves.toEqual({
|
|
version: 4,
|
|
});
|
|
}
|
|
expect(patchBuild).toHaveBeenCalledTimes(3);
|
|
patchBuild.mockClear();
|
|
|
|
for (const forbidden of [
|
|
textPatch("outside"),
|
|
stylePatch,
|
|
expressionPatch,
|
|
assetPatch,
|
|
structuralPatch,
|
|
reorderPatch,
|
|
replaceInstanceStructurePatch,
|
|
]) {
|
|
await expect(caller.build.patch(patch([[forbidden]]))).rejects.toThrow();
|
|
}
|
|
await expect(
|
|
caller.build.patch(patch([[textPatch("text-instance")], [stylePatch]]))
|
|
).rejects.toThrow();
|
|
expect(patchBuild).not.toHaveBeenCalled();
|
|
|
|
vi.mocked(authDb.getTokenInfo).mockResolvedValue(
|
|
createToken({ relation: "builders" })
|
|
);
|
|
await expect(caller.build.patch(patch([[stylePatch]]))).resolves.toEqual({
|
|
version: 4,
|
|
});
|
|
|
|
patchBuild.mockClear();
|
|
vi.mocked(authDb.getTokenInfo).mockResolvedValue(
|
|
createToken({ relation: "viewers" })
|
|
);
|
|
await expect(
|
|
caller.build.patch(patch([[textPatch("text-instance")]]))
|
|
).rejects.toMatchObject({ code: "FORBIDDEN" });
|
|
vi.mocked(authDb.getTokenInfo).mockResolvedValue(
|
|
createToken({ relation: "editors", canUseApi: false })
|
|
);
|
|
await expect(
|
|
caller.build.patch(patch([[textPatch("text-instance")]]))
|
|
).rejects.toMatchObject({ code: "FORBIDDEN" });
|
|
expect(patchBuild).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("does not commit empty runtime mutation payloads", async () => {
|
|
const build = {
|
|
id: "build-1",
|
|
projectId: "project-1",
|
|
version: 7,
|
|
pages: createDefaultPages({ rootInstanceId: "root" }),
|
|
breakpoints: [],
|
|
styles: [],
|
|
styleSources: [],
|
|
styleSourceSelections: [],
|
|
props: [],
|
|
dataSources: [],
|
|
resources: [],
|
|
instances: [
|
|
{
|
|
type: "instance",
|
|
id: "root",
|
|
component: blockComponent,
|
|
children: [],
|
|
},
|
|
],
|
|
marketplaceProduct: {},
|
|
} as unknown as Awaited<
|
|
ReturnType<typeof projectBuild.loadDevBuildByProjectId>
|
|
>;
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(build);
|
|
const patchBuild = vi.spyOn(projectApi, "patchBuild");
|
|
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(
|
|
caller.pages.update({
|
|
projectId: "project-1",
|
|
pageId: build.pages.homePageId,
|
|
values: {},
|
|
})
|
|
).resolves.toEqual({
|
|
version: 7,
|
|
pageId: build.pages.homePageId,
|
|
});
|
|
|
|
expect(patchBuild).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("api instance queries", () => {
|
|
const createInstanceQueryBuild = () =>
|
|
({
|
|
id: "build-1",
|
|
projectId: "project-1",
|
|
version: 1,
|
|
pages: createDefaultPages({ rootInstanceId: "root-1" }),
|
|
instances: [
|
|
{
|
|
type: "instance",
|
|
id: "root-1",
|
|
component: "Body",
|
|
tag: "body",
|
|
label: "Root",
|
|
children: [
|
|
{ type: "id", value: "hero-1" },
|
|
{ type: "text", value: "Root text" },
|
|
],
|
|
},
|
|
{
|
|
type: "instance",
|
|
id: "hero-1",
|
|
component: blockComponent,
|
|
tag: "section",
|
|
label: "Hero",
|
|
children: [{ type: "expression", value: "system.params.slug" }],
|
|
},
|
|
],
|
|
props: [],
|
|
styles: [],
|
|
styleSources: [],
|
|
styleSourceSelections: [],
|
|
resources: [],
|
|
dataSources: [],
|
|
breakpoints: [],
|
|
}) as unknown as Awaited<
|
|
ReturnType<typeof projectBuild.loadDevBuildByProjectId>
|
|
>;
|
|
|
|
test("lists instances through server semantics", async () => {
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(
|
|
createInstanceQueryBuild()
|
|
);
|
|
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(
|
|
caller.instances.list({
|
|
projectId: "project-1",
|
|
pagePath: "",
|
|
maxDepth: 1,
|
|
labelContains: "Hero",
|
|
})
|
|
).resolves.toMatchObject({
|
|
detail: "compact",
|
|
total: 1,
|
|
instances: [
|
|
{
|
|
id: "hero-1",
|
|
label: "Hero",
|
|
component: blockComponent,
|
|
tag: "section",
|
|
depth: 1,
|
|
childCount: 1,
|
|
parentId: "root-1",
|
|
indexWithinParent: 0,
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
test("lists text and expression children through server semantics", async () => {
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(
|
|
createInstanceQueryBuild()
|
|
);
|
|
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(
|
|
caller.instances.listTexts({
|
|
projectId: "project-1",
|
|
pagePath: "",
|
|
mode: "expression",
|
|
})
|
|
).resolves.toEqual({
|
|
texts: [
|
|
{
|
|
instanceId: "hero-1",
|
|
childIndex: 0,
|
|
component: blockComponent,
|
|
label: "Hero",
|
|
mode: "expression",
|
|
valuePreview: "system.params.slug",
|
|
valueLength: 18,
|
|
truncated: false,
|
|
},
|
|
],
|
|
detail: "compact",
|
|
total: 1,
|
|
returnedCount: 1,
|
|
nextCursor: null,
|
|
filters: { pagePath: "", mode: "expression" },
|
|
});
|
|
});
|
|
|
|
test("lists project entities through semantic routes", async () => {
|
|
const build = createInstanceQueryBuild();
|
|
build.pages.folders.set("folder-1", {
|
|
id: "folder-1",
|
|
name: "Blog",
|
|
slug: "blog",
|
|
children: ["page-1"],
|
|
});
|
|
build.pages.folders
|
|
.get(build.pages.rootFolderId)
|
|
?.children.push("folder-1");
|
|
build.pages.pages.set("page-1", {
|
|
id: "page-1",
|
|
name: "Post",
|
|
path: "/first-post",
|
|
title: "Post",
|
|
rootInstanceId: "hero-1",
|
|
meta: { description: "Post description" },
|
|
});
|
|
build.styleSources.push({
|
|
id: "token-1",
|
|
type: "token",
|
|
name: "Primary",
|
|
});
|
|
build.styles.push({
|
|
styleSourceId: "token-1",
|
|
breakpointId: "base",
|
|
property: "color",
|
|
value: { type: "keyword", value: "red" },
|
|
});
|
|
build.styleSourceSelections.push({
|
|
instanceId: "hero-1",
|
|
values: ["token-1"],
|
|
});
|
|
build.dataSources.push(
|
|
{
|
|
id: "variable-1",
|
|
type: "variable",
|
|
scopeInstanceId: "hero-1",
|
|
name: "Title",
|
|
value: { type: "string", value: "Hello" },
|
|
},
|
|
{
|
|
id: "resource-data-source-1",
|
|
type: "resource",
|
|
scopeInstanceId: "hero-1",
|
|
name: "Posts",
|
|
resourceId: "resource-1",
|
|
}
|
|
);
|
|
build.resources.push({
|
|
id: "resource-1",
|
|
name: "Posts",
|
|
method: "get",
|
|
url: '"https://example.com/posts"',
|
|
headers: [],
|
|
});
|
|
vi.spyOn(authDb, "getTokenInfo").mockResolvedValue(createToken());
|
|
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
|
|
vi.spyOn(projectBuild, "loadDevBuildByProjectId").mockResolvedValue(build);
|
|
|
|
const caller = createCaller(createContext(true));
|
|
|
|
await expect(
|
|
caller.pages.list({ projectId: "project-1" })
|
|
).resolves.toMatchObject({
|
|
pages: [
|
|
expect.objectContaining({ isHome: true, path: "" }),
|
|
expect.objectContaining({
|
|
id: "page-1",
|
|
path: "/blog/first-post",
|
|
parentFolderId: "folder-1",
|
|
}),
|
|
],
|
|
});
|
|
await expect(
|
|
caller.pages.getByPath({
|
|
projectId: "project-1",
|
|
path: "/blog/first-post",
|
|
})
|
|
).resolves.toMatchObject({
|
|
id: "page-1",
|
|
meta: { description: "Post description" },
|
|
});
|
|
await expect(
|
|
caller.folders.list({ projectId: "project-1" })
|
|
).resolves.toMatchObject({
|
|
folders: expect.arrayContaining([
|
|
expect.objectContaining({
|
|
id: "folder-1",
|
|
slug: "blog",
|
|
parentFolderId: build.pages.rootFolderId,
|
|
children: ["page-1"],
|
|
}),
|
|
]),
|
|
});
|
|
await expect(
|
|
caller.designTokens.list({
|
|
projectId: "project-1",
|
|
withUsage: true,
|
|
})
|
|
).resolves.toMatchObject({
|
|
detail: "compact",
|
|
total: 1,
|
|
tokens: [
|
|
{
|
|
id: "token-1",
|
|
name: "Primary",
|
|
declarationCount: 1,
|
|
usageCount: 1,
|
|
},
|
|
],
|
|
});
|
|
await expect(
|
|
caller.variables.list({
|
|
projectId: "project-1",
|
|
scopeInstanceId: "hero-1",
|
|
})
|
|
).resolves.toEqual({
|
|
variables: [
|
|
{
|
|
id: "variable-1",
|
|
name: "Title",
|
|
scopeInstanceId: "hero-1",
|
|
valueType: "string",
|
|
},
|
|
],
|
|
detail: "compact",
|
|
total: 1,
|
|
returnedCount: 1,
|
|
nextCursor: null,
|
|
filters: { scopeInstanceId: "hero-1" },
|
|
});
|
|
await expect(
|
|
caller.resources.list({
|
|
projectId: "project-1",
|
|
scopeInstanceId: "hero-1",
|
|
})
|
|
).resolves.toEqual({
|
|
resources: [
|
|
{
|
|
id: "resource-1",
|
|
name: "Posts",
|
|
method: "get",
|
|
url: '"https://example.com/posts"',
|
|
scopeInstanceId: "hero-1",
|
|
exposedAsDataSource: true,
|
|
dataSourceId: "resource-data-source-1",
|
|
},
|
|
],
|
|
detail: "compact",
|
|
total: 1,
|
|
returnedCount: 1,
|
|
nextCursor: null,
|
|
filters: { scopeInstanceId: "hero-1" },
|
|
});
|
|
});
|
|
});
|