Skip to content

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

11.1 KB
Raw Blame
import {
  createObservation,
  createObservationsCh,
  createOrgProjectAndApiKey,
} from "@langfuse/shared/src/server";
import { prisma } from "@langfuse/shared/src/db";
import {
  makeAPICall,
  makeZodVerifiedAPICall,
} from "@/src/__tests__/test-utils";
import {
  GetObservationsV1Response,
  GetObservationV1Response,
} from "@/src/features/public-api/types/observations";
import {
  DATASET_RUN_ITEMS_DEPRECATION,
  DATASET_RUNS_DEPRECATION,
  INGESTION_DEPRECATION,
  OBSERVATIONS_V1_DEPRECATION,
  SCORES_DEPRECATION,
  SESSIONS_DEPRECATION,
  TRACES_DEPRECATION,
  V3_SUNSET_DATE,
} from "@/src/features/public-api/server/deprecations";
import { randomUUID } from "crypto";

// Legacy (v3-data-model) endpoints attach a top-level `_deprecation`
// object so coding agents get a self-correcting migration signal. Cloud-only:
// assumes the server under test sets NEXT_PUBLIC_LANGFUSE_CLOUD_REGION.
describe("public API deprecation signal", () => {
  let auth: string;
  let projectId: string;

  beforeAll(async () => {
    const fixture = await createOrgProjectAndApiKey();
    auth = fixture.auth;
    projectId = fixture.projectId;
  });

  it("attaches `_deprecation` to the legacy GET /observations list response", async () => {
    const response = await makeZodVerifiedAPICall(
      GetObservationsV1Response,
      "GET",
      "/api/public/observations",
      undefined,
      auth,
    );

    expect(response.status).toBe(200);
    expect(response.body._deprecation).toEqual(OBSERVATIONS_V1_DEPRECATION);
    expect(response.body._deprecation?.replacement).toBe(
      "GET /api/public/v2/observations?fromStartTime=<from>&toStartTime=<to>",
    );
  });

  it("puts the sunset and replacement into `meta.deprecation` on list responses", async () => {
    const response = await makeZodVerifiedAPICall(
      GetObservationsV1Response,
      "GET",
      "/api/public/observations",
      undefined,
      auth,
    );

    expect(Object.keys(response.body)[0]).toBe("_deprecation");
    expect(Object.keys(response.body.meta)[0]).toBe("deprecation");
    expect(response.body.meta.deprecation).toEqual({
      sunsetAt: V3_SUNSET_DATE,
      replacement: OBSERVATIONS_V1_DEPRECATION.replacement,
    });
  });

  // The 404 is written by withMiddlewares, not by the route.
  it("attaches `_deprecation` to a 404 from the legacy single GET /observations/{id}", async () => {
    const response = await makeAPICall(
      "GET",
      `/api/public/observations/${randomUUID()}`,
      undefined,
      auth,
    );

    expect(response.status).toBe(404);
    expect((response.body as Record<string, unknown>)._deprecation).toEqual(
      OBSERVATIONS_V1_DEPRECATION,
    );
    expect((response.body as Record<string, unknown>).message).toMatch(
      / Use GET \/api\/public\/v2\/observations\?.* instead\.$/,
    );
  });

  it("carries the docs URL and the committed sunset date", async () => {
    const response = await makeZodVerifiedAPICall(
      GetObservationsV1Response,
      "GET",
      "/api/public/observations",
      undefined,
      auth,
    );

    // docsUrl points at the migration guidance for the endpoint family.
    expect(response.body._deprecation?.docsUrl).toBe(
      "https://langfuse.com/docs/api-and-data-platform/features/public-api#v2",
    );
    // sunsetAt is committed → present.
    expect(response.body._deprecation?.sunsetAt).toBe(V3_SUNSET_DATE);
  });

  // Single-item response: `_deprecation` is added via `.extend()` at the
  // response level (not on the shared item schema). makeZodVerifiedAPICall
  // validates the strict single-item schema accepts the injected field.
  it("attaches `_deprecation` to the legacy single GET /observations/{id}", async () => {
    const observationId = randomUUID();
    await createObservationsCh([
      createObservation({
        id: observationId,
        project_id: projectId,
        trace_id: randomUUID(),
        type: "GENERATION",
      }),
    ]);

    const response = await makeZodVerifiedAPICall(
      GetObservationV1Response,
      "GET",
      `/api/public/observations/${observationId}`,
      undefined,
      auth,
    );

    expect(response.status).toBe(200);
    expect(response.body._deprecation).toEqual(OBSERVATIONS_V1_DEPRECATION);
  });

  // Scores v2 uses a non-strict response schema, so this also proves the
  // injection works without a schema edit on the response type.
  it("attaches `_deprecation` to the legacy GET /v2/scores list response", async () => {
    const response = await makeAPICall(
      "GET",
      "/api/public/v2/scores",
      undefined,
      auth,
    );

    expect(response.status).toBe(200);
    expect((response.body as Record<string, unknown>)._deprecation).toEqual(
      SCORES_DEPRECATION,
    );
  });

  // Traces is being removed; it points at observations v2 as a soft
  // replacement for reading span/trace data in v4.
  it("attaches `_deprecation` with the observations-v2 soft replacement to legacy GET /traces", async () => {
    const response = await makeAPICall(
      "GET",
      "/api/public/traces",
      undefined,
      auth,
    );

    expect(response.status).toBe(200);
    const deprecation = (response.body as Record<string, unknown>)._deprecation;
    // toEqual against the constant covers `replacement` (incl. query params).
    expect(deprecation).toEqual(TRACES_DEPRECATION);
  });

  // Sessions is deprecated and not replaced, so `_deprecation` is message-only.
  it("attaches a replacement-less `_deprecation` to legacy GET /sessions", async () => {
    const response = await makeAPICall(
      "GET",
      "/api/public/sessions",
      undefined,
      auth,
    );

    expect(response.status).toBe(200);
    const deprecation = (response.body as Record<string, unknown>)._deprecation;
    expect(deprecation).toEqual(SESSIONS_DEPRECATION);
    expect((deprecation as Record<string, unknown>)?.replacement).toBe(
      "GET /api/public/v2/observations?filter=<urlencoded sessionId filter>&fromStartTime=<from>&toStartTime=<to>",
    );
  });

  // Dataset run items (Group C) → experiment items. Needs a real dataset + run
  // (the endpoint 404s otherwise); an empty run returns an empty list + signal.
  it("attaches `_deprecation` to the legacy GET /dataset-run-items list response", async () => {
    const dataset = await prisma.dataset.create({
      data: { name: `deprecation-test-${randomUUID()}`, projectId },
    });
    const run = await prisma.datasetRuns.create({
      data: { name: `run-${randomUUID()}`, datasetId: dataset.id, projectId },
    });

    const response = await makeAPICall(
      "GET",
      `/api/public/dataset-run-items?datasetId=${dataset.id}&runName=${run.name}`,
      undefined,
      auth,
    );

    expect(response.status).toBe(200);
    expect((response.body as Record<string, unknown>)._deprecation).toEqual(
      DATASET_RUN_ITEMS_DEPRECATION,
    );
  });

  it("attaches `_deprecation` to the legacy GET /datasets/{name}/runs list response", async () => {
    const dataset = await prisma.dataset.create({
      data: { name: `deprecation-test-${randomUUID()}`, projectId },
    });
    await prisma.datasetRuns.create({
      data: { name: `run-${randomUUID()}`, datasetId: dataset.id, projectId },
    });

    const response = await makeAPICall(
      "GET",
      `/api/public/datasets/${encodeURIComponent(dataset.name)}/runs`,
      undefined,
      auth,
    );

    expect(response.status).toBe(200);
    expect((response.body as Record<string, unknown>)._deprecation).toEqual(
      DATASET_RUNS_DEPRECATION,
    );
  });

  it("attaches `_deprecation` to the legacy single GET /datasets/{name}/runs/{runName}", async () => {
    const dataset = await prisma.dataset.create({
      data: { name: `deprecation-test-${randomUUID()}`, projectId },
    });
    const run = await prisma.datasetRuns.create({
      data: { name: `run-${randomUUID()}`, datasetId: dataset.id, projectId },
    });

    const response = await makeAPICall(
      "GET",
      `/api/public/datasets/${encodeURIComponent(dataset.name)}/runs/${encodeURIComponent(run.name)}`,
      undefined,
      auth,
    );

    expect(response.status).toBe(200);
    expect((response.body as Record<string, unknown>)._deprecation).toEqual(
      DATASET_RUNS_DEPRECATION,
    );
  });

  it("attaches `_deprecation` to POST /ingestion when the batch writes a trace", async () => {
    const response = await makeAPICall(
      "POST",
      "/api/public/ingestion",
      {
        batch: [
          {
            id: randomUUID(),
            type: "trace-create",
            timestamp: new Date().toISOString(),
            body: {
              id: randomUUID(),
              timestamp: new Date().toISOString(),
            },
          },
        ],
      },
      auth,
    );

    expect(response.status).toBe(207);
    expect((response.body as Record<string, unknown>)._deprecation).toEqual(
      INGESTION_DEPRECATION,
    );
    expect(INGESTION_DEPRECATION.replacement).toBe(
      "POST /api/public/otel/v1/traces",
    );
    expect(INGESTION_DEPRECATION.message).toContain(
      "prefer upgrading to the current Python and JS SDKs",
    );
    expect(INGESTION_DEPRECATION.message).toContain(
      "custom auto-instrumentation",
    );
    expect(INGESTION_DEPRECATION.message).toContain("curl");
    expect(INGESTION_DEPRECATION.message).toContain("POST /api/public/scores");
    expect(INGESTION_DEPRECATION.message).toContain(
      "GET /api/public/v2/observations",
    );
    expect(INGESTION_DEPRECATION.message).toContain(
      "GET /api/public/v2/metrics",
    );
  });

  it("attaches `_deprecation` to POST /ingestion when a mixed batch includes an observation", async () => {
    const response = await makeAPICall(
      "POST",
      "/api/public/ingestion",
      {
        batch: [
          {
            id: randomUUID(),
            type: "score-create",
            timestamp: new Date().toISOString(),
            body: {
              id: randomUUID(),
              name: "score-name",
              traceId: randomUUID(),
              value: 1,
            },
          },
          {
            id: randomUUID(),
            type: "span-create",
            timestamp: new Date().toISOString(),
            body: {
              id: randomUUID(),
              traceId: randomUUID(),
              startTime: new Date().toISOString(),
            },
          },
        ],
      },
      auth,
    );

    expect(response.status).toBe(207);
    expect((response.body as Record<string, unknown>)._deprecation).toEqual(
      INGESTION_DEPRECATION,
    );
  });

  it("omits `_deprecation` from POST /ingestion when the batch is only scores", async () => {
    const response = await makeAPICall(
      "POST",
      "/api/public/ingestion",
      {
        batch: [
          {
            id: randomUUID(),
            type: "score-create",
            timestamp: new Date().toISOString(),
            body: {
              id: randomUUID(),
              name: "score-name",
              traceId: randomUUID(),
              value: 1,
            },
          },
        ],
      },
      auth,
    );

    expect(response.status).toBe(207);
    expect(
      (response.body as Record<string, unknown>)._deprecation,
    ).toBeUndefined();
  });
});