Files
elizaos--eliza/packages/cloud/api/test/e2e/preload.ts
T
wehub-resource-sync 426e9eeabd
Voice Workbench / headless workbench (mocked backends) (push) Has been cancelled
Voice Workbench / real acoustic lane (nightly, provisioned only) (push) Has been cancelled
ci / test (push) Has been cancelled
ci / lint-and-format (push) Has been cancelled
ci / build (push) Has been cancelled
ci / dev-startup (push) Has been cancelled
gitleaks / gitleaks (push) Has been cancelled
Markdown Links / Relative Markdown Links (push) Has been cancelled
Quality (Extended) / Homepage Build (PR smoke) (push) Has been cancelled
Quality (Extended) / Comment-only diff guard (push) Has been cancelled
Quality (Extended) / Format + Type Safety Ratchet (push) Has been cancelled
Quality (Extended) / Develop Gate (secret scan + UI determinism) (push) Has been cancelled
Quality (Extended) / Develop Gate (lint) (push) Has been cancelled
Chat shell gestures / Chat shell gesture + parity e2e (push) Has been cancelled
Cloud Gateway Discord / Test (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx @biomejs/biome check packages/lifeops-bench/src, benchmark-lint) (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx vitest run --config packages/lifeops-bench/vitest.config.ts --root packages/lifeops-bench --passWithNoTests, benchmark-tests) (push) Has been cancelled
Build Agent Image / build-and-push (push) Has been cancelled
Dev Smoke / bun run dev onboarding chat (push) Has been cancelled
Dev Smoke / Vite HMR dependency-level smoke (push) Has been cancelled
Electrobun Submodule Guard / electrobun gitlink is fetchable (push) Has been cancelled
Publish @elizaos/example-code / check_npm (push) Has been cancelled
Publish @elizaos/example-code / publish_npm (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / verify_version (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / publish_npm (push) Has been cancelled
Sandbox Live Smoke / Sandbox live smoke (push) Has been cancelled
Snap Build & Test / Build Snap (amd64) (push) Has been cancelled
Snap Build & Test / Build Snap (arm64) (push) Has been cancelled
Test Packaging / elizaos CLI global-install smoke (node + bun) (push) Has been cancelled
Cloud Gateway Webhook / Test (push) Has been cancelled
Cloud Tests / lint-and-types (push) Has been cancelled
Cloud Tests / unit-tests (push) Has been cancelled
Cloud Tests / integration-tests (push) Has been cancelled
Cloud Tests / e2e-tests (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Apps Worker (Product 2) / Determine environment (push) Has been cancelled
Deploy Apps Worker (Product 2) / Deploy apps worker to apps-control host (${{ needs.determine-env.outputs.environment }}) (push) Has been cancelled
Deploy Eliza Provisioning Worker / Determine environment (push) Has been cancelled
Deploy Eliza Provisioning Worker / Deploy worker to Hetzner host (${{ needs.determine-env.outputs.environment }} @ ${{ needs.determine-env.outputs.deployment_sha }}) (push) Has been cancelled
Dev Smoke / Classify changed paths (push) Has been cancelled
supply-chain / sbom (push) Has been cancelled
supply-chain / vulnerability-scan (push) Has been cancelled
Build, Push & Deploy to Phala Cloud / build-and-push (push) Has been cancelled
Test Packaging / Validate Packaging Configs (push) Has been cancelled
Test Packaging / Build & Test PyPI Package (push) Has been cancelled
Test Packaging / PyPI on Python ${{ matrix.python }} (push) Has been cancelled
Test Packaging / Pack & Test JS Tarballs (push) Has been cancelled
UI Fixture E2E / ui-fixture-e2e (push) Has been cancelled
UI Fixture E2E / fixture-e2e (push) Has been cancelled
UI Story Gate / story-gate (push) Has been cancelled
vault-ci / test (macos-latest) (push) Has been cancelled
vault-ci / test (ubuntu-latest) (push) Has been cancelled
vault-ci / test (windows-latest) (push) Has been cancelled
vault-ci / app-core wiring tests (push) Has been cancelled
verify-patches / verify patches/CHECKSUMS.sha256 (push) Has been cancelled
Voice Benchmark Smoke / voice-emotion fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voiceagentbench fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench-quality unit smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench TypeScript unit (no audio) (push) Has been cancelled
Voice Benchmark Smoke / voice bench smoke summary (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/app-core test bun run --cwd packages/elizaos test bun run --cwd packages/cloud/shared test], app-and-cli) (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/scenario-runner test bun run --cwd packages/vault test bun run --cwd packages/security test bun run --cwd plugins/plugin-coding-tools test], framework-packages) (push) Has been cancelled
Windows CI / windows ([bun run --cwd plugins/plugin-elizacloud test bun run --cwd plugins/plugin-discord test bun run --cwd plugins/plugin-anthropic test bun run --cwd plugins/plugin-openai test bun run --cwd plugins/plugin-app-control test bun run --cwd plugins/pl… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run build --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/agent --concurrency=4 node packages/scripts/run-bash-linux-only.mjs scripts/verify-riscv64-buildpaths.sh node packages/scripts/run… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run typecheck --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/cloud-shared --concurrency=4 bun run --cwd packages/core test bun run --cwd packages/shared test], core-runtime, 75) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:43:05 +08:00

214 lines
6.1 KiB
TypeScript

/**
* Worker-targeted e2e preload.
*
* The Worker-targeted suite expects an already-running Worker
* (typically `wrangler dev` on :8787) and just needs:
*
* 1. Env loaded from .env / .env.local / .env.test.
* 2. Local Postgres seeded with the test org/user/api-key, which exports
* TEST_API_KEY into process.env.
*
* Run with: bun test --preload packages/cloud/api/test/e2e/preload.ts \
* packages/cloud/api/test/e2e
*/
import { createHash } from "node:crypto";
import { resolve } from "node:path";
import { dbWrite } from "@elizaos/cloud-shared/db/helpers";
import { agentSandboxesRepository } from "@elizaos/cloud-shared/db/repositories/agent-sandboxes";
import { usersRepository } from "@elizaos/cloud-shared/db/repositories/users";
import { organizations } from "@elizaos/cloud-shared/db/schemas/organizations";
import { users } from "@elizaos/cloud-shared/db/schemas/users";
import { apiKeysService } from "@elizaos/cloud-shared/lib/services/api-keys";
import { config } from "dotenv";
import { eq } from "drizzle-orm";
import { privateKeyToAccount } from "viem/accounts";
import { ensureFixtureSandbox } from "./fixture-sandbox";
for (const envPath of [
resolve("../../.env"),
resolve("../../.env.local"),
resolve("../../.env.test"),
]) {
config({ path: envPath });
}
const DEFAULT_TEST_SECRETS_MASTER_KEY =
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
function testWalletAddress(seed: string): string {
const digest = createHash("sha256")
.update(`playwright:${seed}`)
.digest("hex");
return privateKeyToAccount(`0x${digest}`).address;
}
if (!process.env.SECRETS_MASTER_KEY) {
process.env.SECRETS_MASTER_KEY = DEFAULT_TEST_SECRETS_MASTER_KEY;
}
process.env.CRON_SECRET ??= "test-cron-secret";
process.env.INTERNAL_SECRET ??= "test-internal-secret";
process.env.AGENT_TEST_BOOTSTRAP_ADMIN ??= "true";
process.env.PLAYWRIGHT_TEST_AUTH ??= "true";
process.env.PLAYWRIGHT_TEST_AUTH_SECRET ??= "playwright-local-auth-secret";
process.env.PAYOUT_STATUS_SKIP_LIVE_BALANCE ??= "1";
if (process.env.SKIP_DB_DEPENDENT === "1") {
throw new Error(
"Worker e2e requires a bootstrapped test database; unset SKIP_DB_DEPENDENT.",
);
}
async function ensureTestUser({
slug,
email,
stewardUserId,
role = "admin",
}: {
slug: string;
email: string;
stewardUserId: string;
role?: string;
}) {
const walletAddress = testWalletAddress(stewardUserId);
const existingOrg = await dbWrite.query.organizations.findFirst({
where: eq(organizations.slug, slug),
});
const organization =
existingOrg ??
(
await dbWrite
.insert(organizations)
.values({
name: slug,
slug,
billing_email: email,
credit_balance: "1000.000000",
})
.returning()
)[0];
const existingUser = await dbWrite.query.users.findFirst({
where: eq(users.steward_user_id, stewardUserId),
});
const user =
existingUser ??
(
await dbWrite
.insert(users)
.values({
email,
email_verified: true,
name: slug,
organization_id: organization.id,
role,
steward_user_id: stewardUserId,
wallet_address: walletAddress,
wallet_chain_type: "evm",
wallet_verified: true,
})
.returning()
)[0];
await dbWrite
.update(organizations)
.set({
credit_balance: "1000.000000",
is_active: true,
updated_at: new Date(),
})
.where(eq(organizations.id, organization.id));
await dbWrite
.update(users)
.set({
email,
organization_id: organization.id,
role,
wallet_address: walletAddress,
wallet_chain_type: "evm",
wallet_verified: true,
is_active: true,
updated_at: new Date(),
})
.where(eq(users.id, user.id));
await usersRepository.upsertStewardIdentity(user.id, stewardUserId);
const sandboxes = await agentSandboxesRepository.listByOrganization(
organization.id,
);
await ensureFixtureSandbox({
slug,
organizationId: organization.id,
userId: user.id,
sandboxes,
repository: agentSandboxesRepository,
});
await apiKeysService.deactivateUserKeysByName(user.id, "playwright-e2e");
const { plainKey } = await apiKeysService.create({
name: "playwright-e2e",
description: "Local cloud API e2e test key",
organization_id: organization.id,
user_id: user.id,
rate_limit: 10_000,
is_active: true,
});
return { organization, user, plainKey };
}
const owner = await ensureTestUser({
slug: "playwright-e2e-org",
email: "playwright-owner@elizalabs.ai",
stewardUserId: "playwright-e2e-owner",
role: "admin",
});
const member = await ensureTestUser({
slug: "playwright-e2e-member-org",
email: "playwright-member@example.test",
stewardUserId: "playwright-e2e-member",
role: "member",
});
const affiliate = await ensureTestUser({
slug: "playwright-e2e-affiliate-org",
email: "playwright-affiliate@example.test",
stewardUserId: "playwright-e2e-affiliate",
role: "admin",
});
await apiKeysService.deactivateUserKeysByName(
affiliate.user.id,
"playwright-e2e-affiliate",
);
const { plainKey: affiliatePlainKey } = await apiKeysService.create({
name: "playwright-e2e-affiliate",
description: "Local cloud API affiliate e2e test key",
organization_id: affiliate.organization.id,
user_id: affiliate.user.id,
rate_limit: 10_000,
is_active: true,
});
process.env.TEST_API_KEY = owner.plainKey;
process.env.TEST_MEMBER_API_KEY = member.plainKey;
process.env.TEST_AFFILIATE_API_KEY = affiliatePlainKey;
process.env.TEST_USER_ID = owner.user.id;
process.env.TEST_USER_EMAIL =
owner.user.email ?? "playwright-owner@example.test";
process.env.TEST_ORGANIZATION_ID = owner.organization.id;
if (process.env.REQUIRE_E2E_SERVER !== "0") {
const baseUrl =
process.env.TEST_API_BASE_URL?.trim() ||
process.env.TEST_BASE_URL?.trim() ||
"http://localhost:8787";
const response = await fetch(`${baseUrl}/api/health`, {
signal: AbortSignal.timeout(10_000),
});
if (!response.ok) {
throw new Error(
`Worker e2e target is not healthy: GET ${baseUrl}/api/health -> ${response.status}`,
);
}
}