-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathworkspaces.test.ts
More file actions
165 lines (148 loc) · 4.88 KB
/
Copy pathworkspaces.test.ts
File metadata and controls
165 lines (148 loc) · 4.88 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { QueryClient } from "react-query";
import { describe, expect, it } from "vitest";
import type { WorkspacesResponse } from "#/api/typesGenerated";
import { getWorkspaceQuotaQueryKey } from "./workspaceQuota";
import {
autoCreateWorkspace,
buildLogsKey,
createWorkspace,
invalidateWorkspaceListQueries,
invalidateWorkspaceMutationQueries,
workspacesKey,
workspacesQueryKeyPrefix,
workspaceUsage,
} from "./workspaces";
const createTestQueryClient = (): QueryClient =>
new QueryClient({
defaultOptions: {
queries: {
retry: false,
gcTime: Number.POSITIVE_INFINITY,
refetchOnWindowFocus: false,
networkMode: "offlineFirst",
},
},
});
const workspacesResponse = {
workspaces: [],
count: 0,
} satisfies WorkspacesResponse;
const seedWorkspaceFamilyQueries = (queryClient: QueryClient) => {
const rawListKey = workspacesQueryKeyPrefix;
const defaultListKey = workspacesKey({});
const filteredListKey = workspacesKey({
q: "owner:me organization:default",
limit: 25,
offset: 50,
});
const usageKey = workspaceUsage({
usageApp: "reconnecting-pty",
connectionStatus: "connected",
workspaceId: "workspace-1",
agentId: "agent-1",
}).queryKey;
const buildLogs = buildLogsKey("workspace-1");
const workspacePermissionsKey = [
"workspaces",
"workspace-1",
"permissions",
] as const;
const workspaceAgentCredentialsKey = [
"workspaces",
"workspace-1",
"agents",
"main",
"credentials",
] as const;
const organizationWorkspacePermissionsKey = [
"workspaces",
["organization-1"],
"permissions",
] as const;
queryClient.setQueryData(rawListKey, workspacesResponse);
queryClient.setQueryData(defaultListKey, workspacesResponse);
queryClient.setQueryData(filteredListKey, workspacesResponse);
queryClient.setQueryData(usageKey, { tracked: true });
queryClient.setQueryData(buildLogs, []);
queryClient.setQueryData(workspacePermissionsKey, { read: true });
queryClient.setQueryData(workspaceAgentCredentialsKey, { token: "secret" });
queryClient.setQueryData(organizationWorkspacePermissionsKey, { read: true });
return {
listKeys: [rawListKey, defaultListKey, filteredListKey],
nonListKeys: [
usageKey,
buildLogs,
workspacePermissionsKey,
workspaceAgentCredentialsKey,
organizationWorkspacePermissionsKey,
],
};
};
describe("invalidateWorkspaceListQueries", () => {
it("invalidates workspace list queries without touching side-effecting workspace-family queries", async () => {
const queryClient = createTestQueryClient();
const { listKeys, nonListKeys } = seedWorkspaceFamilyQueries(queryClient);
await invalidateWorkspaceListQueries(queryClient);
for (const key of listKeys) {
expect(
queryClient.getQueryState(key)?.isInvalidated,
`${JSON.stringify(key)} should be invalidated`,
).toBe(true);
}
for (const key of nonListKeys) {
expect(
queryClient.getQueryState(key)?.isInvalidated,
`${JSON.stringify(key)} should NOT be invalidated`,
).not.toBe(true);
}
});
});
describe("invalidateWorkspaceMutationQueries", () => {
it("uses narrowed list invalidation and keeps workspace usage queries untouched", async () => {
const queryClient = createTestQueryClient();
const { listKeys, nonListKeys } = seedWorkspaceFamilyQueries(queryClient);
const quotaKey = getWorkspaceQuotaQueryKey("default", "me");
queryClient.setQueryData(quotaKey, { credits_consumed: 1, budget: 10 });
await invalidateWorkspaceMutationQueries(queryClient, {
organizationName: "default",
username: "me",
});
for (const key of listKeys) {
expect(
queryClient.getQueryState(key)?.isInvalidated,
`${JSON.stringify(key)} should be invalidated`,
).toBe(true);
}
expect(queryClient.getQueryState(quotaKey)?.isInvalidated).toBe(true);
for (const key of nonListKeys) {
expect(
queryClient.getQueryState(key)?.isInvalidated,
`${JSON.stringify(key)} should NOT be invalidated`,
).not.toBe(true);
}
});
});
describe("workspace creation mutations", () => {
it("use narrowed list invalidation for manual workspace creation", async () => {
const queryClient = createTestQueryClient();
const { listKeys, nonListKeys } = seedWorkspaceFamilyQueries(queryClient);
await createWorkspace(queryClient).onSuccess();
for (const key of listKeys) {
expect(queryClient.getQueryState(key)?.isInvalidated).toBe(true);
}
for (const key of nonListKeys) {
expect(queryClient.getQueryState(key)?.isInvalidated).not.toBe(true);
}
});
it("use narrowed list invalidation for auto workspace creation", async () => {
const queryClient = createTestQueryClient();
const { listKeys, nonListKeys } = seedWorkspaceFamilyQueries(queryClient);
await autoCreateWorkspace(queryClient).onSuccess();
for (const key of listKeys) {
expect(queryClient.getQueryState(key)?.isInvalidated).toBe(true);
}
for (const key of nonListKeys) {
expect(queryClient.getQueryState(key)?.isInvalidated).not.toBe(true);
}
});
});