|
| 1 | +import type { ServerProviderStatus } from "@okcode/contracts"; |
| 2 | +import type { ComponentProps, ReactElement } from "react"; |
| 3 | +import { renderToStaticMarkup } from "react-dom/server"; |
| 4 | +import { act, create, type ReactTestRenderer } from "react-test-renderer"; |
| 5 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +import { ErrorNotificationBar } from "./ErrorNotificationBar"; |
| 8 | + |
| 9 | +function makeProviderStatus(overrides: Partial<ServerProviderStatus> = {}): ServerProviderStatus { |
| 10 | + return { |
| 11 | + provider: "codex", |
| 12 | + status: "warning", |
| 13 | + available: true, |
| 14 | + authStatus: "authenticated", |
| 15 | + checkedAt: "2026-04-10T12:00:00.000Z", |
| 16 | + message: "Provider is checking state.", |
| 17 | + ...overrides, |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +const THREAD_ERROR = |
| 22 | + "Git command failed in GitCore.createWorktree: OPENAI_API_KEY=sk-proj-secret (/repo) - Base branch 'main' does not resolve to a commit yet."; |
| 23 | + |
| 24 | +function renderBar( |
| 25 | + overrides: Partial<ComponentProps<typeof ErrorNotificationBar>> = {}, |
| 26 | +): ReactElement { |
| 27 | + const { onDismissThreadError, transportState, ...restOverrides } = overrides; |
| 28 | + return ( |
| 29 | + <ErrorNotificationBar |
| 30 | + threadError={THREAD_ERROR} |
| 31 | + showAuthFailuresAsErrors |
| 32 | + showNotificationDetails={false} |
| 33 | + includeDiagnosticsTipsInCopy={true} |
| 34 | + providerStatus={makeProviderStatus()} |
| 35 | + isMobileCompanion={false} |
| 36 | + {...restOverrides} |
| 37 | + {...(onDismissThreadError ? { onDismissThreadError } : {})} |
| 38 | + {...(transportState ? { transportState } : {})} |
| 39 | + /> |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +afterEach(() => { |
| 44 | + vi.restoreAllMocks(); |
| 45 | +}); |
| 46 | + |
| 47 | +describe("ErrorNotificationBar", () => { |
| 48 | + it("keeps raw error text out of the collapsed bar and shows the aggregate count", () => { |
| 49 | + const markup = renderToStaticMarkup(renderBar()); |
| 50 | + |
| 51 | + expect(markup).toContain("Show 2 notifications"); |
| 52 | + expect(markup).not.toContain("OPENAI_API_KEY=sk-proj-secret"); |
| 53 | + expect(markup).not.toContain("Base branch 'main' does not resolve to a commit yet."); |
| 54 | + }); |
| 55 | + |
| 56 | + it("expands to show redacted error text and diagnostics copy", async () => { |
| 57 | + let renderer: ReactTestRenderer | null = null; |
| 58 | + await act(async () => { |
| 59 | + renderer = create(renderBar()); |
| 60 | + }); |
| 61 | + |
| 62 | + const root = renderer!.root; |
| 63 | + const toggle = root.findByProps({ "aria-label": "Show 2 notifications" }); |
| 64 | + |
| 65 | + await act(async () => { |
| 66 | + toggle.props.onClick(); |
| 67 | + }); |
| 68 | + |
| 69 | + expect(root.findByProps({ "aria-label": "Hide 2 notifications" })).toBeTruthy(); |
| 70 | + expect(root.findByProps({ "aria-label": "Copy diagnostics" })).toBeTruthy(); |
| 71 | + expect(JSON.stringify(renderer!.toJSON())).toContain("Worktree thread could not start"); |
| 72 | + expect(JSON.stringify(renderer!.toJSON())).toContain( |
| 73 | + "Base branch 'main' does not resolve to a commit yet.", |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it("starts expanded when notification details are enabled", () => { |
| 78 | + const markup = renderToStaticMarkup( |
| 79 | + renderBar({ |
| 80 | + showNotificationDetails: true, |
| 81 | + }), |
| 82 | + ); |
| 83 | + |
| 84 | + expect(markup).toContain("Hide 2 notifications"); |
| 85 | + expect(markup).toContain("Worktree thread could not start"); |
| 86 | + expect(markup).toContain("Base branch 'main' does not resolve to a commit yet."); |
| 87 | + }); |
| 88 | +}); |
0 commit comments