Skip to content

Commit

Permalink
More JS testing (#1591)
Browse files Browse the repository at this point in the history
* More JS testing

* Cleanup
  • Loading branch information
NolanTrem authored Nov 14, 2024
1 parent b68421f commit ae47d79
Show file tree
Hide file tree
Showing 9 changed files with 2,621 additions and 1,106 deletions.
4 changes: 2 additions & 2 deletions js/sdk/__tests__/DocumentsIntegrationSuperUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("r2rClient V3 Documents Integration Tests", () => {

expect(response.results.document_id).toBeDefined();
documentId = response.results.document_id;
});
}, 10000);

test("Create document with content", async () => {
const response = await client.documents.create({
Expand All @@ -37,7 +37,7 @@ describe("r2rClient V3 Documents Integration Tests", () => {
});

expect(response.results.document_id).toBeDefined();
});
}, 10000);

test("Update document", async () => {
const response = await client.documents.update({
Expand Down
140 changes: 140 additions & 0 deletions js/sdk/__tests__/RetrievalIntegrationSuperUser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { r2rClient } from "../src/index";
const fs = require("fs");
import { describe, test, beforeAll, expect } from "@jest/globals";

const baseUrl = "http://localhost:7272";

const messages = [
{
role: "system" as const,
content: "You are a helpful assistant.",
},
{
role: "user" as const,
content: "Tell me about Sonia.",
},
];

/**
* sonia.txt will have an id of 28ce9a4c-4d15-5287-b0c6-67834b9c4546
*/
describe("r2rClient V3 Documents Integration Tests", () => {
let client: r2rClient;
let documentId: string;

beforeAll(async () => {
client = new r2rClient(baseUrl);
await client.users.login({
email: "[email protected]",
password: "change_me_immediately",
});
});

async function readStream(
stream: ReadableStream<Uint8Array>,
): Promise<string> {
const reader = stream.getReader();
let result = "";

while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
result += new TextDecoder().decode(value);
}

return result;
}

test("Create document with file path", async () => {
const response = await client.documents.create({
file: { path: "examples/data/sonia.txt", name: "sonia.txt" },
metadata: { title: "sonia.txt" },
});

expect(response.results.document_id).toBeDefined();
documentId = response.results.document_id;
}, 10000);

test("Search documents with no parameters", async () => {
const response = await client.retrieval.search({ query: "Sonia" });

expect(response.results).toBeDefined();
});

test("RAG with no parameters", async () => {
const response = await client.retrieval.rag({ query: "Sonia" });

expect(response.results).toBeDefined();
}, 30000);

test("Streaming RAG", async () => {
const stream = await client.retrieval.rag({
query: "Sonia",
generation_config: {
stream: true,
},
});

expect(stream).toBeInstanceOf(ReadableStream);
const content = await readStream(stream);
expect(content).toBeTruthy();
expect(typeof content).toBe("string");
expect(content.length).toBeGreaterThan(0);
}, 30000);

test("Agent with no parameters", async () => {
const response = await client.retrieval.agent({
messages: messages,
});

expect(response.results).toBeDefined();
}, 30000);

test("Streaming RAG", async () => {
const stream = await client.retrieval.agent({
messages: messages,
generation_config: {
stream: true,
},
});

expect(stream).toBeInstanceOf(ReadableStream);
const content = await readStream(stream);
expect(content).toBeTruthy();
expect(typeof content).toBe("string");
expect(content.length).toBeGreaterThan(0);
}, 30000);

// test("Completion with no parameters", async () => {
// const response = await client.retrieval.completion({
// messages: messages,
// });

// expect(response.results).toBeDefined();
// }, 30000);

// test("Streaming Completion", async () => {
// const stream = await client.retrieval.completion({
// messages: messages,
// generation_config: {
// stream: true,
// },
// });

// expect(stream).toBeInstanceOf(ReadableStream);
// const content = await readStream(stream);
// expect(content).toBeTruthy();
// expect(typeof content).toBe("string");
// expect(content.length).toBeGreaterThan(0);
// }, 30000);

test("Delete untitled document", async () => {
const response = await client.documents.delete({
id: "28ce9a4c-4d15-5287-b0c6-67834b9c4546",
});

expect(response.results).toBeDefined();
});
});
2 changes: 1 addition & 1 deletion js/sdk/__tests__/r2rV2ClientIntegrationSuperUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe("r2rClient Integration Tests", () => {
const files = ["examples/data/folder"];

await expect(client.ingestFiles(files)).resolves.not.toThrow();
});
}, 10000);

test("Update files", async () => {
const updated_file = [
Expand Down
39 changes: 39 additions & 0 deletions js/sdk/examples/data/sonia.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
On the canal bank near the bridge and not two houses away from the one
where Sonia lodged, there was a crowd of people, consisting principally
of gutter children. The hoarse broken voice of Katerina Ivanovna could
be heard from the bridge, and it certainly was a strange spectacle
likely to attract a street crowd. Katerina Ivanovna in her old dress
with the green shawl, wearing a torn straw hat, crushed in a hideous way
on one side, was really frantic. She was exhausted and breathless. Her
wasted consumptive face looked more suffering than ever, and indeed out
of doors in the sunshine a consumptive always looks worse than at home.
But her excitement did not flag, and every moment her irritation grew
more intense. She rushed at the children, shouted at them, coaxed
them, told them before the crowd how to dance and what to sing, began
explaining to them why it was necessary, and driven to desperation by
their not understanding, beat them.... Then she would make a rush at the
crowd; if she noticed any decently dressed person stopping to look, she
immediately appealed to him to see what these children “from a genteel,
one may say aristocratic, house” had been brought to. If she heard
laughter or jeering in the crowd, she would rush at once at the scoffers
and begin squabbling with them. Some people laughed, others shook their
heads, but everyone felt curious at the sight of the madwoman with the
frightened children. The frying-pan of which Lebeziatnikov had spoken
was not there, at least Raskolnikov did not see it. But instead of
rapping on the pan, Katerina Ivanovna began clapping her wasted hands,
when she made Lida and Kolya dance and Polenka sing. She too joined in
the singing, but broke down at the second note with a fearful cough,
which made her curse in despair and even shed tears. What made her most
furious was the weeping and terror of Kolya and Lida. Some effort had
been made to dress the children up as street singers are dressed. The
boy had on a turban made of something red and white to look like a Turk.
There had been no costume for Lida; she simply had a red knitted cap,
or rather a night cap that had belonged to Marmeladov, decorated with
a broken piece of white ostrich feather, which had been Katerina
Ivanovna’s grandmother’s and had been preserved as a family possession.
Polenka was in her everyday dress; she looked in timid perplexity at her
mother, and kept at her side, hiding her tears. She dimly realised her
mother’s condition, and looked uneasily about her. She was terribly
frightened of the street and the crowd. Sonia followed Katerina
Ivanovna, weeping and beseeching her to return home, but Katerina
Ivanovna was not to be persuaded.
Loading

0 comments on commit ae47d79

Please sign in to comment.