mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
test(sharing): break every control, then keep the ones that noticed
Each behaviour here was verified by removing the code that provides it and confirming the test went red, then putting it back. Two of them did not, and both were rewritten rather than kept: The restore-side persister check asserted that the delegate's own dashboard could not be read back inside a shared record. Deleting the guard left it green, because the record-scoped hash already makes a snapshot written in one record unreadable in another. It now counts what landed in the cache, so the guard it names is the thing it measures — and the weaker line is kept beside it with a note saying why. The switcher's account rows were asserted from server-rendered markup. Radix portals do not render server-side at all, so an earlier draft was measuring the renderer. Those rows moved to the browser suite; what stays here is the gate, which fails sharply — with the `canSwitch` bail removed the two cases do not return different markup, they throw. Two more shapes worth naming. The cache dimension is exercised through a real QueryClient built from the app's own defaults, because "the hash string changed" and "one record's data cannot be read while inside another" are different claims. And the invalidation is a plain function over a client with three live entries seeded, with a test that asserts they start valid — without it, a client that failed to seed and one where everything was invalidated look the same from every other assertion.
This commit is contained in:
1 parent
5199de78af
commit
04d30c2570
11 files changed
+1204
-28
No files matched your search
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* Leaving a record the server has stopped honouring.
|
||||
*
|
||||
* Revocation clears the delegate's acting sessions in the same transaction, so
|
||||
* that path puts the browser back by itself. Expiry does not — nothing sweeps,
|
||||
* the grant simply stops being live on the next request, and the session keeps
|
||||
* its stamp. From then on every read is a correct 403 under a banner that says
|
||||
* the person is somewhere they are not.
|
||||
*
|
||||
* The subscriber is what closes that. It runs against a real `QueryClient`
|
||||
* here, driven into the error state the way a failing read drives it, because
|
||||
* "the effect calls subscribe" is a claim about source and "the callback fires
|
||||
* when a read is refused" is the behaviour somebody depends on.
|
||||
*
|
||||
* Mutation check, run: widen the guard to fire on any error (drop the
|
||||
* `isSharingAccessDenied` test) → "ignores an ordinary failure" goes red.
|
||||
*/
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { QueryClient } from "@tanstack/react-query";
|
||||
|
||||
import {
|
||||
isSharingAccessDenied,
|
||||
isSharingNotPermitted,
|
||||
subscribeToGrantLoss,
|
||||
} from "@/hooks/use-account-switch";
|
||||
import { ApiError } from "@/lib/api/api-fetch";
|
||||
import { QUERY_CLIENT_DEFAULT_OPTIONS } from "@/lib/pwa/query-client-options";
|
||||
|
||||
function client(): QueryClient {
|
||||
return new QueryClient({ defaultOptions: QUERY_CLIENT_DEFAULT_OPTIONS });
|
||||
}
|
||||
|
||||
/** Drive one query to the error state, the way a refused read does. */
|
||||
async function failWith(qc: QueryClient, error: unknown): Promise<void> {
|
||||
await qc
|
||||
.fetchQuery({
|
||||
queryKey: ["probe", Math.random()],
|
||||
queryFn: () => Promise.reject(error),
|
||||
retry: false,
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
const DENIED = new ApiError("Account access denied", 403, {
|
||||
errorCode: "sharing.access.denied",
|
||||
});
|
||||
const NOT_PERMITTED = new ApiError("Not permitted", 403, {
|
||||
errorCode: "sharing.not_permitted",
|
||||
});
|
||||
|
||||
describe("the two sharing error codes", () => {
|
||||
it("tells the two apart", () => {
|
||||
// Different facts, different responses. One means the caller may no longer
|
||||
// act as that account and must leave; the other means the surface is not
|
||||
// part of what sharing covers and leaving would be the wrong answer.
|
||||
expect(isSharingAccessDenied(DENIED)).toBe(true);
|
||||
expect(isSharingNotPermitted(DENIED)).toBe(false);
|
||||
expect(isSharingNotPermitted(NOT_PERMITTED)).toBe(true);
|
||||
expect(isSharingAccessDenied(NOT_PERMITTED)).toBe(false);
|
||||
});
|
||||
|
||||
it("reads the code, not the status or the prose", () => {
|
||||
expect(isSharingAccessDenied(new ApiError("Forbidden", 403))).toBe(false);
|
||||
expect(isSharingAccessDenied(new Error("Account access denied"))).toBe(
|
||||
false,
|
||||
);
|
||||
expect(isSharingAccessDenied(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("subscribeToGrantLoss", () => {
|
||||
it("leaves when a read is refused for a grant that has ended", async () => {
|
||||
const qc = client();
|
||||
const onLoss = vi.fn();
|
||||
const stop = subscribeToGrantLoss(qc, onLoss);
|
||||
|
||||
await failWith(qc, DENIED);
|
||||
stop();
|
||||
|
||||
expect(onLoss).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("ignores an ordinary failure", async () => {
|
||||
const qc = client();
|
||||
const onLoss = vi.fn();
|
||||
const stop = subscribeToGrantLoss(qc, onLoss);
|
||||
|
||||
// A 500, a network blip and a not-permitted surface are all reasons to
|
||||
// stay put. Reloading the app out of the record on any of them would turn
|
||||
// a transient error into a lost context.
|
||||
await failWith(qc, new ApiError("Server error", 500));
|
||||
await failWith(qc, NOT_PERMITTED);
|
||||
await failWith(qc, new TypeError("fetch failed"));
|
||||
stop();
|
||||
|
||||
expect(onLoss).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("fires once, however many reads are refused", async () => {
|
||||
// A switched page has several reads in flight and they all fail together.
|
||||
// The callback replaces the document; a second one would queue a second
|
||||
// navigation behind it.
|
||||
const qc = client();
|
||||
const onLoss = vi.fn();
|
||||
const stop = subscribeToGrantLoss(qc, onLoss);
|
||||
|
||||
await failWith(qc, DENIED);
|
||||
await failWith(qc, DENIED);
|
||||
await failWith(qc, DENIED);
|
||||
stop();
|
||||
|
||||
expect(onLoss).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("stops listening once unsubscribed", async () => {
|
||||
const qc = client();
|
||||
const onLoss = vi.fn();
|
||||
subscribeToGrantLoss(qc, onLoss)();
|
||||
|
||||
await failWith(qc, DENIED);
|
||||
|
||||
expect(onLoss).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -26,7 +26,11 @@
|
||||
* Callers therefore do not need a success state, only a pending one.
|
||||
*/
|
||||
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import {
|
||||
useMutation,
|
||||
useQueryClient,
|
||||
type QueryClient,
|
||||
} from "@tanstack/react-query";
|
||||
import { useCallback } from "react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
@@ -154,6 +158,32 @@ export function useAccountSwitch() {
|
||||
* down) the reload still happens: the stamp survives, but so does the honest
|
||||
* refusal, and the alternative is a page that neither recovers nor says why.
|
||||
*/
|
||||
/**
|
||||
* Watch the cache for the moment the server stops honouring the switch.
|
||||
*
|
||||
* A plain function over a QueryClient rather than an effect body, so a test
|
||||
* can drive a real cache into the error state and check what happens — which
|
||||
* is the only way to tell a subscriber that fires from one that was written
|
||||
* and never wired.
|
||||
*
|
||||
* Fires `onLoss` at most once. The caller reloads the document, and a second
|
||||
* trigger would queue a second navigation behind one already in flight.
|
||||
*/
|
||||
export function subscribeToGrantLoss(
|
||||
queryClient: QueryClient,
|
||||
onLoss: () => void,
|
||||
): () => void {
|
||||
let fired = false;
|
||||
return queryClient.getQueryCache().subscribe((event) => {
|
||||
if (fired) return;
|
||||
if (event.type !== "updated") return;
|
||||
if (event.query.state.status !== "error") return;
|
||||
if (!isSharingAccessDenied(event.query.state.error)) return;
|
||||
fired = true;
|
||||
onLoss();
|
||||
});
|
||||
}
|
||||
|
||||
export function useLeaveSharedRecordOnGrantLoss() {
|
||||
return useCallback(async () => {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user