🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
import { testFeatureFlags } from "@/src/__tests__/fixtures/feature-flags";
import { disconnectQueues } from "@/src/__tests__/test-utils";
import { appRouter } from "@/src/server/api/root";
import { createInnerTRPCContext } from "@/src/server/api/trpc";
import { prisma, Prisma } from "@langfuse/shared/src/db";
import {
createOrgProjectAndApiKey,
EntityChangeQueue,
} from "@langfuse/shared/src/server";
import { PromptType } from "@langfuse/shared";
import type { Session } from "next-auth";
import { v4 } from "uuid";
import waitForExpect from "wait-for-expect";
import { entitlementAccess } from "@/src/features/entitlements/constants/entitlements";
function createCaller({
projectId,
orgId,
projectName = "Test project",
orgName = "Test organization",
admin = true,
projectRole = "ADMIN",
}: {
projectId: string;
orgId: string;
projectName?: string;
orgName?: string;
admin?: boolean;
projectRole?: "ADMIN" | "MEMBER";
}) {
const session: Session = {
expires: "1",
user: {
id: "user-1",
canCreateOrganizations: true,
name: "Demo User",
organizations: [
{
id: orgId,
name: orgName,
role: "OWNER",
plan: "cloud:hobby",
cloudConfig: undefined,
metadata: {},
aiFeaturesEnabled: false,
aiTelemetryEnabled: false,
projects: [
{
id: projectId,
role: projectRole,
retentionDays: 30,
deletedAt: null,
hasTraces: false,
name: projectName,
metadata: {},
createdAt: new Date().toISOString(),
},
],
},
],
featureFlags: testFeatureFlags(),
admin,
},
environment: {
enableExperimentalFeatures: false,
selfHostedInstancePlan: "cloud:hobby",
},
};
const ctx = createInnerTRPCContext({ session, headers: {} });
const caller = appRouter.createCaller({ ...ctx, prisma });
return { session, ctx, caller };
}
async function prepare() {
const { project, org } = await createOrgProjectAndApiKey();
const { session, ctx, caller } = createCaller({
projectId: project.id,
orgId: org.id,
projectName: project.name,
orgName: org.name,
});
return { project, org, session, ctx, caller };
}
describe("prompts trpc", () => {
afterAll(async () => {
await disconnectQueues();
});
describe("prompts.create", () => {
it("returns CONFLICT when concurrent requests allocate the same prompt version", async () => {
const { project, caller } = await prepare();
const transactionSpy = vi
.spyOn(prisma, "$transaction")
.mockRejectedValueOnce(
new Prisma.PrismaClientKnownRequestError(
"Unique constraint failed on the fields: (`project_id`,`name`,`version`)",
{
code: "P2002",
clientVersion: "test",
meta: { target: ["project_id", "name", "version"] },
},
),
);
try {
await expect(
caller.prompts.create({
projectId: project.id,
name: `concurrent-${v4()}`,
prompt: "test",
type: PromptType.Text,
labels: [],
config: {},
}),
).rejects.toMatchObject({ code: "CONFLICT" });
} finally {
transactionSpy.mockRestore();
}
});
});
describe("prompts.all input validation", () => {
it.each([
{
caseName: "filter",
filter: [
{
column: "bogus_xyz",
type: "string" as const,
operator: "=" as const,
value: "test",
},
],
orderBy: { column: "createdAt", order: "DESC" as const },
},
{
caseName: "order by",
filter: [],
orderBy: { column: "bogus_xyz", order: "DESC" as const },
},
])(
"rejects an invalid $caseName column as BAD_REQUEST",
async ({ filter, orderBy }) => {
const projectId = v4();
const { caller } = createCaller({
projectId,
orgId: v4(),
admin: false,
});
await expect(
caller.prompts.all({
projectId,
page: 0,
limit: 10,
filter,
orderBy,
}),
).rejects.toMatchObject({
code: "BAD_REQUEST",
});
},
);
it("accepts leaked tracing time-column orderBy aliases", async () => {
const { project, caller } = await prepare();
await expect(
caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "startTime", order: "DESC" },
}),
).resolves.toMatchObject({ prompts: [], totalCount: 0 });
});
it("lists prompts when orderBy is null", async () => {
const { project, caller } = await prepare();
await expect(
caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: null,
}),
).resolves.toMatchObject({ prompts: [], totalCount: 0 });
});
});
describe("prompts.importBulk", () => {
it("enforces the prompt count limit for new prompt names", async () => {
const { project, org } = await createOrgProjectAndApiKey();
const existingPromptName = `existing-${v4()}`;
await prisma.prompt.create({
data: {
projectId: project.id,
name: existingPromptName,
version: 1,
type: "text",
prompt: "Version 1",
createdBy: "API",
labels: ["latest"],
},
});
const { caller } = createCaller({
projectId: project.id,
orgId: org.id,
projectName: project.name,
orgName: org.name,
admin: false,
});
const previousLimit =
entitlementAccess["cloud:hobby"].entitlementLimits[
"prompt-management-count-prompts"
];
entitlementAccess["cloud:hobby"].entitlementLimits[
"prompt-management-count-prompts"
] = 1;
try {
const existingPromptResult = await caller.prompts.importBulk({
projectId: project.id,
prompts: [{ name: existingPromptName, prompt: "Version 2" }],
});
expect(existingPromptResult.results).toEqual([
{ name: existingPromptName, success: true },
]);
const newPromptName = `new-${v4()}`;
await expect(
caller.prompts.importBulk({
projectId: project.id,
prompts: [{ name: newPromptName, prompt: "Version 1" }],
}),
).rejects.toMatchObject({ code: "FORBIDDEN" });
await expect(
prisma.prompt.findFirst({
where: { projectId: project.id, name: newPromptName },
}),
).resolves.toBeNull();
} finally {
entitlementAccess["cloud:hobby"].entitlementLimits[
"prompt-management-count-prompts"
] = previousLimit;
}
});
it("rejects protected labels without protected-label access", async () => {
const { project, org } = await createOrgProjectAndApiKey();
const protectedLabel = `protected-${v4().slice(0, 8)}`;
await prisma.promptProtectedLabels.create({
data: { projectId: project.id, label: protectedLabel },
});
const { caller } = createCaller({
projectId: project.id,
orgId: org.id,
projectName: project.name,
orgName: org.name,
admin: false,
projectRole: "MEMBER",
});
await expect(
caller.prompts.importBulk({
projectId: project.id,
prompts: [
{
name: `protected-label-import-${v4()}`,
prompt: "Hello world",
labels: [protectedLabel],
},
],
}),
).rejects.toMatchObject({ code: "FORBIDDEN" });
});
it.each([
{
caseName: "array content without a chat type",
prompt: { name: "invalid-text-prompt", prompt: [] },
},
{
caseName: "string content with a chat type",
prompt: {
name: "invalid-chat-prompt",
type: "chat",
prompt: "Hello world",
},
},
{
caseName: "a malformed chat message",
prompt: {
name: "invalid-chat-message",
type: "chat",
prompt: [{ role: "user" }],
},
},
])("rejects $caseName", async ({ prompt }) => {
const { project, caller } = await prepare();
const input: unknown = {
projectId: project.id,
prompts: [prompt],
};
await expect(
caller.prompts.importBulk(
input as Parameters<typeof caller.prompts.importBulk>[0],
),
).rejects.toMatchObject({ code: "BAD_REQUEST" });
});
it("reports a committed item as successful when audit logging fails", async () => {
const { project, caller } = await prepare();
const promptName = `audit-failure-${v4()}`;
const auditLogCreateSpy = vi
.spyOn(prisma.auditLog, "create")
.mockRejectedValueOnce(new Error("audit log unavailable"));
try {
const result = await caller.prompts.importBulk({
projectId: project.id,
prompts: [{ name: promptName, prompt: "Hello world" }],
});
expect(result.results).toEqual([{ name: promptName, success: true }]);
await expect(
prisma.prompt.findUnique({
where: {
projectId_name_version: {
projectId: project.id,
name: promptName,
version: 1,
},
},
}),
).resolves.not.toBeNull();
} finally {
auditLogCreateSpy.mockRestore();
}
});
it("reports a committed item as successful when event sourcing fails", async () => {
const { project, caller } = await prepare();
const promptName = `event-failure-${v4()}`;
const getQueueSpy = vi
.spyOn(EntityChangeQueue, "getInstance")
.mockReturnValue({
add: vi.fn().mockRejectedValue(new Error("queue unavailable")),
} as never);
try {
const result = await caller.prompts.importBulk({
projectId: project.id,
prompts: [{ name: promptName, prompt: "Hello world" }],
});
expect(result.results).toEqual([{ name: promptName, success: true }]);
await expect(
prisma.prompt.findUnique({
where: {
projectId_name_version: {
projectId: project.id,
name: promptName,
version: 1,
},
},
}),
).resolves.not.toBeNull();
} finally {
getQueueSpy.mockRestore();
}
});
it("reports failures that happen before the prompt is committed", async () => {
const { project, caller } = await prepare();
const promptName = `type-mismatch-${v4()}`;
await caller.prompts.importBulk({
projectId: project.id,
prompts: [{ name: promptName, prompt: "Text prompt" }],
});
const result = await caller.prompts.importBulk({
projectId: project.id,
prompts: [
{
name: promptName,
type: PromptType.Chat,
prompt: [{ role: "user", content: "Chat prompt" }],
},
],
});
expect(result.results).toEqual([
expect.objectContaining({
name: promptName,
success: false,
}),
]);
await expect(
prisma.prompt.count({
where: { projectId: project.id, name: promptName },
}),
).resolves.toBe(1);
});
});
describe("prompts.allVersions", () => {
it("returns comment counts for the requested prompt version page only", async () => {
const { project, caller } = await prepare();
const promptName = `test-prompt-comments-${v4()}`;
const promptVersions = Array.from({ length: 350 }, (_, index) => ({
id: v4(),
projectId: project.id,
name: promptName,
version: index + 1,
type: "text" as const,
prompt: { text: `Hello world v${index + 1}` },
createdBy: "API",
}));
await prisma.prompt.createMany({
data: promptVersions,
});
await prisma.comment.createMany({
data: [
{
projectId: project.id,
content: "old version comment",
objectId: promptVersions[0].id,
objectType: "PROMPT",
},
{
projectId: project.id,
content: "latest version comment",
objectId: promptVersions[349].id,
objectType: "PROMPT",
},
],
});
const result = await caller.prompts.allVersions({
projectId: project.id,
name: promptName,
page: 0,
limit: 10,
includeCommentCounts: true,
});
expect(result.promptVersions).toHaveLength(10);
expect(result.promptVersions[0]?.version).toBe(350);
expect(result.commentCounts).toEqual(
new Map([[promptVersions[349].id, 1]]),
);
});
});
describe("prompts.setLabels", () => {
it("should set labels on a prompt and remove them from other versions", async () => {
const { project, caller } = await prepare();
// Create trigger for prompt updates
const trigger = await prisma.trigger.create({
data: {
id: v4(),
projectId: project.id,
eventSource: "prompt",
eventActions: ["updated"],
filter: [],
status: "ACTIVE",
},
});
// Create webhook action
const action = await prisma.action.create({
data: {
id: v4(),
projectId: project.id,
type: "WEBHOOK",
config: {
type: "WEBHOOK",
url: "https://example.com/prompt-labels-webhook",
headers: { "Content-Type": "application/json" },
apiVersion: { prompt: "v1" },
},
},
});
// Link trigger to action
await prisma.automation.create({
data: {
projectId: project.id,
triggerId: trigger.id,
actionId: action.id,
name: "Prompt Labels Automation",
},
});
// Create test prompts with different versions
const prompt1 = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "test-prompt",
version: 1,
type: "text",
prompt: { text: "Hello world v1" },
createdBy: "test-user",
labels: ["production"],
},
});
const prompt2 = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "test-prompt",
version: 2,
type: "text",
prompt: { text: "Hello world v2" },
createdBy: "test-user",
labels: ["latest"],
},
});
// Set "production" label on prompt2, which should remove it from prompt1
await caller.prompts.setLabels({
projectId: project.id,
promptId: prompt2.id,
labels: ["production", "latest"],
});
// Verify that prompt1 no longer has the "production" label
const updatedPrompt1 = await prisma.prompt.findUnique({
where: { id: prompt1.id },
});
expect(updatedPrompt1?.labels).not.toContain("production");
// Verify that prompt2 has both labels
const updatedPrompt2 = await prisma.prompt.findUnique({
where: { id: prompt2.id },
});
expect(updatedPrompt2?.labels).toEqual(
expect.arrayContaining(["production", "latest"]),
);
await waitForExpect(async () => {
const executions = await prisma.automationExecution.findMany({
where: {
projectId: project.id,
triggerId: trigger.id,
actionId: action.id,
},
});
expect(executions).toHaveLength(2);
expect(executions).toEqual(
expect.arrayContaining([
expect.objectContaining({
sourceId: prompt1.id,
status: "PENDING",
}),
expect.objectContaining({
sourceId: prompt2.id,
status: "PENDING",
}),
]),
);
});
});
});
describe("prompts.updateTags", () => {
it("should update tags on all versions of a prompt", async () => {
const { project, caller } = await prepare();
// Create trigger for prompt updates
const trigger = await prisma.trigger.create({
data: {
id: v4(),
projectId: project.id,
eventSource: "prompt",
eventActions: ["updated"],
filter: [],
status: "ACTIVE",
},
});
// Create webhook action
const action = await prisma.action.create({
data: {
id: v4(),
projectId: project.id,
type: "WEBHOOK",
config: {
type: "WEBHOOK",
url: "https://example.com/prompt-labels-webhook",
headers: { "Content-Type": "application/json" },
apiVersion: { prompt: "v1" },
},
},
});
// Link trigger to action
await prisma.automation.create({
data: {
projectId: project.id,
triggerId: trigger.id,
actionId: action.id,
name: "Prompt Tags Automation",
},
});
// Create test prompts with different versions but same name
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "test-prompt-tags",
version: 1,
type: "text",
prompt: { text: "Hello world v1" },
createdBy: "test-user",
tags: ["old-tag"],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "test-prompt-tags",
version: 2,
type: "text",
prompt: { text: "Hello world v2" },
createdBy: "test-user",
tags: ["old-tag"],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "test-prompt-tags",
version: 3,
type: "text",
prompt: { text: "Hello world v3" },
createdBy: "test-user",
tags: ["old-tag"],
},
});
// Update tags for all versions of the prompt
await caller.prompts.updateTags({
projectId: project.id,
name: "test-prompt-tags",
tags: ["new-tag", "updated-tag"],
});
// Verify that all versions now have the new tags
const updatedPrompts = await prisma.prompt.findMany({
where: {
projectId: project.id,
name: "test-prompt-tags",
},
orderBy: { version: "asc" },
});
expect(updatedPrompts).toHaveLength(3);
updatedPrompts.forEach((prompt) => {
expect(prompt.tags).toEqual(
expect.arrayContaining(["new-tag", "updated-tag"]),
);
expect(prompt.tags).not.toContain("old-tag");
});
await waitForExpect(async () => {
const executions = await prisma.automationExecution.findMany({
where: {
projectId: project.id,
triggerId: trigger.id,
actionId: action.id,
},
});
expect(executions).toHaveLength(3);
expect(executions).toEqual(
expect.arrayContaining([
expect.objectContaining({
sourceId: updatedPrompts[0].id,
status: "PENDING",
}),
expect.objectContaining({
sourceId: updatedPrompts[1].id,
status: "PENDING",
}),
expect.objectContaining({
sourceId: updatedPrompts[2].id,
status: "PENDING",
}),
]),
);
});
});
});
describe("prompts.delete", () => {
it("should delete all versions of a prompt", async () => {
const { project, caller } = await prepare();
// Create trigger for prompt deletions
const trigger = await prisma.trigger.create({
data: {
id: v4(),
projectId: project.id,
eventSource: "prompt",
eventActions: ["deleted"],
filter: [],
status: "ACTIVE",
},
});
// Create webhook action
const action = await prisma.action.create({
data: {
id: v4(),
projectId: project.id,
type: "WEBHOOK",
config: {
type: "WEBHOOK",
url: "https://example.com/prompt-delete-webhook",
headers: { "Content-Type": "application/json" },
apiVersion: { prompt: "v1" },
},
},
});
// Link trigger to action
await prisma.automation.create({
data: {
projectId: project.id,
triggerId: trigger.id,
actionId: action.id,
name: "Prompt Delete Automation",
},
});
// Create test prompts with multiple versions
const prompt1 = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "test-prompt-delete",
version: 1,
type: "text",
prompt: { text: "Hello world v1" },
createdBy: "test-user",
},
});
const prompt2 = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "test-prompt-delete",
version: 2,
type: "text",
prompt: { text: "Hello world v2" },
createdBy: "test-user",
},
});
// Delete all versions of the prompt
await caller.prompts.delete({
projectId: project.id,
promptName: "test-prompt-delete",
});
// Verify prompts are deleted
const remainingPrompts = await prisma.prompt.findMany({
where: {
projectId: project.id,
name: "test-prompt-delete",
},
});
expect(remainingPrompts).toHaveLength(0);
await waitForExpect(async () => {
// Verify automation executions were created for both deleted prompts
const executions = await prisma.automationExecution.findMany({
where: {
projectId: project.id,
triggerId: trigger.id,
actionId: action.id,
},
});
expect(executions).toHaveLength(2);
expect(executions).toEqual(
expect.arrayContaining([
expect.objectContaining({
sourceId: prompt1.id,
status: "PENDING",
}),
expect.objectContaining({
sourceId: prompt2.id,
status: "PENDING",
}),
]),
);
});
});
it("should delete prompts by pathPrefix (folder) including nested folders but preserving prompt with same name", async () => {
const { project, caller } = await prepare();
// Create test structure:
// folder1/prompt-1 (Prompt)
// folder1/folder2 (Prompt named folder1/folder2)
// folder1/folder2/prompt-2 (Prompt inside folder1/folder2)
// folder1/folder2/prompt-3 (Prompt inside folder1/folder2)
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "folder1/prompt-1",
version: 1,
type: "text",
prompt: { text: "Hello world 1" },
createdBy: "test-user",
},
});
const promptNamedSameAsFolder = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "folder1/folder2",
version: 1,
type: "text",
prompt: { text: "This is a prompt named folder1/folder2" },
createdBy: "test-user",
},
});
const prompt2 = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "folder1/folder2/prompt-2",
version: 1,
type: "text",
prompt: { text: "Hello world 2" },
createdBy: "test-user",
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "folder1/folder2/prompt-3",
version: 1,
type: "text",
prompt: { text: "Hello world 3" },
createdBy: "test-user",
},
});
// Add intra-folder dependency: prompt-2 depends on prompt-3
// This should NOT block folder deletion since both are inside the folder
await prisma.promptDependency.create({
data: {
projectId: project.id,
parentId: prompt2.id,
childName: "folder1/folder2/prompt-3",
childVersion: 1,
},
});
// Delete the folder "folder1/folder2"
await caller.prompts.delete({
projectId: project.id,
pathPrefix: "folder1/folder2",
});
// Verify nested items are deleted
const nestedPrompts = await prisma.prompt.findMany({
where: {
projectId: project.id,
name: { startsWith: "folder1/folder2/" },
},
});
expect(nestedPrompts).toHaveLength(0);
// Verify prompt named folder1/folder2 remains
const sameNamePrompt = await prisma.prompt.findUnique({
where: { id: promptNamedSameAsFolder.id },
});
expect(sameNamePrompt).not.toBeNull();
expect(sameNamePrompt?.name).toBe("folder1/folder2");
// Verify folder1/prompt-1 still exists
const unrelatedPrompt = await prisma.prompt.findFirst({
where: {
projectId: project.id,
name: "folder1/prompt-1",
},
});
expect(unrelatedPrompt).not.toBeNull();
});
it("should treat wildcard characters in pathPrefix as literals for all/count/delete", async () => {
const { project, caller } = await prepare();
const literalPathPrefix = "folder%_x";
const literalChildPromptName = `${literalPathPrefix}/inside`;
const wildcardMatchPromptName = "folderABx/decoy";
const promptNamedLikeFolder = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: literalPathPrefix,
version: 1,
type: "text",
prompt: { text: "Prompt with wildcard chars in the name" },
createdBy: "test-user",
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: literalChildPromptName,
version: 1,
type: "text",
prompt: { text: "Prompt inside wildcard-like folder" },
createdBy: "test-user",
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: wildcardMatchPromptName,
version: 1,
type: "text",
prompt: { text: "Should not match literal wildcard folder prefix" },
createdBy: "test-user",
},
});
const listResult = await caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "createdAt", order: "DESC" },
pathPrefix: literalPathPrefix,
});
expect(listResult.prompts).toHaveLength(1);
expect(listResult.prompts[0]?.name).toBe("inside");
const countResult = await caller.prompts.count({
projectId: project.id,
pathPrefix: literalPathPrefix,
});
expect(countResult.totalCount).toBe(BigInt(1));
await caller.prompts.delete({
projectId: project.id,
pathPrefix: literalPathPrefix,
});
const deletedChildPrompt = await prisma.prompt.findFirst({
where: {
projectId: project.id,
name: literalChildPromptName,
},
});
expect(deletedChildPrompt).toBeNull();
const remainingPromptNamedLikeFolder = await prisma.prompt.findUnique({
where: { id: promptNamedLikeFolder.id },
});
expect(remainingPromptNamedLikeFolder).not.toBeNull();
const remainingWildcardMatchPrompt = await prisma.prompt.findFirst({
where: {
projectId: project.id,
name: wildcardMatchPromptName,
},
});
expect(remainingWildcardMatchPrompt).not.toBeNull();
});
});
describe("prompts.deleteVersion", () => {
it("should delete a specific version of a prompt", async () => {
const { project, caller } = await prepare();
// Create trigger for prompt deletions
const trigger = await prisma.trigger.create({
data: {
id: v4(),
projectId: project.id,
eventSource: "prompt",
eventActions: ["deleted"],
filter: [],
status: "ACTIVE",
},
});
// Create webhook action
const action = await prisma.action.create({
data: {
id: v4(),
projectId: project.id,
type: "WEBHOOK",
config: {
type: "WEBHOOK",
url: "https://example.com/prompt-delete-version-webhook",
headers: { "Content-Type": "application/json" },
apiVersion: { prompt: "v1" },
},
},
});
// Link trigger to action
await prisma.automation.create({
data: {
projectId: project.id,
triggerId: trigger.id,
actionId: action.id,
name: "Prompt Delete Version Automation",
},
});
// Create test prompts with multiple versions
const prompt1 = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "test-prompt-delete-version",
version: 1,
type: "text",
prompt: { text: "Hello world v1" },
createdBy: "test-user",
},
});
const prompt2 = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "test-prompt-delete-version",
version: 2,
type: "text",
prompt: { text: "Hello world v2" },
createdBy: "test-user",
},
});
// Delete only version 1
await caller.prompts.deleteVersion({
projectId: project.id,
promptVersionId: prompt1.id,
});
// Verify only version 1 is deleted
const remainingPrompts = await prisma.prompt.findMany({
where: {
projectId: project.id,
name: "test-prompt-delete-version",
},
});
expect(remainingPrompts).toHaveLength(1);
expect(remainingPrompts[0].id).toBe(prompt2.id);
await waitForExpect(async () => {
// Verify automation execution was created for the deleted prompt
const executions = await prisma.automationExecution.findMany({
where: {
projectId: project.id,
triggerId: trigger.id,
actionId: action.id,
},
});
expect(executions).toHaveLength(1);
expect(executions[0]).toEqual(
expect.objectContaining({
sourceId: prompt1.id,
status: "PENDING",
}),
);
});
});
it("should not delete an unlabeled prompt version referenced by version", async () => {
const { project, caller } = await prepare();
const childName = `test-prompt-delete-unlabeled-dependency-${v4()}`;
const parentName = `test-prompt-delete-unlabeled-dependent-${v4()}`;
const childPrompt = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: childName,
version: 1,
type: "text",
prompt: "child content",
createdBy: "test-user",
labels: [],
},
});
const parentPrompt = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: parentName,
version: 1,
type: "text",
prompt: `Depends on @@@langfusePrompt:name=${childName}|version=1@@@`,
createdBy: "test-user",
labels: ["latest"],
},
});
await prisma.promptDependency.create({
data: {
projectId: project.id,
parentId: parentPrompt.id,
childName,
childVersion: 1,
},
});
await expect(
caller.prompts.deleteVersion({
projectId: project.id,
promptVersionId: childPrompt.id,
}),
).rejects.toThrow(/depending on the prompt version/);
const remainingChild = await prisma.prompt.findUnique({
where: {
id: childPrompt.id,
projectId: project.id,
},
});
expect(remainingChild).not.toBeNull();
});
});
describe("prompts.duplicatePrompt", () => {
it("should duplicate a prompt and trigger webhook automation", async () => {
const { project, caller } = await prepare();
// Create trigger for prompt creation
const trigger = await prisma.trigger.create({
data: {
id: v4(),
projectId: project.id,
eventSource: "prompt",
eventActions: ["created"],
filter: [],
status: "ACTIVE",
},
});
// Create webhook action
const action = await prisma.action.create({
data: {
id: v4(),
projectId: project.id,
type: "WEBHOOK",
config: {
type: "WEBHOOK",
url: "https://example.com/prompt-duplicate-webhook",
headers: { "Content-Type": "application/json" },
apiVersion: { prompt: "v1" },
},
},
});
// Link trigger to action
await prisma.automation.create({
data: {
projectId: project.id,
triggerId: trigger.id,
actionId: action.id,
name: "Prompt Duplicate Automation",
},
});
// Create original prompt to duplicate
const originalPrompt = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "original-prompt",
version: 1,
type: "text",
prompt: "Hello world original",
createdBy: "test-user",
labels: ["production"],
tags: ["original"],
},
});
// Duplicate the prompt
const duplicatedPrompt = await caller.prompts.duplicatePrompt({
projectId: project.id,
promptId: originalPrompt.id,
name: "duplicated-prompt",
isSingleVersion: true,
});
expect(duplicatedPrompt).toMatchObject({
name: "duplicated-prompt",
version: 1,
type: "text",
prompt: "Hello world original",
labels: expect.arrayContaining(["production", "latest"]),
tags: ["original"],
});
// Verify the duplicated prompt exists in database
const dbPrompt = await prisma.prompt.findUnique({
where: { id: duplicatedPrompt.id },
});
expect(dbPrompt).not.toBeNull();
expect(dbPrompt?.name).toBe("duplicated-prompt");
await waitForExpect(async () => {
// Verify automation execution was created for the duplicated prompt
const executions = await prisma.automationExecution.findMany({
where: {
projectId: project.id,
triggerId: trigger.id,
actionId: action.id,
},
});
expect(executions).toHaveLength(1);
expect(executions[0]).toEqual(
expect.objectContaining({
sourceId: duplicatedPrompt.id,
status: "PENDING",
}),
);
});
});
});
describe("prompts.all with search", () => {
it("should find prompts by searching in content, name, tags, and labels", async () => {
const { project, caller } = await prepare();
// Create test prompts with different searchable content
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "customer-service-prompt",
version: 1,
type: "text",
prompt: {
text: "You are a helpful customer support agent. Answer questions about billing and account issues.",
},
createdBy: "test-user",
tags: ["support", "customer"],
labels: ["production"],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "marketing-prompt",
version: 1,
type: "text",
prompt: {
text: "Create engaging marketing content that drives sales and conversions.",
},
createdBy: "test-user",
tags: ["marketing", "content"],
labels: ["staging"],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "technical-docs",
version: 1,
type: "text",
prompt: {
text: "Generate comprehensive technical documentation for APIs and software systems.",
},
createdBy: "test-user",
tags: ["technical", "documentation"],
labels: ["latest"],
},
});
// Test 1: Search by prompt content
const contentSearchResults = await caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "createdAt", order: "DESC" },
searchQuery: "customer support agent",
searchType: ["content"],
});
expect(contentSearchResults.prompts).toHaveLength(1);
expect(contentSearchResults.prompts[0].name).toBe(
"customer-service-prompt",
);
// Test 2: Search by prompt name
const nameSearchResults = await caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "createdAt", order: "DESC" },
searchQuery: "marketing",
searchType: ["id"],
});
expect(nameSearchResults.prompts).toHaveLength(1);
expect(nameSearchResults.prompts[0].name).toBe("marketing-prompt");
// Test 3: Search by tags
const tagSearchResults = await caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "createdAt", order: "DESC" },
searchQuery: "documentation",
searchType: ["id"],
});
expect(tagSearchResults.prompts).toHaveLength(1);
expect(tagSearchResults.prompts[0].name).toBe("technical-docs");
// Test 4: Search with no matches
const noMatchResults = await caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "createdAt", order: "DESC" },
searchQuery: "nonexistent content",
searchType: ["id", "content"],
});
expect(noMatchResults.prompts).toHaveLength(0);
// Test 5: Case insensitive search
const caseInsensitiveResults = await caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "createdAt", order: "DESC" },
searchQuery: "TECHNICAL",
searchType: ["id"],
});
expect(caseInsensitiveResults.prompts).toHaveLength(1);
expect(caseInsensitiveResults.prompts[0].name).toBe("technical-docs");
});
it("should find prompts with multiple versions when searching content", async () => {
const { project, caller } = await prepare();
// Create multiple versions of the same prompt with different content
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "evolving-prompt",
version: 1,
type: "text",
prompt: { text: "You are a basic chatbot. Answer simple questions." },
createdBy: "test-user",
tags: ["basic"],
labels: [],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "evolving-prompt",
version: 2,
type: "text",
prompt: {
text: "You are an advanced AI assistant with expertise in machine learning.",
},
createdBy: "test-user",
tags: ["advanced"],
labels: ["latest"],
},
});
// Search for content that exists in version 2 but not version 1
const searchResults = await caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "createdAt", order: "DESC" },
searchQuery: "machine learning",
searchType: ["content"],
});
// Should find the prompt because one of its versions contains the search term
expect(searchResults.prompts).toHaveLength(1);
expect(searchResults.prompts[0].name).toBe("evolving-prompt");
// The returned prompt should be the latest version
expect(searchResults.prompts[0].version).toBe(2);
});
it("should match LIKE wildcard characters in names and tags literally", async () => {
const { project, caller } = await prepare();
const createPrompt = (name: string, tags: string[]) =>
prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name,
version: 1,
type: "text",
prompt: { text: "unrelated" },
createdBy: "test-user",
tags,
},
});
await createPrompt("gpt_4-prompt", []);
await createPrompt("gpt-4-prompt", []);
await createPrompt("discount-literal", ["save 100%"]);
await createPrompt("discount-wildcard", ["save 1000"]);
const search = async (searchQuery: string) =>
(
await caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "createdAt", order: "DESC" },
searchQuery,
searchType: ["id"],
})
).prompts.map((p) => p.name);
expect(await search("gpt_4")).toEqual(["gpt_4-prompt"]);
expect(await search("100%")).toEqual(["discount-literal"]);
});
});
describe("prompts.count with search and searchType", () => {
it("should count prompts correctly with different search types", async () => {
const { project, caller } = await prepare();
// Create test prompts with different searchable content
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "customer-service-prompt",
version: 1,
type: "text",
prompt: {
text: "You are a helpful customer support agent. Answer questions about billing and account issues.",
},
createdBy: "test-user",
tags: ["support", "customer"],
labels: ["production"],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "marketing-prompt",
version: 1,
type: "text",
prompt: {
text: "Create engaging marketing content that drives sales and conversions.",
},
createdBy: "test-user",
tags: ["marketing", "content"],
labels: ["staging"],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "technical-docs",
version: 1,
type: "text",
prompt: {
text: "Generate comprehensive technical documentation for APIs and software systems.",
},
createdBy: "test-user",
tags: ["technical", "documentation"],
labels: ["latest"],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "sales-support-prompt",
version: 1,
type: "text",
prompt: {
text: "Assist sales teams with lead qualification and customer outreach.",
},
createdBy: "test-user",
tags: ["sales", "support"],
labels: ["production"],
},
});
// Test 1: Count all prompts (no search)
const totalCountResult = await caller.prompts.count({
projectId: project.id,
});
expect(totalCountResult.totalCount).toBe(BigInt(4));
// Test 2: Count with ID search type (default) - search in names and tags
const idSearchCountResult = await caller.prompts.count({
projectId: project.id,
searchQuery: "support",
searchType: ["id"],
});
// Should find "customer-service-prompt" and "sales-support-prompt" (by name and tags)
expect(idSearchCountResult.totalCount).toBe(BigInt(2));
// Test 3: Count with content search type - search in prompt content
const contentSearchCountResult = await caller.prompts.count({
projectId: project.id,
searchQuery: "customer",
searchType: ["content"],
});
// Should find "customer-service-prompt" and "sales-support-prompt" (by content: "customer" appears in both)
expect(contentSearchCountResult.totalCount).toBe(BigInt(2));
// Test 4: Count with both ID and content search types
const combinedSearchCountResult = await caller.prompts.count({
projectId: project.id,
searchQuery: "marketing",
searchType: ["id", "content"],
});
// Should find "marketing-prompt" (by both name and content)
expect(combinedSearchCountResult.totalCount).toBe(BigInt(1));
// Test 5: Count with tag-specific search (ID search type)
const tagSearchCountResult = await caller.prompts.count({
projectId: project.id,
searchQuery: "documentation",
searchType: ["id"],
});
// Should find "technical-docs" (by tag)
expect(tagSearchCountResult.totalCount).toBe(BigInt(1));
// Test 6: Count with content that doesn't exist in names/tags but exists in prompt text
const contentOnlySearchResult = await caller.prompts.count({
projectId: project.id,
searchQuery: "billing",
searchType: ["content"],
});
// Should find "customer-service-prompt" (by content only)
expect(contentOnlySearchResult.totalCount).toBe(BigInt(1));
// Test 7: Count with no matches
const noMatchCountResult = await caller.prompts.count({
projectId: project.id,
searchQuery: "nonexistent",
searchType: ["id", "content"],
});
expect(noMatchCountResult.totalCount).toBe(BigInt(0));
// Test 8: Count with case insensitive search
const caseInsensitiveCountResult = await caller.prompts.count({
projectId: project.id,
searchQuery: "TECHNICAL",
searchType: ["id"],
});
// Should find "technical-docs" (case insensitive)
expect(caseInsensitiveCountResult.totalCount).toBe(BigInt(1));
// Test 9: Count with filters and search combined
const filteredSearchCountResult = await caller.prompts.count({
projectId: project.id,
searchQuery: "support",
searchType: ["id"],
filter: [
{
column: "labels",
type: "arrayOptions",
operator: "any of",
value: ["production"],
},
],
});
// Should find prompts with "support" AND "production" label
expect(filteredSearchCountResult.totalCount).toBe(BigInt(2));
});
it("should count prompts with folder path prefix and search", async () => {
const { project, caller } = await prepare();
// Create prompts in different folder structures
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "folder1/customer-prompt",
version: 1,
type: "text",
prompt: { text: "Customer service prompt" },
createdBy: "test-user",
tags: ["customer"],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "folder1/support-prompt",
version: 1,
type: "text",
prompt: { text: "Technical support prompt" },
createdBy: "test-user",
tags: ["support"],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "folder2/customer-prompt",
version: 1,
type: "text",
prompt: { text: "Another customer prompt" },
createdBy: "test-user",
tags: ["customer"],
},
});
// Test: Count with path prefix and search
const folderSearchCountResult = await caller.prompts.count({
projectId: project.id,
pathPrefix: "folder1",
searchQuery: "customer",
searchType: ["id"],
});
// Should only find prompts in folder1 that match "customer"
expect(folderSearchCountResult.totalCount).toBe(BigInt(1));
// Test: Count with path prefix but no search
const folderCountResult = await caller.prompts.count({
projectId: project.id,
pathPrefix: "folder1",
});
// Should find all prompts in folder1
expect(folderCountResult.totalCount).toBe(BigInt(2));
});
});
describe("folder navigation with conflicting names", () => {
it("should handle prompts where individual prompt name conflicts with folder prefix - BUG REPRODUCTION", async () => {
const { project, caller } = await prepare();
// Create the exact bug scenario:
// - a/prompt (individual prompt)
// - a/prompt/v1 (prompt in subfolder)
// - a/prompt/v2 (prompt in subfolder)
// Bug: LFE-6515
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "a/prompt",
version: 1,
type: "text",
prompt: { text: "This is the individual a/prompt" },
createdBy: "test-user",
tags: ["individual"],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "a/prompt/v1",
version: 1,
type: "text",
prompt: { text: "This is a/prompt/v1" },
createdBy: "test-user",
tags: ["subfolder"],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "a/prompt/v2",
version: 1,
type: "text",
prompt: { text: "This is a/prompt/v2" },
createdBy: "test-user",
tags: ["subfolder"],
},
});
// Test 1: Root level should show folder "a"
const rootResults = await caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "createdAt", order: "DESC" },
});
// Should show exactly one item: the "a" folder
expect(rootResults.prompts).toHaveLength(1);
expect(rootResults.prompts[0].name).toBe("a");
// Test 2: Folder "a" level should show BOTH individual "prompt" AND folder entry for "prompt/*"
const folderAResults = await caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "createdAt", order: "DESC" },
pathPrefix: "a",
});
// We expect 2 items: individual "prompt" and folder "prompt" (for v1, v2)
expect(folderAResults.prompts).toHaveLength(2);
// Should have both an individual prompt and a folder entry
const promptNames = folderAResults.prompts.map((p) => p.name);
expect(promptNames).toContain("prompt");
// Test 3: Folder "a/prompt" level should show v1 and v2
const subfolderResults = await caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "createdAt", order: "DESC" },
pathPrefix: "a/prompt",
});
// Should show exactly 2 items: v1 and v2
expect(subfolderResults.prompts).toHaveLength(2);
const subfolderNames = subfolderResults.prompts.map((p) => p.name);
expect(subfolderNames).toContain("v1");
expect(subfolderNames).toContain("v2");
// Test 4: Count verification
const rootCount = await caller.prompts.count({
projectId: project.id,
});
expect(rootCount.totalCount).toBe(BigInt(1)); // Should show 1 folder
const folderACount = await caller.prompts.count({
projectId: project.id,
pathPrefix: "a",
});
expect(folderACount.totalCount).toBe(BigInt(2)); // Should show individual + folder entry
const subfolderCount = await caller.prompts.count({
projectId: project.id,
pathPrefix: "a/prompt",
});
expect(subfolderCount.totalCount).toBe(BigInt(2)); // Should show v1 and v2
});
it("should maintain search functionality across conflicting folder/prompt names", async () => {
const { project, caller } = await prepare();
// Create searchable content in conflicting structure
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "search/test",
version: 1,
type: "text",
prompt: { text: "Individual search test with unique keyword" },
createdBy: "test-user",
tags: ["searchable"],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "search/test/nested1",
version: 1,
type: "text",
prompt: { text: "Nested prompt with unique keyword" },
createdBy: "test-user",
tags: ["nested"],
},
});
await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: "search/test/nested2",
version: 1,
type: "text",
prompt: { text: "Another nested prompt" },
createdBy: "test-user",
tags: ["nested"],
},
});
// Test search at different folder levels
const searchResults = await caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "createdAt", order: "DESC" },
searchQuery: "unique",
searchType: ["content"],
});
// Should find prompts containing "unique" keyword
expect(searchResults.prompts.length).toBeGreaterThan(0);
// Test search within specific folder
const folderSearchResults = await caller.prompts.all({
projectId: project.id,
page: 0,
limit: 10,
filter: [],
orderBy: { column: "createdAt", order: "DESC" },
pathPrefix: "search/test",
searchQuery: "nested",
searchType: ["content"],
});
// Should find nested prompts within the folder
expect(folderSearchResults.prompts.length).toBeGreaterThan(0);
});
});
describe("prompts.duplicateFolder", () => {
it("should duplicate a folder when the target path exists as a prompt and keep intra-folder references self-contained", async () => {
const { project, caller } = await prepare();
const folderPrefix = `dup-self-contained-${v4().slice(0, 8)}`;
const folderAPath = `${folderPrefix}/folder-a`;
const folderACopyPath = `${folderPrefix}/folder-a-copy`;
const solverPromptName = `${folderAPath}/solver-a`;
const rubricPromptName = `${folderAPath}/rubric-a`;
const copiedSolverPromptName = `${folderACopyPath}/solver-a`;
const copiedRubricPromptName = `${folderACopyPath}/rubric-a`;
const solverPrompt = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: solverPromptName,
version: 1,
type: "text",
prompt: `Solve using @@@langfusePrompt:name=${rubricPromptName}|label=production@@@`,
createdBy: "test-user",
labels: ["latest"],
},
});
await prisma.prompt.createMany({
data: [
{
id: v4(),
projectId: project.id,
name: folderAPath,
version: 1,
type: "text",
prompt: "prompt with same name as source folder",
createdBy: "test-user",
labels: ["latest"],
},
{
id: v4(),
projectId: project.id,
name: rubricPromptName,
version: 1,
type: "text",
prompt: "rubric latest-labeled content",
createdBy: "test-user",
labels: ["production", "latest"],
},
{
id: v4(),
projectId: project.id,
name: rubricPromptName,
version: 2,
type: "text",
prompt: "rubric higher-version content",
createdBy: "test-user",
labels: ["staging"],
},
{
id: v4(),
projectId: project.id,
name: folderACopyPath,
version: 1,
type: "text",
prompt: "prompt with same name as target folder",
createdBy: "test-user",
labels: ["latest"],
},
],
});
await prisma.promptDependency.create({
data: {
projectId: project.id,
parentId: solverPrompt.id,
childName: rubricPromptName,
childLabel: "production",
},
});
const result = await caller.prompts.duplicateFolder({
projectId: project.id,
sourcePath: folderAPath,
targetPath: folderACopyPath,
isSingleVersion: true,
rewritePromptReferences: true,
});
expect(result.copiedCount).toBe(2);
expect(result.copiedPromptNames).toEqual(
expect.arrayContaining([
copiedSolverPromptName,
copiedRubricPromptName,
]),
);
const copiedSolverPrompt = await prisma.prompt.findFirst({
where: {
projectId: project.id,
name: copiedSolverPromptName,
},
include: {
PromptDependency: true,
},
});
expect(copiedSolverPrompt).not.toBeNull();
expect(copiedSolverPrompt?.version).toBe(1);
expect(copiedSolverPrompt?.prompt).toBe(
`Solve using @@@langfusePrompt:name=${copiedRubricPromptName}|label=production@@@`,
);
expect(copiedSolverPrompt?.PromptDependency).toEqual(
expect.arrayContaining([
expect.objectContaining({
childName: copiedRubricPromptName,
childLabel: "production",
childVersion: null,
}),
]),
);
const copiedRubricPrompt = await prisma.prompt.findFirst({
where: {
projectId: project.id,
name: copiedRubricPromptName,
},
});
expect(copiedRubricPrompt).not.toBeNull();
expect(copiedRubricPrompt?.prompt).toBe("rubric latest-labeled content");
const exactTargetPrompt = await prisma.prompt.findMany({
where: {
projectId: project.id,
name: folderACopyPath,
},
});
expect(exactTargetPrompt).toHaveLength(1);
expect(exactTargetPrompt[0].prompt).toBe(
"prompt with same name as target folder",
);
});
it("should reject when the target path already exists as a folder with prompts", async () => {
const { project, caller } = await prepare();
const folderPrefix = `dup-folder-conflict-${v4().slice(0, 8)}`;
const folderAPath = `${folderPrefix}/folder-a`;
const folderACopyPath = `${folderPrefix}/folder-a-copy`;
await prisma.prompt.createMany({
data: [
{
id: v4(),
projectId: project.id,
name: folderAPath,
version: 1,
type: "text",
prompt: "prompt with same name as source folder",
createdBy: "test-user",
labels: ["latest"],
},
{
id: v4(),
projectId: project.id,
name: `${folderAPath}/solver-a`,
version: 1,
type: "text",
prompt: "solver content",
createdBy: "test-user",
labels: ["latest"],
},
{
id: v4(),
projectId: project.id,
name: `${folderAPath}/rubric-a`,
version: 1,
type: "text",
prompt: "rubric content",
createdBy: "test-user",
labels: ["latest"],
},
{
id: v4(),
projectId: project.id,
name: `${folderACopyPath}/existing`,
version: 1,
type: "text",
prompt: "existing target folder content",
createdBy: "test-user",
labels: ["latest"],
},
],
});
await expect(
caller.prompts.duplicateFolder({
projectId: project.id,
sourcePath: folderAPath,
targetPath: folderACopyPath,
isSingleVersion: true,
}),
).rejects.toThrow(/already exist/i);
});
it("should roll back latest-only duplication when a rewritten label dependency would be missing", async () => {
const { project, caller } = await prepare();
const folderPrefix = `dup-missing-label-${v4().slice(0, 8)}`;
const sourcePath = `${folderPrefix}/source`;
const targetPath = `${folderPrefix}/copy`;
const parentName = `${sourcePath}/parent`;
const childName = `${sourcePath}/child`;
const parentPrompt = await prisma.prompt.create({
data: {
id: v4(),
projectId: project.id,
name: parentName,
version: 1,
type: "text",
prompt: `Depends on @@@langfusePrompt:name=${childName}|label=cause@@@`,
createdBy: "test-user",
labels: ["latest"],
},
});
await prisma.prompt.createMany({
data: [
{
id: v4(),
projectId: project.id,
name: childName,
version: 1,
type: "text",
prompt: "cause-labeled child",
createdBy: "test-user",
labels: ["cause"],
},
{
id: v4(),
projectId: project.id,
name: childName,
version: 2,
type: "text",
prompt: "latest child",
createdBy: "test-user",
labels: ["latest"],
},
],
});
await prisma.promptDependency.create({
data: {
projectId: project.id,
parentId: parentPrompt.id,
childName,
childLabel: "cause",
},
});
await expect(
caller.prompts.duplicateFolder({
projectId: project.id,
sourcePath,
targetPath,
isSingleVersion: true,
rewritePromptReferences: true,
}),
).rejects.toThrow(/Copy all versions or disable reference rewriting/);
const copiedPrompts = await prisma.prompt.findMany({
where: {
projectId: project.id,
name: {
startsWith: `${targetPath}/`,
},
},
});
expect(copiedPrompts).toHaveLength(0);
});
});
});