Files
Davide Silvestri 89184e9b27 fix: stop editing rows hidden by restricted view filters (#6217)
* fix: stop editing rows hidden by restricted view filters

Cancel edits still queued for a row that the backend reported as hidden,
including pasted values, in grid, gallery, timeline, kanban and calendar,
so they are neither sent nor put back on screen. Close the row modal when
its row gets hidden in a restricted view; other views keep it open.

* test: cover builders and ignore broadcast order in restricted view tests

* test: add e2e tests for restricted view row visibility

Cover creating, editing, pasting and dragging rows in a restricted view
while the HTTP response is held after the server commits, the row modal
closing only in restricted views, and view default values.

* fix: hide rows created with the grid's floating add button when filtered out

createNewRowConfirmed used the single-row endpoint, which doesn't return
hidden_row_ids, so a restricted editor kept a row the view's filters
exclude. Create it through the batch endpoint and report hidden rows like
the other create paths.

* test: expect the batch endpoint for the grid's floating add button

* fix: cancel queued kanban edits when a drag hides the card
2026-10-08 09:23:06 +02:00

243 lines
6.5 KiB
TypeScript

import { Page, Route } from "@playwright/test";
export interface RouteOptions {
/**
* Only intercept requests with this HTTP method (e.g. "POST", "PATCH").
* Requests with other methods continue normally.
* If omitted, all methods are intercepted.
*/
method?: string;
}
export interface PausedRequest {
/** Let the intercepted request proceed normally. */
release: () => void;
/** Fulfill the intercepted request with HTTP 500 instead of continuing it. */
fail: () => void;
intercepted: Promise<void>;
}
/**
* Arms a one-shot route intercept that fulfills the NEXT matching request
* with HTTP 500. Returns a promise that resolves once the 500 has been
* delivered, so callers can `await` it before asserting rollback state.
*
* Pass `{ method: "POST" }` to only intercept POST requests and let GETs
* through - important for the flat grid which fires background GETs.
*
* Awaiting the call guarantees the route is registered before the triggering
* action runs, so the request can't slip through unintercepted.
*
* Usage:
* const { failed } = await failNextRequest(page, `**\/rows\/table\/${id}\/`, { method: "POST" })
* await grid.addRow()
* await failed
* await grid.expectRowCount(1) // optimistic row was rolled back
*/
export async function failNextRequest(
page: Page,
urlPattern: string,
options: RouteOptions = {},
): Promise<{ failed: Promise<void> }> {
let markFailed!: () => void;
const failed = new Promise<void>((resolve) => {
markFailed = resolve;
});
const handler = async (route: Route) => {
if (options.method && route.request().method() !== options.method) {
// Wrong method - let it through and keep the handler registered
await route.continue();
return;
}
// Correct method (or no method filter) - fail it and unregister.
// Use a Baserow-structured body so hasBaserowAPIError() returns true and
// the standard error toast fires.
await route.fulfill({
status: 500,
contentType: "application/json",
body: JSON.stringify({
error: "ERROR_E2E_FORCED_FAILURE",
detail: "Forced e2e failure.",
}),
});
await page.unroute(urlPattern, handler);
markFailed();
};
await page.route(urlPattern, handler);
return { failed };
}
export async function pauseNextRequestWithSignal(
page: Page,
urlPattern: string,
options: RouteOptions = {},
): Promise<PausedRequest> {
let release!: () => void;
let fail!: () => void;
let releaseGate!: () => void;
let markIntercepted!: () => void;
let handler!: (route: Route) => Promise<void>;
let interceptedResolved = false;
let released = false;
let failMode = false;
let routed = true;
const gate = new Promise<void>((r) => {
releaseGate = r;
});
const intercepted = new Promise<void>((r) => {
markIntercepted = r;
});
const unrouteOnce = async () => {
if (!routed) {
return;
}
routed = false;
await page.unroute(urlPattern, handler);
};
release = () => {
if (released) {
return;
}
released = true;
failMode = false;
releaseGate();
};
fail = () => {
if (released) {
return;
}
released = true;
failMode = true;
releaseGate();
};
handler = async (route: Route) => {
if (options.method && route.request().method() !== options.method) {
await route.continue();
return;
}
if (interceptedResolved) {
await route.continue();
return;
}
interceptedResolved = true;
markIntercepted();
await gate;
if (failMode) {
await route.fulfill({
status: 500,
contentType: "application/json",
body: JSON.stringify({
error: "ERROR_E2E_FORCED_FAILURE",
detail: "Forced e2e failure.",
}),
});
} else {
await route.continue();
}
await unrouteOnce();
};
await page.route(urlPattern, handler);
return { release, fail, intercepted };
}
export type RowRequestMethod = "POST" | "PATCH" | "DELETE";
function rowsUrlPattern(tableId: number): string {
return `**/api/database/rows/table/${tableId}/**`;
}
/**
* The grid specs pause/fail the same rows batch endpoint repeatedly; these thin
* wrappers build that URL so call sites only pass the table id and method.
*/
export function pauseRows(
page: Page,
tableId: number,
method: RowRequestMethod,
): Promise<PausedRequest> {
return pauseNextRequestWithSignal(page, rowsUrlPattern(tableId), { method });
}
export function failRows(
page: Page,
tableId: number,
method: RowRequestMethod,
): Promise<{ failed: Promise<void> }> {
return failNextRequest(page, rowsUrlPattern(tableId), { method });
}
export interface HeldResponse {
/** Resolves once the server answered, so the change is committed. */
committed: Promise<void>;
/** Hands the held response to the page. */
release: () => void;
}
/** Lets the NEXT matching request reach the server but holds its response until released. */
export async function holdNextResponse(
page: Page,
urlPattern: string,
options: RouteOptions = {},
): Promise<HeldResponse> {
let release!: () => void;
let markCommitted!: () => void;
const gate = new Promise<void>((resolve) => {
release = resolve;
});
const committed = new Promise<void>((resolve) => {
markCommitted = resolve;
});
let taken = false;
const handler = async (route: Route) => {
if (
taken ||
(options.method && route.request().method() !== options.method)
) {
await route.continue();
return;
}
taken = true;
const response = await route.fetch();
markCommitted();
await gate;
await route.fulfill({ response });
await page.unroute(urlPattern, handler);
};
await page.route(urlPattern, handler);
return { committed, release };
}
export function holdRowsResponse(
page: Page,
tableId: number,
method: RowRequestMethod,
): Promise<HeldResponse> {
return holdNextResponse(page, rowsUrlPattern(tableId), { method });
}
/** Resolves when the server confirms the page's realtime subscription; call before navigating. */
export function realtimePageSubscribed(
page: Page,
pageType: string,
): Promise<void> {
return new Promise((resolve) => {
page.on("websocket", (socket) => {
socket.on("framereceived", ({ payload }) => {
if (typeof payload !== "string" || !payload.includes("page_add")) {
return;
}
const message = JSON.parse(payload);
if (message.type === "page_add" && message.page === pageType) {
resolve();
}
});
});
});
}