-
Notifications
You must be signed in to change notification settings - Fork 8
Add facilitator-server Postgres e2e CI pipeline #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bf84972
Add facilitator server e2e CI pipeline
ponderingdemocritus f1e7b32
Harden facilitator e2e private key handling
ponderingdemocritus 6d9e9a2
Merge origin/main and resolve facilitator conflicts
ponderingdemocritus 330d559
Run facilitator e2e job on Node 22
ponderingdemocritus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { strict as assert } from "node:assert"; | ||
| import { Pool } from "pg"; | ||
| import { resolve } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { resolveE2ePrivateKey } from "./e2e-env.js"; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const testsDir = resolve(__filename, ".."); | ||
| const serverDir = resolve(testsDir, ".."); | ||
|
|
||
| const DATABASE_URL = process.env.DATABASE_URL; | ||
| const PORT = Number(process.env.PORT ?? "18090"); | ||
| const BEARER_TOKEN = process.env.BEARER_TOKEN ?? "e2e-test-token"; | ||
|
|
||
| if (!DATABASE_URL) { | ||
| throw new Error("DATABASE_URL is required for e2e test"); | ||
| } | ||
|
|
||
| const sleep = (ms: number): Promise<void> => | ||
| new Promise((resolveSleep) => setTimeout(resolveSleep, ms)); | ||
|
|
||
| async function waitForServer(url: string, timeoutMs = 30_000): Promise<void> { | ||
| const deadline = Date.now() + timeoutMs; | ||
|
|
||
| while (Date.now() < deadline) { | ||
| try { | ||
| const response = await fetch(`${url}/supported`); | ||
| if (response.ok) return; | ||
| } catch { | ||
| // Server not ready yet. | ||
| } | ||
| await sleep(500); | ||
| } | ||
|
|
||
| throw new Error("Timed out waiting for facilitator server to start"); | ||
| } | ||
|
|
||
| async function waitForDatabase( | ||
| pool: Pool, | ||
| timeoutMs = 20_000 | ||
| ): Promise<void> { | ||
| const deadline = Date.now() + timeoutMs; | ||
|
|
||
| while (Date.now() < deadline) { | ||
| try { | ||
| await pool.query("SELECT 1"); | ||
| return; | ||
| } catch { | ||
| // Database not ready yet. | ||
| } | ||
| await sleep(500); | ||
| } | ||
|
|
||
| throw new Error("Timed out waiting for Postgres to become ready"); | ||
| } | ||
|
|
||
| async function waitForRecord(pool: Pool, timeoutMs = 15_000): Promise<void> { | ||
| const deadline = Date.now() + timeoutMs; | ||
|
|
||
| while (Date.now() < deadline) { | ||
| const result = await pool.query<{ count: string }>( | ||
| `SELECT COUNT(*) AS count | ||
| FROM resource_call_records | ||
| WHERE path = '/verify' AND response_status = 400` | ||
| ); | ||
|
|
||
| if (Number(result.rows[0]?.count ?? "0") > 0) { | ||
| return; | ||
| } | ||
|
|
||
| await sleep(300); | ||
| } | ||
|
|
||
| throw new Error("No /verify tracking row found in Postgres"); | ||
| } | ||
|
|
||
| async function run(): Promise<void> { | ||
| const baseUrl = `http://127.0.0.1:${PORT}`; | ||
| const pool = new Pool({ connectionString: DATABASE_URL }); | ||
| let failed = false; | ||
| const privateKey = resolveE2ePrivateKey(process.env.EVM_PRIVATE_KEY); | ||
| if (process.env.EVM_PRIVATE_KEY && privateKey !== process.env.EVM_PRIVATE_KEY) { | ||
| console.warn( | ||
| "EVM_PRIVATE_KEY for e2e was malformed; using normalized fallback key." | ||
| ); | ||
| } | ||
|
|
||
| const server = Bun.spawn({ | ||
| cmd: ["node", "dist/index.js"], | ||
| cwd: serverDir, | ||
| env: { | ||
| ...process.env, | ||
| PORT: String(PORT), | ||
| DATABASE_URL, | ||
| TRACKING_ALLOW_IN_MEMORY_FALLBACK: "false", | ||
| OTEL_SDK_DISABLED: "true", | ||
| BEARER_TOKEN, | ||
| EVM_PRIVATE_KEY: privateKey, | ||
| EVM_NETWORKS: process.env.EVM_NETWORKS ?? "base-sepolia", | ||
| }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const stdoutTextPromise = new Response(server.stdout).text(); | ||
| const stderrTextPromise = new Response(server.stderr).text(); | ||
|
|
||
| try { | ||
| await waitForDatabase(pool); | ||
| await waitForServer(baseUrl); | ||
| await pool.query("TRUNCATE TABLE resource_call_records"); | ||
|
|
||
| const verifyResponse = await fetch(`${baseUrl}/verify`, { | ||
| method: "POST", | ||
| headers: { | ||
| "content-type": "application/json", | ||
| authorization: `Bearer ${BEARER_TOKEN}`, | ||
| }, | ||
| body: JSON.stringify({}), | ||
| }); | ||
|
|
||
| assert.equal(verifyResponse.status, 400, "Expected /verify to return 400"); | ||
| await waitForRecord(pool); | ||
| } catch (error) { | ||
| failed = true; | ||
| throw error; | ||
| } finally { | ||
| server.kill(); | ||
| await server.exited; | ||
| await pool.end(); | ||
|
|
||
| const stdoutText = await stdoutTextPromise.catch(() => ""); | ||
| const stderrText = await stderrTextPromise.catch(() => ""); | ||
| if (failed && stdoutText.trim()) { | ||
| console.log("=== facilitator stdout ==="); | ||
| console.log(stdoutText); | ||
| } | ||
| if (failed && stderrText.trim()) { | ||
| console.log("=== facilitator stderr ==="); | ||
| console.log(stderrText); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| run().catch(async (error) => { | ||
| console.error(error); | ||
| process.exit(1); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { resolveE2ePrivateKey } from "./e2e-env.js"; | ||
|
|
||
| describe("resolveE2ePrivateKey", () => { | ||
| test("keeps a valid 0x-prefixed private key", () => { | ||
| const key = | ||
| "0x0000000000000000000000000000000000000000000000000000000000000001"; | ||
| expect(resolveE2ePrivateKey(key)).toBe(key); | ||
| }); | ||
|
|
||
| test("prefixes a valid 64-char hex key", () => { | ||
| const raw = | ||
| "0000000000000000000000000000000000000000000000000000000000000001"; | ||
| expect(resolveE2ePrivateKey(raw)).toBe(`0x${raw}`); | ||
| }); | ||
|
|
||
| test("falls back for malformed values", () => { | ||
| const fallback = | ||
| "0x0000000000000000000000000000000000000000000000000000000000000001"; | ||
| expect(resolveE2ePrivateKey("1")).toBe(fallback); | ||
| expect(resolveE2ePrivateKey(undefined)).toBe(fallback); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| const DEFAULT_E2E_PRIVATE_KEY = | ||
| "0x0000000000000000000000000000000000000000000000000000000000000001"; | ||
|
|
||
| const HEX_64 = /^[0-9a-fA-F]{64}$/; | ||
| const HEX_0X_64 = /^0x[0-9a-fA-F]{64}$/; | ||
|
|
||
| /** | ||
| * Returns a valid hex private key for e2e tests. | ||
| * Falls back to a known public test key when input is missing or malformed. | ||
| */ | ||
| export function resolveE2ePrivateKey(value?: string): string { | ||
| if (!value) return DEFAULT_E2E_PRIVATE_KEY; | ||
| if (HEX_0X_64.test(value)) return value; | ||
| if (HEX_64.test(value)) return `0x${value}`; | ||
| return DEFAULT_E2E_PRIVATE_KEY; | ||
| } | ||
|
|
||
| export { DEFAULT_E2E_PRIVATE_KEY }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misleading log message when in-memory fallback is active.
When execution reaches Line 73,
TRACKING_ALLOW_IN_MEMORY_FALLBACKis necessarilytrue(otherwise the process would have exited at Line 48). The message telling the user to "set TRACKING_ALLOW_IN_MEMORY_FALLBACK=true to allow this explicitly" is confusing because they already did.Proposed fix
} else if (DATABASE_URL) { console.log( - `Resource tracking: In-memory (DB init failed; set TRACKING_ALLOW_IN_MEMORY_FALLBACK=true to allow this explicitly)` + `Resource tracking: In-memory (DB init failed; running with in-memory fallback)` ); } else {📝 Committable suggestion
🤖 Prompt for AI Agents