|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + clearRuntimeConfigSnapshot, |
| 4 | + setRuntimeConfigSnapshot, |
| 5 | +} from "../config/runtime-snapshot.js"; |
| 6 | +import { |
| 7 | + setActiveDegradedSecretOwners, |
| 8 | + SecretSurfaceUnavailableError, |
| 9 | +} from "../secrets/runtime-degraded-state.js"; |
2 | 10 | import { ControlUiGitHubError } from "./control-ui-github-api.js"; |
3 | 11 | import { |
4 | 12 | loadControlUiGitHubPreview, |
@@ -113,11 +121,15 @@ describe("parseControlUiGitHubPreviewTarget", () => { |
113 | 121 |
|
114 | 122 | describe("loadControlUiGitHubPreview", () => { |
115 | 123 | beforeEach(() => { |
| 124 | + clearRuntimeConfigSnapshot(); |
| 125 | + setActiveDegradedSecretOwners([]); |
116 | 126 | vi.stubEnv("GH_TOKEN", ""); |
117 | 127 | vi.stubEnv("GITHUB_TOKEN", ""); |
118 | 128 | }); |
119 | 129 |
|
120 | 130 | afterEach(() => { |
| 131 | + clearRuntimeConfigSnapshot(); |
| 132 | + setActiveDegradedSecretOwners([]); |
121 | 133 | vi.unstubAllEnvs(); |
122 | 134 | }); |
123 | 135 |
|
@@ -162,6 +174,75 @@ describe("loadControlUiGitHubPreview", () => { |
162 | 174 | expect(avatarRequest instanceof URL ? avatarRequest.searchParams.get("s") : null).toBe("64"); |
163 | 175 | }); |
164 | 176 |
|
| 177 | + it("does not reuse cached previews after the GitHub credential scope changes", async () => { |
| 178 | + const fetchMock = vi.fn<typeof fetch>().mockImplementation(async (input, init) => { |
| 179 | + const url = requestUrl(input); |
| 180 | + if (!url.includes("/issues/")) { |
| 181 | + return githubJson({ private: false }); |
| 182 | + } |
| 183 | + const authorization = new Headers(init?.headers).get("Authorization"); |
| 184 | + return githubJson( |
| 185 | + previewPayload({ |
| 186 | + user: { |
| 187 | + login: authorization === "Bearer preview-token-a" ? "token-a" : "token-b", |
| 188 | + }, |
| 189 | + }), |
| 190 | + ); |
| 191 | + }); |
| 192 | + const target = { |
| 193 | + kind: "issue" as const, |
| 194 | + number: 70013, |
| 195 | + owner: "openclaw", |
| 196 | + repo: "credential-scope", |
| 197 | + }; |
| 198 | + vi.stubEnv("GH_TOKEN", "preview-token-a"); |
| 199 | + |
| 200 | + const first = await loadControlUiGitHubPreview(target, fetchMock); |
| 201 | + vi.stubEnv("GH_TOKEN", "preview-token-b"); |
| 202 | + const second = await loadControlUiGitHubPreview(target, fetchMock); |
| 203 | + |
| 204 | + expect(first.login).toBe("token-a"); |
| 205 | + expect(second.login).toBe("token-b"); |
| 206 | + expect(fetchMock).toHaveBeenCalledTimes(6); |
| 207 | + }); |
| 208 | + |
| 209 | + it("revalidates configured credential availability before serving a cached preview", async () => { |
| 210 | + setRuntimeConfigSnapshot({ |
| 211 | + gateway: { controlUi: { github: { token: "configured-preview-token" } } }, |
| 212 | + }); |
| 213 | + const fetchMock = vi |
| 214 | + .fn<typeof fetch>() |
| 215 | + .mockImplementation(async (input) => |
| 216 | + requestUrl(input).includes("/issues/") |
| 217 | + ? githubJson(previewPayload({ user: { login: "cached-preview" } })) |
| 218 | + : githubJson({ private: false }), |
| 219 | + ); |
| 220 | + const target = { |
| 221 | + kind: "issue" as const, |
| 222 | + number: 70014, |
| 223 | + owner: "openclaw", |
| 224 | + repo: "configured-degraded", |
| 225 | + }; |
| 226 | + |
| 227 | + await loadControlUiGitHubPreview(target, fetchMock); |
| 228 | + setActiveDegradedSecretOwners([ |
| 229 | + { |
| 230 | + ownerKind: "capability", |
| 231 | + ownerId: "control-ui-github", |
| 232 | + state: "unavailable", |
| 233 | + degradationState: "cold", |
| 234 | + paths: ["gateway.controlUi.github.token"], |
| 235 | + refKeys: ["store:default:PREVIEW_TOKEN"], |
| 236 | + reason: "secret reference was not found", |
| 237 | + }, |
| 238 | + ]); |
| 239 | + |
| 240 | + expect(() => loadControlUiGitHubPreview(target, fetchMock)).toThrow( |
| 241 | + SecretSurfaceUnavailableError, |
| 242 | + ); |
| 243 | + expect(fetchMock).toHaveBeenCalledTimes(3); |
| 244 | + }); |
| 245 | + |
165 | 246 | it.each([ |
166 | 247 | { avatarUrl: "https://example.com/avatar.png", number: 70001, repo: "avatar-host" }, |
167 | 248 | { |
|
0 commit comments