Path: blob/master/src/packages/next/pages/api/v2/exec.e2e.test.ts
5751 views
/** @jest-environment node */12const runE2E = process.env.COCALC_E2E === "true";3const describeE2E = runE2E ? describe : describe.skip;45describeE2E("/api/v2/exec e2e", () => {6const apiKey = process.env.COCALC_API_KEY ?? "";7const projectId = process.env.COCALC_PROJECT_ID ?? "";8const host = process.env.COCALC_HOST ?? "http://localhost:5000";910beforeAll(() => {11if (!apiKey || !projectId) {12throw new Error(13"COCALC_API_KEY and COCALC_PROJECT_ID must be set when COCALC_E2E=true",14);15}16});1718test("exec date -Is", async () => {19const auth = Buffer.from(`${apiKey}:`).toString("base64");20const response = await fetch(`${host}/api/v2/exec`, {21method: "POST",22headers: {23"Content-Type": "application/json",24Authorization: `Basic ${auth}`,25},26body: JSON.stringify({27project_id: projectId,28command: "date",29args: ["-Is"],30}),31});3233expect(response.status).toBe(200);34const data = await response.json();35if (data?.error) {36throw new Error(`API error: ${data.error}`);37}3839const stdout = String(data.stdout ?? "").trim();40const stderr = String(data.stderr ?? "").trim();4142expect(data.exit_code).toBe(0);43expect(stderr).toBe("");44expect(stdout).toMatch(45/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{2}:\d{2}$/,46);47});4849test("exec async_call with async_get", async () => {50const auth = Buffer.from(`${apiKey}:`).toString("base64");51const startResp = await fetch(`${host}/api/v2/exec`, {52method: "POST",53headers: {54"Content-Type": "application/json",55Authorization: `Basic ${auth}`,56},57body: JSON.stringify({58project_id: projectId,59bash: true,60command:61"echo $TEST_ENV; for i in $(seq 10); do echo i=$i; sleep 0.1; done",62timeout: 10,63async_call: true,64env: {65TEST_ENV: "123",66},67}),68});6970expect(startResp.status).toBe(200);71const startData = await startResp.json();72if (startData?.error) {73throw new Error(`API error: ${startData.error}`);74}7576expect(startData.type).toBe("async");77expect(startData.job_id).toBeTruthy();78expect(["running", "completed"]).toContain(startData.status);7980const pollResp = await fetch(`${host}/api/v2/exec`, {81method: "POST",82headers: {83"Content-Type": "application/json",84Authorization: `Basic ${auth}`,85},86body: JSON.stringify({87project_id: projectId,88async_get: startData.job_id,89async_stats: true,90async_await: true,91}),92});9394expect(pollResp.status).toBe(200);95const pollData = await pollResp.json();96if (pollData?.error) {97throw new Error(`API error: ${pollData.error}`);98}99100const stdout = String(pollData.stdout ?? "").trim();101const stderr = String(pollData.stderr ?? "").trim();102103expect(pollData.type).toBe("async");104expect(pollData.job_id).toBe(startData.job_id);105expect(pollData.status).toBe("completed");106expect(pollData.exit_code).toBe(0);107expect(stderr).toBe("");108const stdoutLines = stdout.split("\n");109expect(stdoutLines[0]).toBe("123");110expect(stdout).toContain("i=10");111});112});113114115