Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions pkgs/github-ci/worker/src/turso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,34 @@ export class Turso {
}),
});
if (!res.ok) throw new Error(`turso pipeline failed: HTTP ${res.status}`);
const out = (await res.json()) as { results?: PipelineResult[] };
const failed = (out.results ?? []).find((r) => r.type === "error");
// The envelope is validated before anything reads it (#6949): a 200
// whose body is not the pipeline shape (null, a scalar, an object
// without a results array, non-JSON) must fail in this client's own
// contract form, never resolve as a successful write or escape as a
// raw TypeError/SyntaxError from a downstream dereference.
let out: { results?: PipelineResult[] };
try {
out = (await res.json()) as { results?: PipelineResult[] };
} catch {
throw new Error("turso pipeline failed: malformed response (not JSON)");
}
if (!out || typeof out !== "object" || !Array.isArray(out.results)) {
throw new Error("turso pipeline failed: malformed response (missing results)");
}
const failed = out.results.find((r) => r?.type === "error");
if (failed) {
throw new Error(`turso pipeline failed: ${failed.error?.message ?? "pipeline error"}`);
}
return out.results ?? [];
return out.results;
}

async execute(sql: string, args?: TursoArg[]): Promise<PipelineResult> {
const results = await this.batch([{ sql, args }]);
return results[0];
const first: PipelineResult | undefined = results[0];
if (!first || typeof first !== "object") {
throw new Error("turso pipeline failed: malformed response (missing statement result)");
}
return first;
}

/** First column of first row as text, or null when no row. */
Expand Down
86 changes: 86 additions & 0 deletions pkgs/github-ci/worker/test/turso.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { fetchMock } from "cloudflare:test";
import { afterEach, beforeAll, describe, expect, it } from "vitest";
import { Turso } from "../src/turso";

// Response-envelope contract for the Turso pipeline client (#6949):
// a 200 whose body is not the pipeline shape must fail as
// "turso pipeline failed: …", never resolve as a successful write and
// never escape as a raw TypeError/SyntaxError.
const DB = "https://turso-shape.test";

beforeAll(() => {
fetchMock.activate();
fetchMock.disableNetConnect();
});
afterEach(() => fetchMock.assertNoPendingInterceptors());

function replyOnce(body: string, contentType = "application/json") {
fetchMock
.get(DB)
.intercept({ path: "/v2/pipeline", method: "POST" })
.reply(200, body, { headers: { "content-type": contentType } });
}

const client = () => new Turso(DB, "test-token");

const okEnvelope = JSON.stringify({
results: [
{ type: "ok", response: { result: { rows: [[{ type: "text", value: "main" }]] } } },
{ type: "ok" },
],
});

describe("Turso pipeline response envelope", () => {
it("resolves a well-formed envelope", async () => {
replyOnce(okEnvelope);
await expect(client().queryText("SELECT default_branch FROM t")).resolves.toBe("main");
});

it("keeps the per-statement error contract", async () => {
replyOnce(JSON.stringify({ results: [{ type: "error", error: { message: "no such table" } }] }));
await expect(client().execute("SELECT 1")).rejects.toThrow("turso pipeline failed: no such table");
});

it("rejects a null body as malformed, not a TypeError", async () => {
replyOnce("null");
await expect(client().execute("INSERT INTO ci_events VALUES (1)")).rejects.toThrow(
"turso pipeline failed: malformed response (missing results)",
);
});

it("rejects a results-less object body instead of reading the write as success", async () => {
replyOnce("{}");
await expect(client().execute("INSERT INTO ci_events VALUES (1)")).rejects.toThrow(
"turso pipeline failed: malformed response (missing results)",
);
});

it("rejects a scalar body and a queryText on a results-less body", async () => {
replyOnce('"ok"');
await expect(client().execute("INSERT INTO ci_events VALUES (1)")).rejects.toThrow(
"turso pipeline failed: malformed response (missing results)",
);
replyOnce("{}");
await expect(client().queryText("SELECT default_branch FROM t")).rejects.toThrow(
"turso pipeline failed: malformed response (missing results)",
);
});

it("rejects a non-JSON body in contract form, not a raw SyntaxError", async () => {
replyOnce("<html>bad gateway</html>", "text/html");
await expect(client().execute("SELECT 1")).rejects.toThrow(
"turso pipeline failed: malformed response (not JSON)",
);
});

it("rejects an envelope whose results lack the statement result", async () => {
replyOnce(JSON.stringify({ results: [] }));
await expect(client().execute("SELECT 1")).rejects.toThrow(
"turso pipeline failed: malformed response (missing statement result)",
);
replyOnce(JSON.stringify({ results: [null, { type: "ok" }] }));
await expect(client().execute("SELECT 1")).rejects.toThrow(
"turso pipeline failed: malformed response (missing statement result)",
);
});
});
7 changes: 7 additions & 0 deletions pkgs/github-ci/worker/test/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ describe("github-ci worker", () => {
expect(sqlSeen).toContain("github:developing-today/code:999:1:completed");
});

it("fails the delivery (500) when Turso answers a results-less 200 for the ci_events insert (#6949)", async () => {
fetchMock.get(PLATFORM).intercept({ path: "/v2/pipeline", method: "POST" }).reply(200, "{}");
const res = await post(runPayload, "workflow_run", await sign(runPayload));
expect(res.status).toBe(500);
expect(await res.text()).toContain("turso pipeline failed: malformed response");
});

it("skips non-completed runs without touching Turso", async () => {
const p = JSON.stringify({
action: "requested",
Expand Down
Loading