|
1 | | -import { describe, it, expect } from "vitest"; |
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
2 | 2 | import { TinybirdClient, createClient } from "./base.js"; |
3 | 3 | import type { DatasourcesNamespace } from "./types.js"; |
| 4 | +import { loadConfigAsync } from "../cli/config.js"; |
| 5 | +import { getOrCreateBranch } from "../api/branches.js"; |
| 6 | + |
| 7 | +vi.mock("../cli/config.js", () => ({ |
| 8 | + loadConfigAsync: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("../api/branches.js", () => ({ |
| 12 | + getOrCreateBranch: vi.fn(), |
| 13 | +})); |
4 | 14 |
|
5 | 15 | describe("TinybirdClient", () => { |
| 16 | + const originalEnv = { ...process.env }; |
| 17 | + const mockedLoadConfigAsync = vi.mocked(loadConfigAsync); |
| 18 | + const mockedGetOrCreateBranch = vi.mocked(getOrCreateBranch); |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + process.env = { ...originalEnv }; |
| 22 | + delete process.env.VERCEL_ENV; |
| 23 | + delete process.env.GITHUB_HEAD_REF; |
| 24 | + delete process.env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME; |
| 25 | + delete process.env.CI; |
| 26 | + delete process.env.TINYBIRD_PREVIEW_MODE; |
| 27 | + delete process.env.VERCEL_GIT_COMMIT_REF; |
| 28 | + delete process.env.GITHUB_REF_NAME; |
| 29 | + delete process.env.CI_COMMIT_BRANCH; |
| 30 | + delete process.env.CIRCLE_BRANCH; |
| 31 | + delete process.env.BUILD_SOURCEBRANCHNAME; |
| 32 | + delete process.env.BITBUCKET_BRANCH; |
| 33 | + delete process.env.TINYBIRD_BRANCH_NAME; |
| 34 | + delete process.env.TINYBIRD_BRANCH_TOKEN; |
| 35 | + mockedLoadConfigAsync.mockReset(); |
| 36 | + mockedGetOrCreateBranch.mockReset(); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + process.env = { ...originalEnv }; |
| 41 | + vi.restoreAllMocks(); |
| 42 | + }); |
| 43 | + |
6 | 44 | describe("constructor", () => { |
7 | 45 | it("throws error when baseUrl is missing", () => { |
8 | 46 | expect(() => new TinybirdClient({ baseUrl: "", token: "test-token" })).toThrow( |
@@ -115,6 +153,52 @@ describe("TinybirdClient", () => { |
115 | 153 | expect(context.baseUrl).toBe("https://api.us-east.tinybird.co"); |
116 | 154 | expect(context.token).toBe("us-token"); |
117 | 155 | }); |
| 156 | + |
| 157 | + it("passes custom fetch to devMode branch resolution", async () => { |
| 158 | + const customFetch = vi.fn(); |
| 159 | + |
| 160 | + mockedLoadConfigAsync.mockResolvedValue({ |
| 161 | + include: [], |
| 162 | + token: "workspace-token", |
| 163 | + baseUrl: "https://api.tinybird.co", |
| 164 | + configPath: "/tmp/tinybird.config.json", |
| 165 | + cwd: "/tmp", |
| 166 | + gitBranch: "feature/add-fetch", |
| 167 | + tinybirdBranch: "feature_add_fetch", |
| 168 | + isMainBranch: false, |
| 169 | + devMode: "branch", |
| 170 | + }); |
| 171 | + mockedGetOrCreateBranch.mockResolvedValue({ |
| 172 | + id: "branch-123", |
| 173 | + name: "feature_add_fetch", |
| 174 | + token: "branch-token", |
| 175 | + created_at: "2024-01-01T00:00:00Z", |
| 176 | + wasCreated: false, |
| 177 | + }); |
| 178 | + |
| 179 | + const client = new TinybirdClient({ |
| 180 | + baseUrl: "https://api.tinybird.co", |
| 181 | + token: "workspace-token", |
| 182 | + fetch: customFetch as typeof fetch, |
| 183 | + devMode: true, |
| 184 | + }); |
| 185 | + |
| 186 | + const context = await client.getContext(); |
| 187 | + |
| 188 | + expect(mockedLoadConfigAsync).toHaveBeenCalled(); |
| 189 | + expect(mockedGetOrCreateBranch).toHaveBeenCalledWith( |
| 190 | + { |
| 191 | + baseUrl: "https://api.tinybird.co", |
| 192 | + token: "workspace-token", |
| 193 | + fetch: customFetch, |
| 194 | + }, |
| 195 | + "feature_add_fetch" |
| 196 | + ); |
| 197 | + expect(context.token).toBe("branch-token"); |
| 198 | + expect(context.isBranchToken).toBe(true); |
| 199 | + expect(context.branchName).toBe("feature_add_fetch"); |
| 200 | + expect(context.gitBranch).toBe("feature/add-fetch"); |
| 201 | + }); |
118 | 202 | }); |
119 | 203 |
|
120 | 204 | describe("createClient", () => { |
|
0 commit comments