|
| 1 | +import { Polly, setupMocha as setupPolly } from "@pollyjs/core"; |
| 2 | +import NodeHttpAdapter from "@pollyjs/adapter-node-http"; |
| 3 | +import FetchAdapter from "@pollyjs/adapter-fetch"; |
| 4 | +import FSPersister from "@pollyjs/persister-fs"; |
| 5 | +import * as traceloop from "../src"; |
| 6 | +import * as assert from "assert"; |
| 7 | + |
| 8 | +// Register adapters and persisters |
| 9 | +Polly.register(NodeHttpAdapter); |
| 10 | +Polly.register(FetchAdapter); |
| 11 | +Polly.register(FSPersister); |
| 12 | + |
| 13 | +describe("Experiment Export Tests", () => { |
| 14 | + let polly: Polly; |
| 15 | + let client: traceloop.TraceloopClient; |
| 16 | + let experimentSlug: string; |
| 17 | + let runId: string; |
| 18 | + |
| 19 | + setupPolly({ |
| 20 | + adapters: ["node-http", "fetch"], |
| 21 | + persister: "fs", |
| 22 | + recordIfMissing: process.env.RECORD_MODE === "NEW", |
| 23 | + recordFailedRequests: true, |
| 24 | + mode: process.env.RECORD_MODE === "NEW" ? "record" : "replay", |
| 25 | + matchRequestsBy: { |
| 26 | + headers: false, |
| 27 | + url: { |
| 28 | + protocol: true, |
| 29 | + hostname: true, |
| 30 | + pathname: true, |
| 31 | + query: false, |
| 32 | + }, |
| 33 | + }, |
| 34 | + logging: true, |
| 35 | + }); |
| 36 | + |
| 37 | + before(async function () { |
| 38 | + const apiKey = |
| 39 | + process.env.RECORD_MODE === "NEW" |
| 40 | + ? process.env.TRACELOOP_API_KEY! |
| 41 | + : "test-key"; |
| 42 | + const baseUrl = |
| 43 | + process.env.RECORD_MODE === "NEW" |
| 44 | + ? process.env.TRACELOOP_BASE_URL! |
| 45 | + : "https://api-staging.traceloop.com"; |
| 46 | + |
| 47 | + client = new traceloop.TraceloopClient({ |
| 48 | + appName: "experiment_export_test", |
| 49 | + apiKey, |
| 50 | + baseUrl, |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + beforeEach(function () { |
| 55 | + const { server } = this.polly as Polly; |
| 56 | + server.any().on("beforePersist", (_req, recording) => { |
| 57 | + recording.request.headers = recording.request.headers.filter( |
| 58 | + ({ name }: { name: string }) => |
| 59 | + !["authorization"].includes(name.toLowerCase()), |
| 60 | + ); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe("Export Methods", () => { |
| 65 | + it("should export experiment results as CSV with explicit parameters", async function () { |
| 66 | + // Skip this test unless valid Polly recordings exist |
| 67 | + if (process.env.RECORD_MODE !== "NEW") { |
| 68 | + this.skip(); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // Use known experiment slug and run ID for testing |
| 73 | + experimentSlug = "test-experiment-slug"; |
| 74 | + runId = "test-run-id"; |
| 75 | + |
| 76 | + const csvData = await client.experiment.toCsvString( |
| 77 | + experimentSlug, |
| 78 | + runId, |
| 79 | + ); |
| 80 | + |
| 81 | + assert.ok(csvData); |
| 82 | + assert.strictEqual(typeof csvData, "string"); |
| 83 | + console.log(`✓ Exported CSV data: ${csvData.length} characters`); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should export experiment results as JSON with explicit parameters", async function () { |
| 87 | + // Skip this test unless valid Polly recordings exist |
| 88 | + if (process.env.RECORD_MODE !== "NEW") { |
| 89 | + this.skip(); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + experimentSlug = "test-experiment-slug"; |
| 94 | + runId = "test-run-id"; |
| 95 | + |
| 96 | + const jsonData = await client.experiment.toJsonString( |
| 97 | + experimentSlug, |
| 98 | + runId, |
| 99 | + ); |
| 100 | + |
| 101 | + assert.ok(jsonData); |
| 102 | + assert.strictEqual(typeof jsonData, "string"); |
| 103 | + // Verify it's valid JSON |
| 104 | + JSON.parse(jsonData); |
| 105 | + console.log(`✓ Exported JSON data: ${jsonData.length} characters`); |
| 106 | + }); |
| 107 | + |
| 108 | + it("should throw error when exporting CSV without experiment slug", async function () { |
| 109 | + try { |
| 110 | + await client.experiment.toCsvString(); |
| 111 | + assert.fail("Should have thrown an error"); |
| 112 | + } catch (error) { |
| 113 | + assert.ok(error instanceof Error); |
| 114 | + assert.ok(error.message.includes("experiment_slug is required")); |
| 115 | + console.log("✓ Correctly threw error for missing experiment slug"); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + it("should throw error when exporting JSON without run ID", async function () { |
| 120 | + try { |
| 121 | + await client.experiment.toJsonString("some-slug"); |
| 122 | + assert.fail("Should have thrown an error"); |
| 123 | + } catch (error) { |
| 124 | + assert.ok(error instanceof Error); |
| 125 | + assert.ok(error.message.includes("run_id is required")); |
| 126 | + console.log("✓ Correctly threw error for missing run ID"); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + it("should use last run values when not provided", async function () { |
| 131 | + // This test would require running an actual experiment first |
| 132 | + // For now, we'll just verify the error handling |
| 133 | + try { |
| 134 | + await client.experiment.toCsvString(); |
| 135 | + assert.fail("Should have thrown an error"); |
| 136 | + } catch (error) { |
| 137 | + assert.ok(error instanceof Error); |
| 138 | + // Should fail because no last run exists |
| 139 | + assert.ok( |
| 140 | + error.message.includes("experiment_slug is required") || |
| 141 | + error.message.includes("run_id is required"), |
| 142 | + ); |
| 143 | + console.log("✓ Correctly handled missing last run values"); |
| 144 | + } |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments