|
| 1 | +import { describe, expect, it } from "@effect/vitest" |
| 2 | +import { Effect } from "effect" |
| 3 | +import { vi } from "vitest" |
| 4 | + |
| 5 | +import { githubLoginStreamMarkers } from "../../src/shared/auth-stream-markers.js" |
| 6 | +import { runGithubOauthMutation } from "../../src/web/actions-github-oauth.js" |
| 7 | +import type { BrowserActionContext } from "../../src/web/actions-shared.js" |
| 8 | +import type { AuthSnapshot, GithubAuthStatus } from "../../src/web/api.js" |
| 9 | + |
| 10 | +const loginGithubStreamMock = vi.hoisted(() => vi.fn()) |
| 11 | +const loadAuthSnapshotMock = vi.hoisted(() => vi.fn()) |
| 12 | +const loadGithubStatusMock = vi.hoisted(() => vi.fn()) |
| 13 | + |
| 14 | +vi.mock("../../src/web/api.js", () => ({ |
| 15 | + loadAuthSnapshot: loadAuthSnapshotMock, |
| 16 | + loadGithubStatus: loadGithubStatusMock, |
| 17 | + loginGithubStream: loginGithubStreamMock |
| 18 | +})) |
| 19 | + |
| 20 | +const githubStatus: GithubAuthStatus = { |
| 21 | + summary: "GitHub tokens (1):", |
| 22 | + tokens: [ |
| 23 | + { |
| 24 | + key: "GITHUB_TOKEN", |
| 25 | + label: "default", |
| 26 | + login: "octocat", |
| 27 | + status: "valid" |
| 28 | + } |
| 29 | + ] |
| 30 | +} |
| 31 | + |
| 32 | +const authSnapshot: AuthSnapshot = { |
| 33 | + claudeAuthEntries: 0, |
| 34 | + claudeAuthPath: "/home/dev/.docker-git/.orch/auth/claude", |
| 35 | + geminiAuthEntries: 0, |
| 36 | + geminiAuthPath: "/home/dev/.docker-git/.orch/auth/gemini", |
| 37 | + gitTokenEntries: 0, |
| 38 | + gitUserEntries: 0, |
| 39 | + githubTokenEntries: 1, |
| 40 | + globalEnvPath: "/home/dev/.docker-git/.orch/env/global.env", |
| 41 | + totalEntries: 1 |
| 42 | +} |
| 43 | + |
| 44 | +const makeContext = () => { |
| 45 | + let output = "" |
| 46 | + const setOutput: BrowserActionContext["setOutput"] = (next) => { |
| 47 | + output = typeof next === "function" ? next(output) : next |
| 48 | + } |
| 49 | + const setMessage: BrowserActionContext["setMessage"] = vi.fn() |
| 50 | + const reloadDashboard = vi.fn() |
| 51 | + |
| 52 | + return { |
| 53 | + context: { |
| 54 | + githubStatus: null, |
| 55 | + reloadDashboard, |
| 56 | + selectedProjectId: null, |
| 57 | + selectedProjectName: null, |
| 58 | + setActionPrompt: vi.fn(), |
| 59 | + setAuthSnapshot: vi.fn(), |
| 60 | + setBusyLabel: vi.fn(), |
| 61 | + setGithubStatus: vi.fn(), |
| 62 | + setMessage, |
| 63 | + setOutput, |
| 64 | + setProjectAuthSnapshot: vi.fn(), |
| 65 | + setSelectedMenuIndex: vi.fn(), |
| 66 | + setSelectedProject: vi.fn(), |
| 67 | + setSelectedProjectId: vi.fn(), |
| 68 | + setTerminalSession: vi.fn() |
| 69 | + } satisfies BrowserActionContext, |
| 70 | + output: () => output, |
| 71 | + reloadDashboard, |
| 72 | + setMessage |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +describe("web GitHub OAuth action", () => { |
| 77 | + it.effect("refreshes dashboard projects after successful OAuth", () => |
| 78 | + Effect.gen(function*(_) { |
| 79 | + loginGithubStreamMock.mockImplementation((_label: string | null, onChunk: (chunk: string) => void) => |
| 80 | + Effect.sync(() => { |
| 81 | + onChunk("Copy your one-time code: ABCD-1234\n") |
| 82 | + onChunk("State dir ready: /home/dev/.docker-git\n") |
| 83 | + onChunk(`${githubLoginStreamMarkers.success}\n`) |
| 84 | + return [ |
| 85 | + "Copy your one-time code: ABCD-1234", |
| 86 | + "State dir ready: /home/dev/.docker-git", |
| 87 | + githubLoginStreamMarkers.success |
| 88 | + ].join("\n") |
| 89 | + }) |
| 90 | + ) |
| 91 | + loadAuthSnapshotMock.mockImplementation(() => Effect.succeed(authSnapshot)) |
| 92 | + loadGithubStatusMock.mockImplementation(() => Effect.succeed(githubStatus)) |
| 93 | + |
| 94 | + const { context, output, reloadDashboard, setMessage } = makeContext() |
| 95 | + |
| 96 | + runGithubOauthMutation({ label: "" }, context) |
| 97 | + |
| 98 | + yield* _( |
| 99 | + Effect.tryPromise({ |
| 100 | + catch: (error) => error, |
| 101 | + try: () => |
| 102 | + vi.waitFor(() => { |
| 103 | + expect(reloadDashboard).toHaveBeenCalledTimes(1) |
| 104 | + }) |
| 105 | + }) |
| 106 | + ) |
| 107 | + |
| 108 | + expect(output()).toBe("Copy your one-time code: ABCD-1234\nState dir ready: /home/dev/.docker-git\n") |
| 109 | + expect(context.setActionPrompt).toHaveBeenCalledWith(null) |
| 110 | + expect(context.setAuthSnapshot).toHaveBeenCalledWith(authSnapshot) |
| 111 | + expect(context.setGithubStatus).toHaveBeenCalledWith(githubStatus) |
| 112 | + expect(setMessage).toHaveBeenLastCalledWith("Saved GitHub token (default).") |
| 113 | + })) |
| 114 | +}) |
0 commit comments