Skip to content

Commit

Permalink
core tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdeichmann committed Dec 11, 2023
1 parent 3f1d4c8 commit bc08b81
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 122 deletions.
14 changes: 7 additions & 7 deletions langfuse-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ abstract class LangfuseCoreStateless {

const parsedBody: CreateLangfuseEventBody = {
id,
startTime: bodyStartTime ?? currentISOTime(),
startTime: bodyStartTime ?? new Date(),
...rest,
};
this.enqueue("observation-create", parsedBody);
Expand All @@ -165,7 +165,7 @@ abstract class LangfuseCoreStateless {

const parsedBody: CreateLangfuseSpanBody = {
id,
startTime: bodyStartTime ?? currentISOTime(),
startTime: bodyStartTime ?? new Date(),
...rest,
};
this.enqueue("observation-create", parsedBody);
Expand All @@ -179,7 +179,7 @@ abstract class LangfuseCoreStateless {

const parsedBody: CreateLangfuseGenerationBody = {
id,
startTime: bodyStartTime ?? currentISOTime(),
startTime: bodyStartTime ?? new Date(),
...rest,
};
this.enqueue("observation-create", parsedBody);
Expand All @@ -200,7 +200,7 @@ abstract class LangfuseCoreStateless {
}

protected updateSpanStateless(body: UpdateLangfuseSpanBody): string {
this.enqueue("observation-create", body);
this.enqueue("observation-update", body);
return body.spanId;
}

Expand Down Expand Up @@ -292,7 +292,7 @@ abstract class LangfuseCoreStateless {
}
}

flushAsync(): Promise<any> {
flushAsync(): Promise<void> {
return new Promise((resolve, reject) => {
this.flush((err, data) => {
return err ? reject(err) : resolve(data);
Expand Down Expand Up @@ -667,7 +667,7 @@ export class LangfuseSpanClient extends LangfuseObservationClient {
...body,
spanId: this.id,
traceId: this.traceId,
endTime: currentISOTime(),
endTime: new Date(),
});
return this;
}
Expand All @@ -692,7 +692,7 @@ export class LangfuseGenerationClient extends LangfuseObservationClient {
...body,
generationId: this.id,
traceId: this.traceId,
endTime: currentISOTime(),
endTime: new Date(),
});
return this;
}
Expand Down
21 changes: 9 additions & 12 deletions langfuse-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export type CreateLangfuseTraceBody = {
id?: string | null;
name?: string | null;
userId?: string | null;
externalId?: string | null;
release?: string | null;
version?: string | null;
metadata?: Record<string, unknown> | null;
Expand All @@ -71,7 +70,7 @@ export type CreateLangfuseEventBody = {
traceId?: string | null;
name?: string | null;
/** Format: date-time */
startTime?: string | null;
startTime?: Date | null;
metadata?: Record<string, unknown> | null;
input?: Record<string, unknown> | null;
output?: Record<string, unknown> | null;
Expand All @@ -82,11 +81,11 @@ export type CreateLangfuseEventBody = {
};
export type CreateLangfuseSpanBody = {
/** Format: date-time */
endTime?: string | null;
endTime?: Date | null;
} & CreateLangfuseEventBody;
export type CreateLangfuseGenerationBody = {
/** Format: date-time */
completionStartTime?: string | null;
completionStartTime?: Date | null;
model?: string | null;
modelParameters?: {
[key: string]: components["schemas"]["MapValue"] | undefined;
Expand All @@ -96,23 +95,21 @@ export type CreateLangfuseGenerationBody = {
usage?: components["schemas"]["Usage"];
} & CreateLangfuseSpanBody;
export type CreateLangfuseScoreBody = {
id: string;
id?: string | null;
traceId: string;
name: string;
/** Format: double */
value: number;
observationId?: string | null;
/** Format: date-time */
timestamp?: string;
comment?: string | null;
};
export type UpdateLangfuseSpanBody = {
spanId: string;
traceId?: string | null;
/** Format: date-time */
startTime?: string | null;
startTime?: Date | null;
/** Format: date-time */
endTime?: string | null;
endTime?: Date | null;
name?: string | null;
metadata?: Record<string, unknown> | null;
input?: Record<string, unknown> | null;
Expand All @@ -126,11 +123,11 @@ export type UpdateLangfuseGenerationBody = {
traceId?: string | null;
name?: string | null;
/** Format: date-time */
startTime?: string | null;
startTime?: Date | null;
/** Format: date-time */
endTime?: string | null;
endTime?: Date | null;
/** Format: date-time */
completionStartTime?: string | null;
completionStartTime?: Date | null;
model?: string | null;
modelParameters?: {
[key: string]: components["schemas"]["MapValue"] | undefined;
Expand Down
4 changes: 2 additions & 2 deletions langfuse-core/test/langfuse.debug.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("Langfuse Core", () => {
[langfuse, mocks] = createTestClient({
publicKey: "pk-lf-111",
secretKey: "sk-lf-111",
flushAt: 3,
flushAt: 1,
});
});

Expand All @@ -29,7 +29,7 @@ describe("Langfuse Core", () => {
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(
"Langfuse Debug",
"createTrace",
"trace-create",
expect.objectContaining({
name: "test-trace2",
})
Expand Down
82 changes: 59 additions & 23 deletions langfuse-core/test/langfuse.end.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("Langfuse Core", () => {
[langfuse, mocks] = createTestClient({
publicKey: "pk-lf-111",
secretKey: "sk-lf-111",
flushAt: 1000,
flushAt: 1,
});
});

Expand All @@ -37,14 +37,23 @@ describe("Langfuse Core", () => {

expect(mocks.fetch).toHaveBeenCalledTimes(3);
const [url, options] = mocks.fetch.mock.calls[2];
expect(url).toMatch("https://cloud.langfuse.com/api/public/spans");
expect(options.method).toBe("PATCH");
expect(url).toMatch("https://cloud.langfuse.com/api/public/ingestion");
expect(options.method).toBe("POST");
const body = parseBody(mocks.fetch.mock.calls[2]);
expect(body).toMatchObject({
traceId: trace.id,
spanId: span.id,
output: { text: "test-output" },
endTime: "2022-01-01T00:00:00.000Z",
batch: [
{
id: expect.any(String),
type: "observation-update",
timestamp: expect.any(String),
body: {
traceId: trace.id,
spanId: span.id,
output: { text: "test-output" },
endTime: "2022-01-01T00:00:00.000Z",
},
},
],
});
});

Expand All @@ -63,13 +72,22 @@ describe("Langfuse Core", () => {

expect(mocks.fetch).toHaveBeenCalledTimes(3);
const [url, options] = mocks.fetch.mock.calls[2];
expect(url).toMatch("https://cloud.langfuse.com/api/public/spans");
expect(options.method).toBe("PATCH");
expect(url).toMatch("https://cloud.langfuse.com/api/public/ingestion");
expect(options.method).toBe("POST");
const body = parseBody(mocks.fetch.mock.calls[2]);
expect(body).toEqual({
traceId: trace.id,
spanId: span.id,
endTime: "2022-01-01T00:00:00.000Z",
batch: [
{
id: expect.any(String),
type: "observation-update",
timestamp: expect.any(String),
body: {
traceId: trace.id,
spanId: span.id,
endTime: "2022-01-01T00:00:00.000Z",
},
},
],
});
});
});
Expand All @@ -92,14 +110,23 @@ describe("Langfuse Core", () => {

expect(mocks.fetch).toHaveBeenCalledTimes(3);
const [url, options] = mocks.fetch.mock.calls[2];
expect(url).toMatch("https://cloud.langfuse.com/api/public/generations");
expect(options.method).toBe("PATCH");
expect(url).toMatch("https://cloud.langfuse.com/api/public/ingestion");
expect(options.method).toBe("POST");
const body = parseBody(mocks.fetch.mock.calls[2]);
expect(body).toMatchObject({
traceId: trace.id,
generationId: generation.id,
version: "1.0.0",
endTime: "2022-01-01T00:00:00.000Z",
batch: [
{
id: expect.any(String),
type: "observation-update",
timestamp: expect.any(String),
body: {
traceId: trace.id,
generationId: generation.id,
version: "1.0.0",
endTime: "2022-01-01T00:00:00.000Z",
},
},
],
});
});

Expand All @@ -118,13 +145,22 @@ describe("Langfuse Core", () => {

expect(mocks.fetch).toHaveBeenCalledTimes(3);
const [url, options] = mocks.fetch.mock.calls[2];
expect(url).toMatch("https://cloud.langfuse.com/api/public/generations");
expect(options.method).toBe("PATCH");
expect(url).toMatch("https://cloud.langfuse.com/api/public/ingestion");
expect(options.method).toBe("POST");
const body = parseBody(mocks.fetch.mock.calls[2]);
expect(body).toMatchObject({
traceId: trace.id,
generationId: generation.id,
endTime: "2022-01-01T00:00:00.000Z",
batch: [
{
id: expect.any(String),
type: "observation-update",
timestamp: expect.any(String),
body: {
traceId: trace.id,
generationId: generation.id,
endTime: "2022-01-01T00:00:00.000Z",
},
},
],
});
});
});
Expand Down
10 changes: 5 additions & 5 deletions langfuse-core/test/langfuse.enqueue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ describe("Langfuse Core", () => {
expect(langfuse.getPersistedProperty(LangfusePersistedProperty.Queue)).toHaveLength(1);

const item = langfuse.getPersistedProperty<LangfuseQueueItem[]>(LangfusePersistedProperty.Queue)?.pop();
console.log(item);

expect(item).toMatchObject({
apiRoute: "/api/public/traces",
body: {
name: "test-trace",
id: "123456789",
},
id: expect.any(String),
type: "trace-create",
timestamp: expect.any(String),
body: { id: "123456789", name: "test-trace" },
});

expect(mocks.fetch).not.toHaveBeenCalled();
Expand Down
4 changes: 2 additions & 2 deletions langfuse-core/test/langfuse.flush.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("Langfuse Core", () => {

it("doesn't fail when queue is empty", async () => {
jest.useRealTimers();
await expect(langfuse.flushAsync()).resolves.toEqual([]);
await expect(langfuse.flushAsync()).resolves.not.toThrow();
});

it("flush messsages once called", async () => {
Expand Down Expand Up @@ -82,7 +82,7 @@ describe("Langfuse Core", () => {
expect(mocks.fetch).toHaveBeenCalledTimes(0);

jest.advanceTimersByTime(300);
expect(mocks.fetch).toHaveBeenCalledTimes(3);
expect(mocks.fetch).toHaveBeenCalledTimes(1);
});
});
});
2 changes: 1 addition & 1 deletion langfuse-core/test/langfuse.init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("Langfuse Core", () => {
secretKey: "sk-lf-111",
publicKey: "pk-lf-111",
baseUrl: "https://cloud.langfuse.com",
flushAt: 1,
flushAt: 20,
flushInterval: 10000,
});
});
Expand Down
8 changes: 4 additions & 4 deletions langfuse-core/test/langfuse.listeners.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("Langfuse Core", () => {
[langfuse, mocks] = createTestClient({
publicKey: "pk-lf-111",
secretKey: "sk-lf-111",
flushAt: 10,
flushAt: 1,
});
});

Expand All @@ -26,8 +26,8 @@ describe("Langfuse Core", () => {
const mock = jest.fn();
const mockOther = jest.fn();
const mockOther2 = jest.fn();
langfuse.on("createTrace", mock);
langfuse.on("createTrace", mockOther);
langfuse.on("trace-create", mock);
langfuse.on("trace-create", mockOther);
langfuse.on("somethingElse", mockOther2);

langfuse.trace({ name: "test-trace" });
Expand All @@ -39,7 +39,7 @@ describe("Langfuse Core", () => {

it("should unsubscribe when called", () => {
const mock = jest.fn();
const unsubscribe = langfuse.on("createTrace", mock);
const unsubscribe = langfuse.on("trace-create", mock);

langfuse.trace({ name: "test-trace1" });
expect(mock).toHaveBeenCalledTimes(1);
Expand Down
Loading

0 comments on commit bc08b81

Please sign in to comment.