|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | + |
| 3 | +test.skip( |
| 4 | + Boolean(process.env.CLOUD_E2E_LIVE_URL), |
| 5 | + "API Explorer flow uses local test auth and API stubs; skipped in live-prod mode", |
| 6 | +); |
| 7 | + |
| 8 | +const EXPLORER_KEY = "eliza_test_explorer_123"; |
| 9 | + |
| 10 | +test.beforeEach(async ({ context, page, browserName }) => { |
| 11 | + await context.addCookies([ |
| 12 | + { |
| 13 | + name: "eliza-test-auth", |
| 14 | + value: "1", |
| 15 | + domain: "127.0.0.1", |
| 16 | + path: "/", |
| 17 | + httpOnly: false, |
| 18 | + secure: false, |
| 19 | + sameSite: "Lax", |
| 20 | + }, |
| 21 | + ]); |
| 22 | + |
| 23 | + if (browserName === "chromium") { |
| 24 | + const origin = new URL( |
| 25 | + process.env.PLAYWRIGHT_BASE_URL || "http://127.0.0.1:4173", |
| 26 | + ).origin; |
| 27 | + await context.grantPermissions(["clipboard-read", "clipboard-write"], { |
| 28 | + origin, |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + await page.route("**/api/v1/api-keys/explorer", (route) => |
| 33 | + route.fulfill({ |
| 34 | + json: { |
| 35 | + apiKey: { |
| 36 | + id: "explorer_key_1", |
| 37 | + name: "API Explorer", |
| 38 | + description: "Generated for API Explorer tests", |
| 39 | + key_prefix: "eliza_test_explorer", |
| 40 | + key: EXPLORER_KEY, |
| 41 | + created_at: new Date().toISOString(), |
| 42 | + is_active: true, |
| 43 | + usage_count: 7, |
| 44 | + last_used_at: null, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }), |
| 48 | + ); |
| 49 | + |
| 50 | + await page.route("**/api/v1/pricing/summary", (route) => |
| 51 | + route.fulfill({ |
| 52 | + json: { |
| 53 | + pricing: { |
| 54 | + "chat-completions": { |
| 55 | + cost: 0.0025, |
| 56 | + unit: "1k tokens", |
| 57 | + description: "Input tokens", |
| 58 | + isVariable: true, |
| 59 | + estimatedRange: { min: 0.001, max: 0.03 }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }), |
| 64 | + ); |
| 65 | +}); |
| 66 | + |
| 67 | +test("api explorer: search, auth, request tester, response, and OpenAPI export", async ({ |
| 68 | + page, |
| 69 | +}) => { |
| 70 | + let chatRequest: |
| 71 | + | { |
| 72 | + authorization: string | null; |
| 73 | + body: unknown; |
| 74 | + } |
| 75 | + | undefined; |
| 76 | + |
| 77 | + await page.route("**/api/v1/chat", async (route) => { |
| 78 | + chatRequest = { |
| 79 | + authorization: route.request().headers().authorization ?? null, |
| 80 | + body: route.request().postDataJSON(), |
| 81 | + }; |
| 82 | + await route.fulfill({ |
| 83 | + status: 200, |
| 84 | + contentType: "application/json", |
| 85 | + json: { |
| 86 | + id: "chatcmpl_playwright", |
| 87 | + model: "gpt-oss-120b", |
| 88 | + choices: [ |
| 89 | + { |
| 90 | + message: { |
| 91 | + role: "assistant", |
| 92 | + content: "API Explorer request accepted", |
| 93 | + }, |
| 94 | + }, |
| 95 | + ], |
| 96 | + }, |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + await page.goto("/dashboard/api-explorer"); |
| 101 | + await expect(page).not.toHaveURL(/\/login/); |
| 102 | + await expect( |
| 103 | + page.getByRole("button", { name: /Endpoints/i }), |
| 104 | + ).toBeVisible(); |
| 105 | + |
| 106 | + await page.getByPlaceholder("Search...").fill("chat completion"); |
| 107 | + await expect(page.getByText(/1 endpoint matching/i)).toBeVisible(); |
| 108 | + await expect( |
| 109 | + page.getByRole("button", { name: /Chat Completion/i }), |
| 110 | + ).toBeVisible(); |
| 111 | + |
| 112 | + await page.getByRole("button", { name: /AI Completions/i }).click(); |
| 113 | + await expect(page.getByRole("heading", { name: "AI Completions" })).toBeVisible(); |
| 114 | + |
| 115 | + await page.getByRole("button", { name: /Chat Completion/i }).click(); |
| 116 | + await expect( |
| 117 | + page.getByRole("button", { name: /Back to endpoints/i }), |
| 118 | + ).toBeVisible(); |
| 119 | + await expect(page.getByRole("heading", { name: "Chat Completion" })).toBeVisible(); |
| 120 | + |
| 121 | + const messages = page.locator("#param-messages"); |
| 122 | + await expect(messages).toBeVisible(); |
| 123 | + await messages.fill( |
| 124 | + JSON.stringify([ |
| 125 | + { |
| 126 | + role: "user", |
| 127 | + parts: [{ type: "text", text: "Say hello from Playwright" }], |
| 128 | + }, |
| 129 | + ]), |
| 130 | + ); |
| 131 | + |
| 132 | + await page.getByRole("button", { name: /Copy cURL/i }).click(); |
| 133 | + await expect |
| 134 | + .poll(() => page.evaluate(() => navigator.clipboard.readText())) |
| 135 | + .toContain("/api/v1/chat"); |
| 136 | + |
| 137 | + await page.getByRole("button", { name: /Send Request/i }).click(); |
| 138 | + await expect(page.getByRole("tab", { name: /Response 200/i })).toBeVisible({ |
| 139 | + timeout: 10_000, |
| 140 | + }); |
| 141 | + await expect(page.getByText("API Explorer request accepted")).toBeVisible(); |
| 142 | + expect(chatRequest?.authorization).toBe(`Bearer ${EXPLORER_KEY}`); |
| 143 | + expect(chatRequest?.body).toMatchObject({ |
| 144 | + messages: [ |
| 145 | + { |
| 146 | + role: "user", |
| 147 | + parts: [{ type: "text", text: "Say hello from Playwright" }], |
| 148 | + }, |
| 149 | + ], |
| 150 | + }); |
| 151 | + |
| 152 | + await page.getByRole("button", { name: /^Auth$/i }).click(); |
| 153 | + const apiKeyInput = page.locator("#auth-manager-api-key"); |
| 154 | + await expect(apiKeyInput).toHaveAttribute("type", "password"); |
| 155 | + await expect(apiKeyInput).toHaveValue(EXPLORER_KEY); |
| 156 | + await page.locator("#auth-manager-api-key + button").click(); |
| 157 | + await expect(apiKeyInput).toHaveAttribute("type", "text"); |
| 158 | + |
| 159 | + await page.getByText("Use a different key").click(); |
| 160 | + await page.getByPlaceholder("Enter custom API key...").fill("sk-custom-test"); |
| 161 | + await expect(apiKeyInput).toHaveValue("sk-custom-test"); |
| 162 | + await page.getByRole("button", { name: /Reset to default/i }).click(); |
| 163 | + await expect(apiKeyInput).toHaveValue(EXPLORER_KEY); |
| 164 | + |
| 165 | + await page.getByRole("button", { name: /^OpenAPI$/i }).click(); |
| 166 | + await expect( |
| 167 | + page.getByRole("heading", { name: "OpenAPI 3.0 Specification" }), |
| 168 | + ).toBeVisible(); |
| 169 | + |
| 170 | + await page.getByRole("button", { name: /^JSON$/i }).click(); |
| 171 | + await expect |
| 172 | + .poll(() => page.evaluate(() => navigator.clipboard.readText())) |
| 173 | + .toContain('"openapi"'); |
| 174 | + |
| 175 | + await page.getByRole("button", { name: /^YAML$/i }).click(); |
| 176 | + await expect |
| 177 | + .poll(() => page.evaluate(() => navigator.clipboard.readText())) |
| 178 | + .toContain("openapi:"); |
| 179 | +}); |
0 commit comments