Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw committed Feb 14, 2024
1 parent 6f33ea2 commit 47d0c71
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 280 deletions.
4 changes: 2 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"devDependencies": {
"@babel/preset-env": "^7.22.4",
"@jest/globals": "^29.5.0",
"@langchain/core": "^0.1.28",
"@tsconfig/recommended": "^1.0.2",
"@types/jest": "^29.5.1",
"@typescript-eslint/eslint-plugin": "^5.59.8",
Expand All @@ -75,7 +76,6 @@
"eslint-plugin-no-instanceof": "^1.0.1",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.5.0",
"langchain": "^0.0.147",
"prettier": "^2.8.8",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
Expand Down Expand Up @@ -122,4 +122,4 @@
},
"./package.json": "./package.json"
}
}
}
31 changes: 31 additions & 0 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,37 @@ export class Client {
return result as TracerSession;
}

public async hasProject({
projectId,
projectName,
}: {
projectId?: string;
projectName?: string;
}): Promise<boolean> {
let path = "/sessions";
const params = new URLSearchParams();
if (projectId !== undefined && projectName !== undefined) {
throw new Error("Must provide either projectName or projectId, not both");
} else if (projectId !== undefined) {
assertUuid(projectId);
path += `/${projectId}`;
} else if (projectName !== undefined) {
params.append("name", projectName);
} else {
throw new Error("Must provide projectName or projectId");
}
const response = await this.caller.call(
fetch,
`${this.apiUrl}${path}?${params}`,
{
method: "HEAD",
headers: this.headers,
signal: AbortSignal.timeout(this.timeout_ms),
}
);
return response.ok;
}

public async readProject({
projectId,
projectName,
Expand Down
2 changes: 1 addition & 1 deletion js/src/tests/client.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Dataset, Run } from "../schemas.js";
import { FunctionMessage, HumanMessage } from "langchain/schema";
import { FunctionMessage, HumanMessage } from "@langchain/core/messages";

import { Client } from "../client.js";
import { v4 as uuidv4 } from "uuid";
Expand Down
85 changes: 85 additions & 0 deletions js/src/tests/run_trees.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ async function waitUntil(
throw new Error("Timeout");
}

async function pollRunsUntilCount(
client: Client,
projectName: string,
count: number
): Promise<void> {
await waitUntil(
async () => {
try {
const runs = await toArray(client.listRuns({ projectName }));
return runs.length === count;
} catch (e) {
return false;
}
},
60_000, // Wait up to 60 seconds
3000 // every 3 second
);
}

test.concurrent(
"Test post and patch run",
async () => {
Expand Down Expand Up @@ -130,3 +149,69 @@ test.concurrent(
},
120_000
);

test.concurrent(
"Test list runs multi project",
async () => {
const projectNames = [
"__My JS Tracer Project - test_list_runs_multi_project",
"__My JS Tracer Project - test_list_runs_multi_project2",
];

try {
const langchainClient = new Client();

for (const project of projectNames) {
if (await langchainClient.hasProject({ projectName: project })) {
await langchainClient.deleteProject({ projectName: project });
}
}

const parentRunConfig: RunTreeConfig = {
name: "parent_run",
inputs: { text: "hello world" },
project_name: projectNames[0],
client: langchainClient,
};

const parent_run = new RunTree(parentRunConfig);
await parent_run.postRun();
await parent_run.end({ output: "Completed: foo" });
await parent_run.patchRun();

const parentRunConfig2: RunTreeConfig = {
name: "parent_run",
inputs: { text: "hello world" },
project_name: projectNames[1],
client: langchainClient,
};

const parent_run2 = new RunTree(parentRunConfig2);
await parent_run2.postRun();
await parent_run2.end({ output: "Completed: foo" });
await parent_run2.patchRun();
await pollRunsUntilCount(langchainClient, projectNames[0], 1);
await pollRunsUntilCount(langchainClient, projectNames[1], 1);

const runsIter = langchainClient.listRuns({
projectName: projectNames,
});
const runs = await toArray(runsIter);

expect(runs.length).toBe(2);
expect(
runs.every((run) => run?.outputs?.["output"] === "Completed: foo")
).toBe(true);
expect(runs[0].session_id).not.toBe(runs[1].session_id);
} finally {
const langchainClient = new Client();

for (const project of projectNames) {
if (await langchainClient.hasProject({ projectName: project })) {
await langchainClient.deleteProject({ projectName: project });
}
}
}
},
120_000
);
Loading

0 comments on commit 47d0c71

Please sign in to comment.