forked from paperclipai/paperclip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-auth-recheck.test.ts
More file actions
120 lines (103 loc) · 3.27 KB
/
Copy pathquery-auth-recheck.test.ts
File metadata and controls
120 lines (103 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { QueryClient } from "@tanstack/react-query";
import { describe, expect, it } from "vitest";
import { ApiError } from "../api/client";
import { queryKeys } from "./queryKeys";
import { installAuthFailureRecheck } from "./query-auth-recheck";
function newClient(): QueryClient {
return new QueryClient({ defaultOptions: { queries: { retry: false, staleTime: 30_000 } } });
}
/** Let the cache subscriber and any refetch it triggers settle. */
async function settle(): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, 0));
await new Promise((resolve) => setTimeout(resolve, 0));
}
describe("installAuthFailureRecheck", () => {
it("re-checks the session when any other query fails with 401", async () => {
const client = newClient();
installAuthFailureRecheck(client);
let sessionFetches = 0;
await client.fetchQuery({
queryKey: queryKeys.auth.session,
queryFn: async () => {
sessionFetches += 1;
return { user: { id: "u1" } };
},
});
expect(sessionFetches).toBe(1);
await client
.fetchQuery({
queryKey: ["companies", "list"],
queryFn: async () => {
throw new ApiError("unauthorized", 401, null);
},
})
.catch(() => undefined);
await settle();
expect(sessionFetches).toBe(2);
});
it("does not re-check the session for a 500 — that is not a session problem", async () => {
const client = newClient();
installAuthFailureRecheck(client);
let sessionFetches = 0;
await client.fetchQuery({
queryKey: queryKeys.auth.session,
queryFn: async () => {
sessionFetches += 1;
return { user: { id: "u1" } };
},
});
await client
.fetchQuery({
queryKey: ["companies", "list"],
queryFn: async () => {
throw new ApiError("server error", 500, null);
},
})
.catch(() => undefined);
await settle();
expect(sessionFetches).toBe(1);
});
it("does not re-check the session in response to the session query's own failure", async () => {
const client = newClient();
installAuthFailureRecheck(client);
let sessionFetches = 0;
await client
.fetchQuery({
queryKey: queryKeys.auth.session,
queryFn: async () => {
sessionFetches += 1;
throw new ApiError("unauthorized", 401, null);
},
})
.catch(() => undefined);
await settle();
expect(sessionFetches).toBe(1);
});
it("collapses a burst of 401s from many queries into a single session re-check", async () => {
const client = newClient();
installAuthFailureRecheck(client);
let sessionFetches = 0;
await client.fetchQuery({
queryKey: queryKeys.auth.session,
queryFn: async () => {
sessionFetches += 1;
return { user: { id: "u1" } };
},
});
expect(sessionFetches).toBe(1);
await Promise.all(
["issues", "agents", "labels", "routines", "skills"].map((name) =>
client
.fetchQuery({
queryKey: [name, "list"],
queryFn: async () => {
throw new ApiError("unauthorized", 401, null);
},
})
.catch(() => undefined),
),
);
await settle();
expect(sessionFetches).toBe(2);
});
});