Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(worker): improve test isolation for StorageService dependent tests (#11339)

Michael Chinn9c799dfb3344
diff --git a/worker/src/__tests__/blobStorageIntegrationProcessing.test.ts b/worker/src/__tests__/blobStorageIntegrationProcessing.test.ts
index d46f9df2a0..c2abf86767 100644
--- a/worker/src/__tests__/blobStorageIntegrationProcessing.test.ts
+++ b/worker/src/__tests__/blobStorageIntegrationProcessing.test.ts
@@ -1,4 +1,4 @@
-import { expect, it, describe, beforeAll } from "vitest";
+import { expect, it, describe, beforeAll, beforeEach, afterEach } from "vitest";
import { env } from "../env";
import { randomUUID } from "crypto";
import {
@@ -30,6 +30,7 @@ const maybeIt = env.LANGFUSE_USE_AZURE_BLOB === "true" ? it.skip : it;
describe("BlobStorageIntegrationProcessingJob", () => {
let storageService: StorageService;
let s3StorageService: StorageService;
+ let s3Prefix: string | null = null;
const bucketName = env.LANGFUSE_S3_EVENT_UPLOAD_BUCKET || "";
const accessKeyId = env.LANGFUSE_S3_EVENT_UPLOAD_ACCESS_KEY_ID || "";
const secretAccessKey = env.LANGFUSE_S3_EVENT_UPLOAD_SECRET_ACCESS_KEY || "";
@@ -59,8 +60,21 @@ describe("BlobStorageIntegrationProcessingJob", () => {
});
});
+ afterEach(async () => {
+ // Clean up all files created during this test
+ if (!s3Prefix) return;
+
+ const files = await s3StorageService.listFiles(s3Prefix);
+
+ if (files.length == 0) return;
+
+ await s3StorageService.deleteFiles(files.map((f) => f.file));
+ s3Prefix = null;
+ });
+
it("should not process when blob storage integration is disabled", async () => {
const { projectId } = await createOrgProjectAndApiKey();
+ s3Prefix = projectId;
// Setup an integration but disabled
await prisma.blobStorageIntegration.create({
@@ -68,7 +82,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
projectId,
type: BlobStorageIntegrationType.S3,
bucketName,
- prefix: "",
+ prefix: s3Prefix,
accessKeyId,
secretAccessKey: encrypt(secretAccessKey),
region: region ? region : "auto",
@@ -86,13 +100,14 @@ describe("BlobStorageIntegrationProcessingJob", () => {
} as Job);
// Then
- const files = await storageService.listFiles("");
+ const files = await storageService.listFiles(s3Prefix);
expect(files.filter((f) => f.file.includes(projectId))).toHaveLength(0);
});
it("should export traces, generations, and scores to S3", async () => {
// Setup
const { projectId } = await createOrgProjectAndApiKey();
+ s3Prefix = projectId;
const now = new Date();
// Set lastSyncAt to 2 hours ago so the chunked export (1 hour window) covers recent data
const twoHoursAgo = new Date(now.getTime() - 2 * 60 * 60 * 1000);
@@ -103,7 +118,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
projectId,
type: BlobStorageIntegrationType.S3,
bucketName,
- prefix: "",
+ prefix: s3Prefix,
accessKeyId: minioAccessKeyId,
secretAccessKey: encrypt(minioAccessKeySecret),
region: region ? region : "auto",
@@ -161,7 +176,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
} as Job);
// Then
- const files = await s3StorageService.listFiles("");
+ const files = await s3StorageService.listFiles(s3Prefix);
const projectFiles = files.filter((f) => f.file.includes(projectId));
// Should have 3 files (traces, observations, scores)
@@ -281,7 +296,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
it("should use prefix in file path when specified", async () => {
// Setup
const { projectId } = await createOrgProjectAndApiKey();
- const prefix = "test-prefix";
+ s3Prefix = "test-prefix";
const now = new Date();
const oneHourAgo = new Date(now.getTime() - 60 * 60 * 1000);
@@ -290,7 +305,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
projectId,
type: BlobStorageIntegrationType.S3,
bucketName,
- prefix,
+ prefix: s3Prefix,
accessKeyId: minioAccessKeyId,
secretAccessKey: encrypt(minioAccessKeySecret),
region: region ? region : "auto",
@@ -319,16 +334,17 @@ describe("BlobStorageIntegrationProcessingJob", () => {
} as Job);
// Then
- const files = await storageService.listFiles("");
+ const files = await storageService.listFiles(s3Prefix);
const projectFiles = files.filter((f) => f.file.includes(projectId));
// All files should have the prefix
- expect(projectFiles.every((f) => f.file.startsWith(prefix))).toBe(true);
+ expect(projectFiles.every((f) => f.file.startsWith(s3Prefix))).toBe(true);
});
it("should handle CSV, JSON, and JSONL file types correctly", async () => {
// Setup
const { projectId } = await createOrgProjectAndApiKey();
+ s3Prefix = `${projectId}/`;
const now = new Date();
const oneHourAgo = new Date(now.getTime() - 60 * 60 * 1000);
@@ -373,11 +389,14 @@ describe("BlobStorageIntegrationProcessingJob", () => {
BlobStorageIntegrationFileType.JSON,
BlobStorageIntegrationFileType.JSONL,
]) {
+ const fileTypePrefix = `${fileType.toLowerCase()}-test/`;
+ const prefix = `${s3Prefix}${fileTypePrefix}`;
+
// Create integration with specific file type
await prisma.blobStorageIntegration.upsert({
where: { projectId },
update: {
- prefix: `${fileType.toLowerCase()}-test/`,
+ prefix,
fileType,
lastSyncAt: oneHourAgo,
},
@@ -385,7 +404,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
projectId,
type: BlobStorageIntegrationType.S3,
bucketName,
- prefix: `${fileType.toLowerCase()}-test/`,
+ prefix,
accessKeyId: minioAccessKeyId,
secretAccessKey: encrypt(minioAccessKeySecret),
region: region ? region : "auto",
@@ -405,11 +424,9 @@ describe("BlobStorageIntegrationProcessingJob", () => {
} as Job);
// Get files for this file type
- const files = await s3StorageService.listFiles("");
+ const files = await s3StorageService.listFiles(prefix);
const projectFiles = files.filter(
- (f) =>
- f.file.includes(projectId) &&
- f.file.includes(`${fileType.toLowerCase()}-test/`),
+ (f) => f.file.includes(projectId) && f.file.includes(fileTypePrefix),
);
// Should have 3 files (traces, observations, scores)
@@ -467,6 +484,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
"should export old data for FULL_HISTORY mode when data exists",
async () => {
const { projectId } = await createOrgProjectAndApiKey();
+ s3Prefix = projectId;
// Create trace with old timestamp that's far enough in the past
// but not so old that it might not be found by ClickHouse
@@ -485,7 +503,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
projectId,
type: BlobStorageIntegrationType.S3,
bucketName,
- prefix: `${projectId}/test-full-history/`,
+ prefix: s3Prefix,
accessKeyId,
secretAccessKey: encrypt(secretAccessKey),
region: region ? region : "auto",
@@ -505,9 +523,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
} as Job);
// If data was found and exported, check the files
- const files = await storageService.listFiles(
- `${projectId}/test-full-history/`,
- );
+ const files = await storageService.listFiles(s3Prefix);
const projectFiles = files.filter((f) => f.file.includes(projectId));
// With FULL_HISTORY mode, if the ClickHouse query finds the old data,
@@ -539,6 +555,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
it("should use current date for FROM_TODAY mode on first export", async () => {
const { projectId } = await createOrgProjectAndApiKey();
+ s3Prefix = projectId;
const now = new Date();
const yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000);
const veryOldTrace = new Date(now.getTime() - 2 * 24 * 60 * 60 * 1000); // 2 days ago
@@ -562,7 +579,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
projectId,
type: BlobStorageIntegrationType.S3,
bucketName,
- prefix: `${projectId}/test-today/`,
+ prefix: s3Prefix,
accessKeyId,
secretAccessKey: encrypt(secretAccessKey),
region: region ? region : "auto",
@@ -581,7 +598,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
data: { payload: { projectId } },
} as Job);
- const files = await storageService.listFiles(`${projectId}/test-today/`);
+ const files = await storageService.listFiles(s3Prefix);
const projectFiles = files.filter((f) => f.file.includes(projectId));
const traceFile = projectFiles.find((f) => f.file.includes("/traces/"));
@@ -598,6 +615,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
it("should use custom date for FROM_CUSTOM_DATE mode on first export", async () => {
const { projectId } = await createOrgProjectAndApiKey();
+ s3Prefix = projectId;
const now = new Date();
const customDate = new Date(now.getTime() - 12 * 60 * 60 * 1000); // 12 hours ago
const beforeCustomDate = new Date(customDate.getTime() - 60 * 60 * 1000); // 13 hours ago
@@ -624,7 +642,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
projectId,
type: BlobStorageIntegrationType.S3,
bucketName,
- prefix: `${projectId}/test-custom/`,
+ prefix: s3Prefix,
accessKeyId: minioAccessKeyId,
secretAccessKey: encrypt(minioAccessKeySecret),
region: region ? region : "auto",
@@ -643,9 +661,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
data: { payload: { projectId } },
} as Job);
- const files = await s3StorageService.listFiles(
- `${projectId}/test-custom/`,
- );
+ const files = await s3StorageService.listFiles(s3Prefix);
const projectFiles = files.filter((f) => f.file.includes(projectId));
const traceFile = projectFiles.find((f) => f.file.includes("/traces/"));
@@ -665,6 +681,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
"should cap maxTimestamp to one frequency period ahead for FULL_HISTORY mode",
async () => {
const { projectId } = await createOrgProjectAndApiKey();
+ s3Prefix = projectId;
const now = new Date();
const veryOldTimestamp = new Date(
now.getTime() - 7 * 24 * 60 * 60 * 1000,
@@ -684,7 +701,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
projectId,
type: BlobStorageIntegrationType.S3,
bucketName,
- prefix: `${projectId}/test-chunking/`,
+ prefix: s3Prefix,
accessKeyId,
secretAccessKey: encrypt(secretAccessKey),
region: region ? region : "auto",
@@ -713,9 +730,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
expect(updatedIntegration).toBeDefined();
// Check if files were exported (meaning data was found)
- const files = await storageService.listFiles(
- `${projectId}/test-chunking/`,
- );
+ const files = await storageService.listFiles(s3Prefix);
const projectFiles = files.filter((f) => f.file.includes(projectId));
// If data was found and exported, verify chunking behavior
@@ -741,6 +756,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
it("should immediately schedule next chunk when in catch-up mode", async () => {
const { projectId } = await createOrgProjectAndApiKey();
+ s3Prefix = projectId;
const now = new Date();
const twoDaysAgo = new Date(now.getTime() - 2 * 24 * 60 * 60 * 1000);
@@ -763,7 +779,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
projectId,
type: BlobStorageIntegrationType.S3,
bucketName,
- prefix: `${projectId}/test-catchup/`,
+ prefix: s3Prefix,
accessKeyId: minioAccessKeyId,
secretAccessKey: encrypt(minioAccessKeySecret),
region: region ? region : "auto",
@@ -802,6 +818,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
it("should schedule normally when caught up", async () => {
const { projectId } = await createOrgProjectAndApiKey();
+ s3Prefix = projectId;
const now = new Date();
const oneHourAgo = new Date(now.getTime() - 60 * 60 * 1000);
@@ -819,7 +836,7 @@ describe("BlobStorageIntegrationProcessingJob", () => {
projectId,
type: BlobStorageIntegrationType.S3,
bucketName,
- prefix: `${projectId}/test-caught-up/`,
+ prefix: s3Prefix,
accessKeyId: minioAccessKeyId,
secretAccessKey: encrypt(minioAccessKeySecret),
region: region ? region : "auto",
diff --git a/worker/src/__tests__/dataRetentionProcessing.test.ts b/worker/src/__tests__/dataRetentionProcessing.test.ts
index 6f30eff3f9..25004dbba1 100644
--- a/worker/src/__tests__/dataRetentionProcessing.test.ts
+++ b/worker/src/__tests__/dataRetentionProcessing.test.ts
@@ -1,4 +1,4 @@
-import { expect, it, describe, beforeAll } from "vitest";
+import { expect, it, describe, beforeAll, beforeEach, afterEach } from "vitest";
import { env } from "../env";
import { randomUUID } from "crypto";
import {
@@ -22,6 +22,7 @@ import { Job } from "bullmq";
describe("DataRetentionProcessingJob", () => {
let storageService: StorageService;
+ let s3Prefix: string | null = null;
const projectId = "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a";
beforeAll(() => {
@@ -35,10 +36,26 @@ describe("DataRetentionProcessingJob", () => {
});
});
+ beforeEach(() => {
+ s3Prefix = `${randomUUID()}/`;
+ });
+
+ afterEach(async () => {
+ // Clean up all files created during this test
+ if (!s3Prefix) return;
+
+ const files = await storageService.listFiles(s3Prefix);
+
+ if (files.length == 0) return;
+
+ await storageService.deleteFiles(files.map((f) => f.file));
+ s3Prefix = null;
+ });
+
it("should NOT delete event files from cloud storage if after expiry cutoff", async () => {
// Setup
const baseId = randomUUID();
- const fileName = `${baseId}.json`;
+ const fileName = `${s3Prefix}${baseId}.json`;
const fileType = "application/json";
const data = JSON.stringify({ hello: "world" });
@@ -72,7 +89,7 @@ describe("DataRetentionProcessingJob", () => {
} as Job);
// Then
- const files = await storageService.listFiles("");
+ const files = await storageService.listFiles(s3Prefix);
expect(files.map((file) => file.file)).toContain(fileName);
const eventLogRecord = await getBlobStorageByProjectAndEntityId(
@@ -86,7 +103,7 @@ describe("DataRetentionProcessingJob", () => {
it("should delete event files from cloud storage if expired", async () => {
// Setup
const baseId = randomUUID();
- const fileName = `${baseId}.json`;
+ const fileName = `${s3Prefix}${baseId}.json`;
const fileType = "application/json";
const data = JSON.stringify({ hello: "world" });
@@ -120,7 +137,7 @@ describe("DataRetentionProcessingJob", () => {
} as Job);
// Then
- const files = await storageService.listFiles("");
+ const files = await storageService.listFiles(s3Prefix);
expect(files.map((file) => file.file)).not.toContain(fileName);
const eventLogRecord = await getBlobStorageByProjectAndEntityId(
@@ -133,7 +150,7 @@ describe("DataRetentionProcessingJob", () => {
it("should NOT delete media files from cloud storage and database if after expiry cutoff", async () => {
// Setup
- const fileName = `${randomUUID()}.txt`;
+ const fileName = `${s3Prefix}${randomUUID()}.txt`;
const fileType = "text/plain";
const data = "Hello, world!";
@@ -174,7 +191,7 @@ describe("DataRetentionProcessingJob", () => {
} as Job);
// Then
- const files = await storageService.listFiles("");
+ const files = await storageService.listFiles(s3Prefix);
expect(files.map((file) => file.file)).toContain(fileName);
const media = await prisma.media.findUnique({
@@ -190,7 +207,7 @@ describe("DataRetentionProcessingJob", () => {
it("should delete media files from cloud storage and database if expired", async () => {
// Setup
- const fileName = `${randomUUID()}.txt`;
+ const fileName = `${s3Prefix}${randomUUID()}.txt`;
const fileType = "text/plain";
const data = "Hello, world!";
@@ -231,7 +248,7 @@ describe("DataRetentionProcessingJob", () => {
} as Job);
// Then
- const files = await storageService.listFiles("");
+ const files = await storageService.listFiles(s3Prefix);
expect(files.map((file) => file.file)).not.toContain(fileName);
const media = await prisma.media.findUnique({
diff --git a/worker/src/__tests__/projectDeletionProcessing.test.ts b/worker/src/__tests__/projectDeletionProcessing.test.ts
index ee6760dbab..55d40cf208 100644
--- a/worker/src/__tests__/projectDeletionProcessing.test.ts
+++ b/worker/src/__tests__/projectDeletionProcessing.test.ts
@@ -1,4 +1,4 @@
-import { expect, it, describe, beforeAll } from "vitest";
+import { expect, it, describe, beforeAll, beforeEach, afterEach } from "vitest";
import { env } from "../env";
import { randomUUID } from "crypto";
import {
@@ -23,6 +23,7 @@ import { projectDeleteProcessor } from "../queues/projectDelete";
describe("ProjectDeletionProcessingJob", () => {
let storageService: StorageService;
+ let s3Prefix: string | null = null;
const orgId = "seed-org-id";
beforeAll(() => {
@@ -36,6 +37,18 @@ describe("ProjectDeletionProcessingJob", () => {
});
});
+ afterEach(async () => {
+ // Clean up all files created during this test
+ if (!s3Prefix) return;
+
+ const files = await storageService.listFiles(s3Prefix);
+
+ if (files.length == 0) return;
+
+ await storageService.deleteFiles(files.map((f) => f.file));
+ s3Prefix = null;
+ });
+
it("should delete the project record after processing has completed", async () => {
// Setup
const projectId = randomUUID();
@@ -193,6 +206,7 @@ describe("ProjectDeletionProcessingJob", () => {
it("should delete all media assets for the project", async () => {
// Setup
const projectId = randomUUID();
+ s3Prefix = `${randomUUID()}/`;
await prisma.project.create({
data: {
id: projectId,
@@ -201,7 +215,7 @@ describe("ProjectDeletionProcessingJob", () => {
},
});
- const fileName = `${randomUUID()}.txt`;
+ const fileName = `${s3Prefix}${randomUUID()}.txt`;
const fileType = "text/plain";
const data = "Hello, world!";
@@ -242,7 +256,7 @@ describe("ProjectDeletionProcessingJob", () => {
} as Job);
// Then
- const files = await storageService.listFiles("");
+ const files = await storageService.listFiles(s3Prefix);
expect(files.map((file) => file.file)).not.toContain(fileName);
const media = await prisma.media.findUnique({
diff --git a/worker/src/__tests__/storageservice.test.ts b/worker/src/__tests__/storageservice.test.ts
index f27206e88d..1e938dd52a 100644
--- a/worker/src/__tests__/storageservice.test.ts
+++ b/worker/src/__tests__/storageservice.test.ts
@@ -1,4 +1,11 @@
-import { expect, test, describe, beforeAll } from "vitest";
+import {
+ expect,
+ test,
+ describe,
+ beforeAll,
+ beforeEach,
+ afterEach,
+} from "vitest";
import { env } from "../env";
import { randomUUID } from "crypto";
import {
@@ -11,6 +18,7 @@ const { Readable } = require("stream");
describe("StorageService", () => {
let storageService: StorageService;
let storageServiceWithExternalEndpoint: StorageService;
+ let s3Prefix: string;
const baseUrl = `${env.LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT}/${env.LANGFUSE_S3_EVENT_UPLOAD_BUCKET}`;
const externalEndpoint = "https://external-endpoint.example.com";
@@ -35,9 +43,21 @@ describe("StorageService", () => {
});
});
+ beforeEach(() => {
+ s3Prefix = `${randomUUID()}/`;
+ });
+
+ afterEach(async () => {
+ const files = await storageService.listFiles(s3Prefix);
+
+ if (files.length == 0) return;
+
+ await storageService.deleteFiles(files.map((f) => f.file));
+ });
+
test("uploadWithSignedUrl should upload a file and return a signed URL", async () => {
// Setup
- const fileName = `${randomUUID()}.txt`;
+ const fileName = `${s3Prefix}${randomUUID()}.txt`;
const fileType = "text/plain";
const data = "Hello, world!";
const expiresInSeconds = 3600;
@@ -58,7 +78,7 @@ describe("StorageService", () => {
test("uploadJson should upload a JSON file", async () => {
// Setup
- const fileName = `${randomUUID()}.json`;
+ const fileName = `${s3Prefix}${randomUUID()}.json`;
const data = [{ hello: "world" }];
const expiresInSeconds = 3600;
@@ -72,13 +92,13 @@ describe("StorageService", () => {
test("listFiles should list files in the bucket", async () => {
// Setup
- const fileName1 = `${randomUUID()}.txt`;
- const fileName2 = `${randomUUID()}.txt`;
+ const fileName1 = `${s3Prefix}${randomUUID()}.txt`;
+ const fileName2 = `${s3Prefix}${randomUUID()}.txt`;
await storageService.uploadJson(fileName1, [{ hello: "world" }]);
await storageService.uploadJson(fileName2, [{ hello: "world" }]);
// When
- const files = await storageService.listFiles("");
+ const files = await storageService.listFiles(s3Prefix);
// Then
const fileNames = files.map((f) => f.file);
@@ -97,14 +117,14 @@ describe("StorageService", () => {
await storageService.deleteFiles([fileName1]);
// Then
- const files = await storageService.listFiles("");
+ const files = await storageService.listFiles(s3Prefix);
const fileNames = files.map((f) => f.file);
expect(fileNames).not.toContain(fileName1);
});
test("uploadFile should successfully process a Readable entity", async () => {
// Setup
- const fileName = `${randomUUID()}.txt`;
+ const fileName = `${s3Prefix}${randomUUID()}.txt`;
const fileType = "text/plain";
const data = "Hello, world!";
const expiresInSeconds = 3600;
@@ -129,7 +149,7 @@ describe("StorageService", () => {
test("getSignedUrl should return URL with internal endpoint when no external endpoint is configured", async () => {
// Setup
- const fileName = `${randomUUID()}.txt`;
+ const fileName = `${s3Prefix}${randomUUID()}.txt`;
const fileType = "text/plain";
const data = "Hello, world!";
const expiresInSeconds = 3600;
@@ -154,7 +174,7 @@ describe("StorageService", () => {
test("getSignedUrl should return URL with external endpoint when configured", async () => {
// Setup
- const fileName = `${randomUUID()}.txt`;
+ const fileName = `${s3Prefix}${randomUUID()}.txt`;
const fileType = "text/plain";
const data = "Hello, world!";
const expiresInSeconds = 3600;
@@ -224,7 +244,7 @@ describe("StorageService", () => {
test("uploadWithSignedUrl should return signed URL with external endpoint when configured", async () => {
// Setup
- const fileName = `${randomUUID()}.txt`;
+ const fileName = `${s3Prefix}${randomUUID()}.txt`;
const fileType = "text/plain";
const data = "Hello, external world!";
const expiresInSeconds = 3600;