Files
paseo/packages/app/e2e/support/helpers/usage-reports.ts
T
Mohamed Boudra 1f5f5d384e Sidebar usage shows default percents with logos, and remembers the picked host (#5786)
* fix(app): show default usage windows when no windows are pinned

* fix(app): show only percentages in sidebar usage summaries

* fix(client): restore usage source logos for released hosts

* fix(app): restore the labeled footer line below sidebar items

* fix(app): use History's host filter to pick the usage and plugin host

The Usage screen, the compact usage sheet and plugin screens used a separate
HostSwitcher trigger. They now use HostFilter without "All hosts", as the
label manager does, and hide it with one host, as History does.

* fix(app): save the usage host pick and show it in the sidebar summary

Picking a host on the Usage screen or usage sheet was view state: it reset when
the view closed, and the sidebar summary never read it. The pick is now a
device-local usage preference. The summary and the screen show the picked host
while it is connected, else the active workspace's host, else the first host.

The usage fixture answers by request, since the summary and the screen each
load reports and only a card's Refresh forces one.

* test(app): expect plugin footer rows above the footer bottom line

Add project moved to the bottom line with the icons, so footer rows sit above
it, spanning Add project to Settings.

* test(app): expect the saved usage host in stored settings

* test(app): let usage settings specs share reports with the sidebar summary

The sidebar summary loads reports on app start, so the settings specs answer
by request instead of by order and read percents from the settings card.
2026-09-30 21:09:07 +01:00

195 lines
6.3 KiB
TypeScript

import type { Page } from "@playwright/test";
import type { UsageReportEntry } from "@getpaseo/protocol/messages";
import { daemonWsRoutePattern, wsRoutePatternForPort } from "./daemon-port";
export interface UsageListRequest {
forceRefresh: boolean;
reportIds?: string[];
}
export interface UsageReportsFixture {
listRequests(): UsageListRequest[];
waitForListRequests(count: number): Promise<void>;
}
interface UsageReportsFixtureOptions {
/**
* Successive `usage.list_reports` responses; the last one repeats. `{ error }` fails that
* request; a function builds the response from the request when it arrives (e.g. a fresh
* `fetchedAt`, or a different answer to a forced refresh).
*/
lists?: Array<UsageListResponse | ((request: UsageListRequest) => UsageListResponse)>;
/** False simulates a host with no usage reporting capability. */
usageSupported?: boolean;
/** Released hosts expose provider.usage.list with no source icons. */
providerUsageListOnly?: boolean;
/** The host daemon's port; defaults to the E2E daemon. */
port?: number;
}
type UsageListResponse = UsageReportEntry[] | { error: string };
type WebSocketMessage = string | Buffer;
function parseJson(message: WebSocketMessage): unknown {
const raw = typeof message === "string" ? message : message.toString("utf8");
try {
return JSON.parse(raw);
} catch {
return null;
}
}
function getSessionMessage(message: WebSocketMessage): Record<string, unknown> | null {
const envelope = parseJson(message) as { type?: unknown; message?: unknown } | null;
if (!envelope || envelope.type !== "session" || typeof envelope.message !== "object") {
return null;
}
return envelope.message as Record<string, unknown>;
}
function withUsageSupportFeature(
message: WebSocketMessage,
enabled: boolean,
providerUsageListOnly: boolean,
): string | null {
const envelope = parseJson(message) as {
type?: unknown;
message?: { type?: unknown; payload?: Record<string, unknown> };
} | null;
const payload = envelope?.message?.payload;
if (
envelope?.type !== "session" ||
envelope.message?.type !== "status" ||
payload?.status !== "server_info"
) {
return null;
}
const features =
typeof payload.features === "object" && payload.features !== null ? payload.features : {};
return JSON.stringify({
...envelope,
message: {
...envelope.message,
payload: {
...payload,
features: {
...features,
usageSources: enabled && !providerUsageListOnly,
providerUsageList: enabled,
},
},
},
});
}
function pick<T>(values: T[], index: number): T {
const value = values[Math.min(index, values.length - 1)];
if (value === undefined) throw new Error("Usage fixture requires at least one response.");
return value;
}
function createCounter() {
let count = 0;
const waiters: Array<{ count: number; resolve: () => void }> = [];
return {
increment() {
count += 1;
for (const waiter of waiters.splice(0)) {
if (count >= waiter.count) waiter.resolve();
else waiters.push(waiter);
}
},
waitFor(target: number): Promise<void> {
if (count >= target) return Promise.resolve();
return new Promise((resolve) => waiters.push({ count: target, resolve }));
},
};
}
export async function installUsageReportsFixture(
page: Page,
options: UsageReportsFixtureOptions,
): Promise<UsageReportsFixture> {
const listRequests: UsageListRequest[] = [];
const listCounter = createCounter();
const usageSupported = options.usageSupported ?? true;
const providerUsageListOnly = options.providerUsageListOnly ?? false;
const requestType = providerUsageListOnly
? "provider.usage.list.request"
: "usage.list_reports.request";
const route =
options.port === undefined ? daemonWsRoutePattern() : wsRoutePatternForPort(`${options.port}`);
await page.routeWebSocket(route, (ws) => {
const server = ws.connectToServer();
ws.onMessage((message) => {
const request = getSessionMessage(message);
const requestId = request?.requestId;
if (request?.type === requestType && typeof requestId === "string") {
const listRequest: UsageListRequest = {
forceRefresh: request.forceRefresh === true,
reportIds: Array.isArray(request.reportIds) ? (request.reportIds as string[]) : undefined,
};
listRequests.push(listRequest);
const scripted = pick(options.lists ?? [[]], listRequests.length - 1);
const response = typeof scripted === "function" ? scripted(listRequest) : scripted;
if ("error" in response) {
ws.send(
JSON.stringify({
type: "session",
message: {
type: "rpc_error",
payload: {
requestId,
requestType,
error: response.error,
code: "transport",
},
},
}),
);
listCounter.increment();
return;
}
const ids = Array.isArray(request.reportIds) ? request.reportIds : null;
const reports = ids ? response.filter((entry) => ids.includes(entry.id)) : response;
const reply = providerUsageListOnly
? {
type: "provider.usage.list.response",
payload: {
requestId,
fetchedAt: new Date().toISOString(),
providers: reports.map((entry) => ({
providerId: entry.sourceId,
displayName: entry.sourceLabel,
fetchedAt: entry.fetchedAt,
...entry.report,
planLabel: entry.report.planLabel ?? null,
})),
},
}
: { type: "usage.list_reports.response", payload: { requestId, reports } };
ws.send(JSON.stringify({ type: "session", message: reply }));
listCounter.increment();
return;
}
server.send(message);
});
server.onMessage((message) => {
const serverInfo =
typeof message === "string"
? withUsageSupportFeature(message, usageSupported, providerUsageListOnly)
: null;
ws.send(serverInfo ?? message);
});
});
return {
listRequests: () => [...listRequests],
waitForListRequests: (count) => listCounter.waitFor(count),
};
}