diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 77bd964..31cc4ce 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,16 +37,22 @@ jobs: runs-on: ubuntu-latest needs: test timeout-minutes: 30 + # Secrets live in the e2e-live environment, scoped to a dedicated CI workspace. + environment: e2e-live + # All runs share one workspace; serialize so production deploys never overlap. + concurrency: + group: sdk-e2e-live + cancel-in-progress: false env: TINYBIRD_E2E_BASE_URL: ${{ vars.TINYBIRD_E2E_BASE_URL || 'https://api.tinybird.co' }} - TINYBIRD_E2E_USER_TOKEN: ${{ secrets.TINYBIRD_E2E_USER_TOKEN }} - TINYBIRD_E2E_WORKSPACE_ADMIN_TOKEN: ${{ secrets.TINYBIRD_E2E_WORKSPACE_ADMIN_TOKEN }} + TINYBIRD_E2E_WORKSPACE_TOKEN: ${{ secrets.TINYBIRD_E2E_WORKSPACE_TOKEN }} + TINYBIRD_E2E_DEPLOY_TOKEN: ${{ secrets.TINYBIRD_E2E_DEPLOY_TOKEN }} steps: - name: Check required live E2E secrets id: check-secrets run: | - if [ -z "${TINYBIRD_E2E_USER_TOKEN}" ] || [ -z "${TINYBIRD_E2E_WORKSPACE_ADMIN_TOKEN}" ]; then + if [ -z "${TINYBIRD_E2E_WORKSPACE_TOKEN}" ] || [ -z "${TINYBIRD_E2E_DEPLOY_TOKEN}" ]; then echo "run_e2e_tests=false" >> "$GITHUB_OUTPUT" echo "Skipping live E2E: required secrets are not configured." else diff --git a/e2e-live/append.test.ts b/e2e-live/append.test.ts index 6f5aebd..c08914c 100644 --- a/e2e-live/append.test.ts +++ b/e2e-live/append.test.ts @@ -5,7 +5,6 @@ import { beforeAll, beforeEach, afterEach, - afterAll, } from "vitest"; import * as fs from "node:fs"; import * as path from "node:path"; @@ -15,9 +14,7 @@ import { getBranch, deleteBranch, BranchApiError } from "../src/api/branches.js" import { createClient } from "../src/client/base.js"; import { getLiveE2EConfigFromEnv, - assertWorkspaceAdminToken, - createWorkspaceWithToken, - deleteWorkspace, + createRunScopedName, type LiveE2EConfig, } from "./cloud-workspace.js"; import { @@ -63,26 +60,21 @@ describeLive("E2E Live: append", () => { let tempDir = ""; let originalEnv: NodeJS.ProcessEnv; - let workspaceId = ""; - let workspaceName = ""; - let workspaceToken = ""; + const workspaceToken = liveConfig?.workspaceToken ?? ""; + const runName = createRunScopedName("sdk_append"); let branchName = ""; let branchToken = ""; - beforeAll(async () => { + beforeAll(() => { ensureDistBuild(); - await assertWorkspaceAdminToken(config); - - const workspace = await createWorkspaceWithToken(config, "sdk_append"); - workspaceId = workspace.id; - workspaceName = workspace.name; - workspaceToken = workspace.token; }); beforeEach(async () => { tempDir = createTempProjectDir(); originalEnv = { ...process.env }; - process.env.GITHUB_REF_NAME = `live-append/${workspaceName}`; + // The SDK prefers GITHUB_HEAD_REF (set on pull_request runs) over GITHUB_REF_NAME. + delete process.env.GITHUB_HEAD_REF; + process.env.GITHUB_REF_NAME = `live-append/${runName}`; process.env.TINYBIRD_TOKEN = workspaceToken; const initResult = await runInit({ @@ -130,16 +122,10 @@ describeLive("E2E Live: append", () => { cleanupTempProjectDir(tempDir); }); - afterAll(async () => { - if (workspaceId && workspaceName) { - await deleteWorkspace(config, workspaceId, workspaceName); - } - }); - it("appends rows from a local CSV file and serves them via endpoint query", async () => { const csvPath = path.join(tempDir, "page_views.csv"); const timestamp = toTinybirdDateTime(new Date()); - const pathname = "/append-live-e2e"; + const pathname = `/append-live-e2e/${runName}`; fs.writeFileSync( csvPath, diff --git a/e2e-live/build.test.ts b/e2e-live/build.test.ts index 6f1facd..cd7597d 100644 --- a/e2e-live/build.test.ts +++ b/e2e-live/build.test.ts @@ -14,9 +14,7 @@ import { getBranch, deleteBranch, BranchApiError } from "../src/api/branches.js" import { createClient } from "../src/client/base.js"; import { getLiveE2EConfigFromEnv, - assertWorkspaceAdminToken, - createWorkspaceWithToken, - deleteWorkspace, + createRunScopedName, type LiveE2EConfig, } from "./cloud-workspace.js"; import { @@ -65,24 +63,20 @@ describeLive("E2E Live: build", () => { let tempDir = ""; let originalEnv: NodeJS.ProcessEnv; - let workspaceId = ""; - let workspaceName = ""; - let workspaceToken = ""; + const workspaceToken = liveConfig?.workspaceToken ?? ""; + const runName = createRunScopedName("sdk_build"); let tinybirdBranchName = ""; - beforeAll(async () => { + beforeAll(() => { ensureDistBuild(); - await assertWorkspaceAdminToken(config); - const workspace = await createWorkspaceWithToken(config, "sdk_build"); - workspaceId = workspace.id; - workspaceName = workspace.name; - workspaceToken = workspace.token; }); beforeEach(() => { tempDir = createTempProjectDir(); originalEnv = { ...process.env }; - process.env.GITHUB_REF_NAME = `live-build/${workspaceName}`; + // The SDK prefers GITHUB_HEAD_REF (set on pull_request runs) over GITHUB_REF_NAME. + delete process.env.GITHUB_HEAD_REF; + process.env.GITHUB_REF_NAME = `live-build/${runName}`; process.env.TINYBIRD_TOKEN = workspaceToken; }); @@ -104,10 +98,6 @@ describeLive("E2E Live: build", () => { } } } - - if (workspaceId && workspaceName) { - await deleteWorkspace(config, workspaceId, workspaceName); - } }); it("builds and deploys resources, then serves queries from the deployed endpoint", async () => { @@ -153,14 +143,15 @@ describeLive("E2E Live: build", () => { token: branchToken, }); + const pathname = `/live-e2e/${runName}`; await client.ingest("page_views", { timestamp: toTinybirdDateTime(new Date()), session_id: "live_test_session", - pathname: "/live-e2e", + pathname, referrer: null, }); const rows = await waitForPipeData(branchToken, config.baseUrl); - expect(rows.length).toBeGreaterThan(0); + expect(rows.map((row) => row.pathname)).toContain(pathname); }); }); diff --git a/e2e-live/cloud-workspace.ts b/e2e-live/cloud-workspace.ts index 377f46e..43cd8d7 100644 --- a/e2e-live/cloud-workspace.ts +++ b/e2e-live/cloud-workspace.ts @@ -1,182 +1,61 @@ import { loadEnvFiles } from "../src/cli/config.js"; -import { tinybirdFetch } from "../src/api/fetcher.js"; - +import { runDeploy } from "../src/cli/commands/deploy.js"; + +/** + * Live E2E suites run against a single, dedicated CI workspace. + * + * - TINYBIRD_E2E_WORKSPACE_TOKEN: the workspace's admin token. Used for branch + * operations (create/get/delete require ADMIN scope), ingestion, queries and pull. + * - TINYBIRD_E2E_DEPLOY_TOKEN: a static token with WORKSPACE:DEPLOY scope, used for + * production deployments. Optional locally; falls back to the workspace token. + * + * No user tokens are needed: suites never create or delete workspaces. + */ export interface LiveE2EConfig { baseUrl: string; - userToken: string; - workspaceAdminToken: string; -} - -export interface WorkspaceResponse { - id: string; - name: string; - token?: string; -} - -interface WorkspaceTokenInfo { - id: string; - name: string; - scope?: string; -} - -interface JsonRequestOptions { - method: "GET" | "POST" | "DELETE"; - token: string; - body?: string; - contentType?: string; + workspaceToken: string; + deployToken: string; } // Load env from repository root where tinybird.config.* usually lives. loadEnvFiles(process.cwd()); -function parseJsonOrThrow(responseText: string, endpoint: string): unknown { - try { - return JSON.parse(responseText); - } catch { - throw new Error(`Expected JSON from ${endpoint}, got: ${responseText || ""}`); - } -} - -async function requestJson(endpoint: string, options: JsonRequestOptions): Promise { - const response = await tinybirdFetch(endpoint, { - method: options.method, - headers: { - Authorization: `Bearer ${options.token}`, - ...(options.contentType ? { "Content-Type": options.contentType } : {}), - }, - body: options.body, - }); - - const responseText = await response.text(); - if (!response.ok) { - throw new Error( - `Request failed (${options.method} ${endpoint}): ` + - `${response.status} ${response.statusText} - ${responseText || ""}` - ); - } - - return parseJsonOrThrow(responseText, endpoint); -} - export function getLiveE2EConfigFromEnv(): LiveE2EConfig | null { - const userToken = process.env.TINYBIRD_E2E_USER_TOKEN; - const workspaceAdminToken = process.env.TINYBIRD_E2E_WORKSPACE_ADMIN_TOKEN; + const workspaceToken = process.env.TINYBIRD_E2E_WORKSPACE_TOKEN; const baseUrl = process.env.TINYBIRD_E2E_BASE_URL ?? "https://api.tinybird.co"; - if (!userToken || !workspaceAdminToken) { + if (!workspaceToken) { return null; } return { baseUrl, - userToken, - workspaceAdminToken, + workspaceToken, + deployToken: process.env.TINYBIRD_E2E_DEPLOY_TOKEN || workspaceToken, }; } -export function createWorkspaceName(prefix = "sdk_e2e"): string { +/** + * Unique, branch-safe identifier for this run so concurrent CI runs sharing the + * workspace never collide on branch names. + */ +export function createRunScopedName(prefix: string): string { + const runId = process.env.GITHUB_RUN_ID ?? "local"; const suffix = Math.random().toString(36).slice(2, 8); - return `${prefix}_${Date.now()}_${suffix}`.replace(/[^a-zA-Z0-9_]/g, "_"); -} - -export async function assertWorkspaceAdminToken(config: LiveE2EConfig): Promise { - const endpoint = new URL("/v1/workspace", config.baseUrl).toString(); - const payload = (await requestJson(endpoint, { - method: "GET", - token: config.workspaceAdminToken, - })) as WorkspaceTokenInfo; - - if (!payload.id || !payload.name) { - throw new Error("Workspace admin token validation failed: missing workspace identity in response."); - } - - if (payload.scope !== "admin") { - throw new Error( - `Workspace admin token validation failed: expected scope=admin, got scope=${payload.scope ?? "unknown"}.` - ); - } + return `${prefix}_${runId}_${suffix}`.replace(/[^a-zA-Z0-9_]/g, "_"); } -export async function createWorkspace( - config: LiveE2EConfig, - workspaceName: string -): Promise { - const endpoint = new URL("/v1/workspaces", config.baseUrl).toString(); - const body = new URLSearchParams({ name: workspaceName }).toString(); - - const payload = (await requestJson(endpoint, { - method: "POST", - token: config.userToken, - body, - contentType: "application/x-www-form-urlencoded", - })) as WorkspaceResponse; - - if (!payload.id || !payload.name) { - throw new Error("Workspace creation failed: response missing id or name."); - } - - return payload; -} - -export async function getWorkspaceWithToken( - config: LiveE2EConfig, - workspaceId: string -): Promise { - const endpoint = new URL(`/v1/workspaces/${encodeURIComponent(workspaceId)}`, config.baseUrl); - endpoint.searchParams.set("with_token", "true"); - - const payload = (await requestJson(endpoint.toString(), { - method: "GET", - token: config.userToken, - })) as WorkspaceResponse; - - if (!payload.id || !payload.name) { - throw new Error(`Failed to fetch workspace ${workspaceId}: response missing id or name.`); - } - - return payload; -} - -export async function waitForWorkspaceToken( - config: LiveE2EConfig, - workspaceId: string, - maxAttempts = 15, - delayMs = 1_000 -): Promise { - for (let attempt = 0; attempt < maxAttempts; attempt++) { - const workspace = await getWorkspaceWithToken(config, workspaceId); - if (workspace.token) { - return workspace.token; - } - await new Promise((resolve) => setTimeout(resolve, delayMs)); +/** + * Deploy to production with the WORKSPACE:DEPLOY token, then switch back to the + * workspace admin token for ingestion, queries and pull. The workspace is shared + * across suites and runs, so destructive operations are allowed: each deploy + * replaces whatever the previous one left behind. + */ +export async function deployToProduction(config: LiveE2EConfig, cwd: string) { + process.env.TINYBIRD_TOKEN = config.deployToken; + try { + return await runDeploy({ cwd, allowDestructiveOperations: true }); + } finally { + process.env.TINYBIRD_TOKEN = config.workspaceToken; } - - throw new Error(`Timed out waiting for workspace token (workspace=${workspaceId}).`); -} - -export async function createWorkspaceWithToken( - config: LiveE2EConfig, - namePrefix = "sdk_e2e" -): Promise> { - const workspace = await createWorkspace(config, createWorkspaceName(namePrefix)); - const token = await waitForWorkspaceToken(config, workspace.id); - return { - id: workspace.id, - name: workspace.name, - token, - }; -} - -export async function deleteWorkspace( - config: LiveE2EConfig, - workspaceId: string, - workspaceName: string -): Promise { - const endpoint = new URL(`/v1/workspaces/${encodeURIComponent(workspaceId)}`, config.baseUrl); - endpoint.searchParams.set("confirmation", workspaceName); - - await requestJson(endpoint.toString(), { - method: "DELETE", - token: config.userToken, - }); } diff --git a/e2e-live/deploy.test.ts b/e2e-live/deploy.test.ts index 3a23b56..0ef0132 100644 --- a/e2e-live/deploy.test.ts +++ b/e2e-live/deploy.test.ts @@ -5,20 +5,16 @@ import { beforeAll, beforeEach, afterEach, - afterAll, } from "vitest"; import * as fs from "node:fs"; import * as path from "node:path"; import { pathToFileURL } from "node:url"; import { runInit } from "../src/cli/commands/init.js"; -import { runDeploy } from "../src/cli/commands/deploy.js"; import { listDatasources, listPipesV1 } from "../src/api/resources.js"; import { tinybirdFetch } from "../src/api/fetcher.js"; import { getLiveE2EConfigFromEnv, - assertWorkspaceAdminToken, - createWorkspaceWithToken, - deleteWorkspace, + deployToProduction, type LiveE2EConfig, } from "./cloud-workspace.js"; import { @@ -216,22 +212,17 @@ describeLive("E2E Live: deploy", () => { let tempDir = ""; let originalEnv: NodeJS.ProcessEnv; - let workspaceId = ""; - let workspaceName = ""; - let workspaceToken = ""; + const workspaceToken = liveConfig?.workspaceToken ?? ""; - beforeAll(async () => { + beforeAll(() => { ensureDistBuild(); - await assertWorkspaceAdminToken(config); - const workspace = await createWorkspaceWithToken(config, "sdk_deploy"); - workspaceId = workspace.id; - workspaceName = workspace.name; - workspaceToken = workspace.token; }); beforeEach(() => { tempDir = createTempProjectDir(); originalEnv = { ...process.env }; + // The SDK prefers GITHUB_HEAD_REF (set on pull_request runs) over GITHUB_REF_NAME. + delete process.env.GITHUB_HEAD_REF; process.env.GITHUB_REF_NAME = "main"; process.env.TINYBIRD_TOKEN = workspaceToken; process.env.TINYBIRD_URL = config.baseUrl; @@ -242,12 +233,6 @@ describeLive("E2E Live: deploy", () => { cleanupTempProjectDir(tempDir); }); - afterAll(async () => { - if (workspaceId && workspaceName) { - await deleteWorkspace(config, workspaceId, workspaceName); - } - }); - it("deploys custom TypeScript entities to production and serves ingested data", async () => { const initResult = await runInit({ cwd: tempDir, @@ -260,7 +245,7 @@ describeLive("E2E Live: deploy", () => { setConfigBaseUrl(tempDir, config.baseUrl); writeProductionTestEntities(tempDir); - const deployResult = await runDeploy({ cwd: tempDir }); + const deployResult = await deployToProduction(config, tempDir); expect(deployResult.success).toBe(true); expect(deployResult.deploy?.success).toBe(true); @@ -303,7 +288,7 @@ describeLive("E2E Live: deploy", () => { setConfigBaseUrl(tempDir, config.baseUrl); writeProductionTestEntities(tempDir); - const firstDeployResult = await runDeploy({ cwd: tempDir }); + const firstDeployResult = await deployToProduction(config, tempDir); expect(firstDeployResult.success).toBe(true); expect(firstDeployResult.deploy?.success).toBe(true); const previousLiveDeployment = await getCurrentLiveDeployment(config, workspaceToken); @@ -328,7 +313,7 @@ describeLive("E2E Live: deploy", () => { writeProductionTestEntities(tempDir, { requireNonNegativeMetric: true }); - const secondDeployResult = await runDeploy({ cwd: tempDir }); + const secondDeployResult = await deployToProduction(config, tempDir); expect(secondDeployResult.success).toBe(true); expect(secondDeployResult.deploy?.success).toBe(true); expect(secondDeployResult.deploy?.result).toBe("success"); diff --git a/e2e-live/global-setup.ts b/e2e-live/global-setup.ts new file mode 100644 index 0000000..003e8f1 --- /dev/null +++ b/e2e-live/global-setup.ts @@ -0,0 +1,35 @@ +import { listBranches, deleteBranch, BranchApiError } from "../src/api/branches.js"; +import { getLiveE2EConfigFromEnv } from "./cloud-workspace.js"; + +/** + * Branch names created by the live suites. GITHUB_REF_NAME is set to + * `live-/`, which the SDK sanitizes to `live__...`. + */ +const LIVE_BRANCH_PREFIX = "live_"; + +/** + * All live E2E runs share one workspace and CI serializes them, so any `live_*` + * branch present at startup was left behind by a cancelled or crashed run. + * Remove them so they never count against the workspace's branch limit. + */ +export default async function setup(): Promise { + const config = getLiveE2EConfigFromEnv(); + if (!config) { + return; + } + + const apiConfig = { baseUrl: config.baseUrl, token: config.workspaceToken }; + const branches = await listBranches(apiConfig); + const stale = branches.filter((branch) => branch.name.startsWith(LIVE_BRANCH_PREFIX)); + + for (const branch of stale) { + try { + await deleteBranch(apiConfig, branch.name); + console.log(`[e2e-live] Deleted leftover branch ${branch.name}`); + } catch (error) { + if (!(error instanceof BranchApiError && error.status === 404)) { + throw error; + } + } + } +} diff --git a/e2e-live/init.test.ts b/e2e-live/init.test.ts index ad40ad3..b65eed6 100644 --- a/e2e-live/init.test.ts +++ b/e2e-live/init.test.ts @@ -5,7 +5,6 @@ import { beforeAll, beforeEach, afterEach, - afterAll, } from "vitest"; import * as fs from "node:fs"; import * as path from "node:path"; @@ -13,9 +12,7 @@ import { runInit } from "../src/cli/commands/init.js"; import { getWorkspace } from "../src/api/workspaces.js"; import { getLiveE2EConfigFromEnv, - assertWorkspaceAdminToken, - createWorkspaceWithToken, - deleteWorkspace, + createRunScopedName, type LiveE2EConfig, } from "./cloud-workspace.js"; import { @@ -34,23 +31,19 @@ describeLive("E2E Live: init", () => { let tempDir = ""; let originalEnv: NodeJS.ProcessEnv; - let workspaceId = ""; - let workspaceName = ""; - let workspaceToken = ""; + const workspaceToken = liveConfig?.workspaceToken ?? ""; + const runName = createRunScopedName("sdk_init"); - beforeAll(async () => { + beforeAll(() => { ensureDistBuild(); - await assertWorkspaceAdminToken(config); - const workspace = await createWorkspaceWithToken(config, "sdk_init"); - workspaceId = workspace.id; - workspaceName = workspace.name; - workspaceToken = workspace.token; }); beforeEach(() => { tempDir = createTempProjectDir(); originalEnv = { ...process.env }; - process.env.GITHUB_REF_NAME = `live-init/${workspaceName}`; + // The SDK prefers GITHUB_HEAD_REF (set on pull_request runs) over GITHUB_REF_NAME. + delete process.env.GITHUB_HEAD_REF; + process.env.GITHUB_REF_NAME = `live-init/${runName}`; process.env.TINYBIRD_TOKEN = workspaceToken; }); @@ -59,19 +52,12 @@ describeLive("E2E Live: init", () => { cleanupTempProjectDir(tempDir); }); - afterAll(async () => { - if (!workspaceId || !workspaceName) { - return; - } - await deleteWorkspace(config, workspaceId, workspaceName); - }); - it("initializes a project against a real workspace token", async () => { const workspaceInfo = await getWorkspace({ baseUrl: config.baseUrl, token: workspaceToken, }); - expect(workspaceInfo.id).toBe(workspaceId); + expect(workspaceInfo.id).toBeTruthy(); const initResult = await runInit({ cwd: tempDir, diff --git a/e2e-live/null-engine.test.ts b/e2e-live/null-engine.test.ts index 24dbbc1..7cb63c8 100644 --- a/e2e-live/null-engine.test.ts +++ b/e2e-live/null-engine.test.ts @@ -5,7 +5,6 @@ import { beforeAll, beforeEach, afterEach, - afterAll, } from "vitest"; import * as fs from "node:fs"; import * as path from "node:path"; @@ -15,9 +14,7 @@ import { getBranch, deleteBranch, BranchApiError } from "../src/api/branches.js" import { createClient } from "../src/client/base.js"; import { getLiveE2EConfigFromEnv, - assertWorkspaceAdminToken, - createWorkspaceWithToken, - deleteWorkspace, + createRunScopedName, type LiveE2EConfig, } from "./cloud-workspace.js"; import { @@ -155,25 +152,20 @@ describeLive("E2E Live: Null engine", () => { let tempDir = ""; let originalEnv: NodeJS.ProcessEnv; - let workspaceId = ""; - let workspaceName = ""; - let workspaceToken = ""; + const workspaceToken = liveConfig?.workspaceToken ?? ""; + const runName = createRunScopedName("sdk_null_engine"); let branchName = ""; - beforeAll(async () => { + beforeAll(() => { ensureDistBuild(); - await assertWorkspaceAdminToken(config); - - const workspace = await createWorkspaceWithToken(config, "sdk_null_engine"); - workspaceId = workspace.id; - workspaceName = workspace.name; - workspaceToken = workspace.token; }); beforeEach(() => { tempDir = createTempProjectDir(); originalEnv = { ...process.env }; - process.env.GITHUB_REF_NAME = `live-null-engine/${workspaceName}`; + // The SDK prefers GITHUB_HEAD_REF (set on pull_request runs) over GITHUB_REF_NAME. + delete process.env.GITHUB_HEAD_REF; + process.env.GITHUB_REF_NAME = `live-null-engine/${runName}`; process.env.TINYBIRD_TOKEN = workspaceToken; }); @@ -196,12 +188,6 @@ describeLive("E2E Live: Null engine", () => { cleanupTempProjectDir(tempDir); }); - afterAll(async () => { - if (workspaceId && workspaceName) { - await deleteWorkspace(config, workspaceId, workspaceName); - } - }); - it("builds a Null engine source and ingests through its materialized view target", async () => { const initResult = await runInit({ cwd: tempDir, diff --git a/e2e-live/pull.test.ts b/e2e-live/pull.test.ts index 9d4d67d..5bcf792 100644 --- a/e2e-live/pull.test.ts +++ b/e2e-live/pull.test.ts @@ -5,12 +5,10 @@ import { beforeAll, beforeEach, afterEach, - afterAll, } from "vitest"; import * as fs from "node:fs"; import * as path from "node:path"; import { runInit } from "../src/cli/commands/init.js"; -import { runDeploy } from "../src/cli/commands/deploy.js"; import { runPull } from "../src/cli/commands/pull.js"; import { listDatasources, @@ -22,9 +20,7 @@ import { } from "../src/api/resources.js"; import { getLiveE2EConfigFromEnv, - assertWorkspaceAdminToken, - createWorkspaceWithToken, - deleteWorkspace, + deployToProduction, type LiveE2EConfig, } from "./cloud-workspace.js"; import { @@ -83,18 +79,10 @@ describeLive("E2E Live: pull", () => { let tempDir = ""; let originalEnv: NodeJS.ProcessEnv; - let workspaceId = ""; - let workspaceName = ""; - let workspaceToken = ""; + const workspaceToken = liveConfig?.workspaceToken ?? ""; - beforeAll(async () => { + beforeAll(() => { ensureDistBuild(); - await assertWorkspaceAdminToken(config); - - const workspace = await createWorkspaceWithToken(config, "sdk_pull"); - workspaceId = workspace.id; - workspaceName = workspace.name; - workspaceToken = workspace.token; }); beforeEach(() => { @@ -102,6 +90,8 @@ describeLive("E2E Live: pull", () => { originalEnv = { ...process.env }; // Force main branch detection so deploy runs against production workspace. + // The SDK prefers GITHUB_HEAD_REF (set on pull_request runs) over GITHUB_REF_NAME. + delete process.env.GITHUB_HEAD_REF; process.env.GITHUB_REF_NAME = "main"; process.env.TINYBIRD_TOKEN = workspaceToken; }); @@ -111,12 +101,6 @@ describeLive("E2E Live: pull", () => { cleanupTempProjectDir(tempDir); }); - afterAll(async () => { - if (workspaceId && workspaceName) { - await deleteWorkspace(config, workspaceId, workspaceName); - } - }); - it("deploys to production and pulls matching datasource/pipe files", async () => { const initResult = await runInit({ cwd: tempDir, @@ -128,7 +112,7 @@ describeLive("E2E Live: pull", () => { setConfigBaseUrl(tempDir, config.baseUrl); - const deployResult = await runDeploy({ cwd: tempDir }); + const deployResult = await deployToProduction(config, tempDir); expect(deployResult.success).toBe(true); expect(deployResult.deploy?.success).toBe(true); diff --git a/vitest.e2e.live.config.ts b/vitest.e2e.live.config.ts index 72de94c..41fcaab 100644 --- a/vitest.e2e.live.config.ts +++ b/vitest.e2e.live.config.ts @@ -5,6 +5,7 @@ export default defineConfig({ globals: true, environment: "node", include: ["e2e-live/**/*.test.ts"], + globalSetup: ["./e2e-live/global-setup.ts"], fileParallelism: false, testTimeout: 180_000, hookTimeout: 180_000,