Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw committed Sep 18, 2024
1 parent 69c1ae0 commit 2f97893
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 8 deletions.
23 changes: 15 additions & 8 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3127,17 +3127,24 @@ export class Client {
} = {}
): AsyncIterableIterator<AnnotationQueue> {
const { queueIds, name, nameContains, limit } = options;
const params: Record<string, any> = {
ids: queueIds?.map((id, i) => assertUuid(id, `queueIds[${i}]`)),
name,
name_contains: nameContains,
limit: limit !== undefined ? Math.min(limit, 100) : 100,
};
const params = new URLSearchParams();
if (queueIds) {
queueIds.forEach((id, i) => {
assertUuid(id, `queueIds[${i}]`);
params.append("ids", id);
});
}
if (name) params.append("name", name);
if (nameContains) params.append("name_contains", nameContains);
params.append(
"limit",
(limit !== undefined ? Math.min(limit, 100) : 100).toString()
);

let count = 0;
for await (const queue of this._getPaginated(
"/annotation-queues",
params as URLSearchParams
params
)) {
yield queue as unknown as AnnotationQueue;
count++;
Expand Down Expand Up @@ -3441,7 +3448,7 @@ export class Client {
ListCommitsResponse
>(
`/commits/${promptOwnerAndName}/`,
{} as URLSearchParams,
new URLSearchParams(),
(res) => res.commits
)) {
yield* commits;
Expand Down
90 changes: 90 additions & 0 deletions js/src/tests/client.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1147,3 +1147,93 @@ test("clonePublicDataset method can clone a dataset", async () => {
}
}
});

test("annotationqueue crud", async () => {
const client = new Client();
const queueName = `test-queue-${uuidv4().substring(0, 8)}`;
const projectName = `test-project-${uuidv4().substring(0, 8)}`;
const queueId = uuidv4();

try {
// 1. Create an annotation queue
const queue = await client.createAnnotationQueue({
name: queueName,
description: "Initial description",
queueId,
});
expect(queue).toBeDefined();
expect(queue.name).toBe(queueName);

// 1a. Get the annotation queue
const fetchedQueue = await client.readAnnotationQueue(queue.id);
expect(fetchedQueue).toBeDefined();
expect(fetchedQueue.name).toBe(queueName);

// 1b. List annotation queues and check nameContains
const listedQueues = await toArray(
client.listAnnotationQueues({ nameContains: queueName })
);
expect(listedQueues.length).toBeGreaterThan(0);
expect(listedQueues.some((q) => q.id === queue.id)).toBe(true);

// 2. Create a run in a random project
await client.createProject({ projectName });
const runId = uuidv4();
await client.createRun({
id: runId,
name: "Test Run",
run_type: "chain",
inputs: { foo: "bar" },
outputs: { baz: "qux" },
project_name: projectName,
});

// Wait for run to be found in the db
const maxWaitTime = 30000; // 30 seconds
const startTime = Date.now();
let foundRun = null;

while (Date.now() - startTime < maxWaitTime) {
try {
foundRun = await client.readRun(runId);
if (foundRun) break;
} catch (error) {
// If run is not found, getRun might throw an error
// We'll ignore it and keep trying
}
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second before trying again
}

if (!foundRun) {
throw new Error(
`Run with ID ${runId} not found after ${maxWaitTime / 1000} seconds`
);
}

// 3. Add the run to the annotation queue
await client.addRunsToAnnotationQueue(fetchedQueue.id, [runId]);

// 4. Check that the run is in the annotation queue
const queuedRuns = await toArray(
client.listRunsFromAnnotationQueue(queue.id)
);
expect(queuedRuns.some((r) => r.id === runId)).toBe(true);

// 5. Update the annotation queue description and check that it is updated
const newDescription = "Updated description";
await client.updateAnnotationQueue(queue.id, {
name: queueName,
description: newDescription,
});
const updatedQueue = await client.readAnnotationQueue(queue.id);
expect(updatedQueue.description).toBe(newDescription);
} finally {
// 6. Delete the annotation queue
await client.deleteAnnotationQueue(queueId);

// Clean up the project
if (await client.hasProject({ projectName })) {
await client.deleteProject({ projectName });
}
}
});

0 comments on commit 2f97893

Please sign in to comment.