Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search to SDK #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ export class Catalog {
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async searchDocuments(query: string): Promise<any> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do decide to add this to the SDK we need to add strongly typed output if possible. In general we should never use any unless we have a very good reason.

this.checkDeleted();

const res = await this.apiClient.GET(
`/catalogs/${this.name}/search?query=${query}`,
);
if (res.status !== 200) {
throw new Error(`Failed to search documents: ${res.statusText}`);
}

return await res.json();
}

async delete() {
this.checkDeleted();
this.deleted = true;
Expand Down
67 changes: 14 additions & 53 deletions document.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { expect, test } from "vitest";
import { CatalogConfig } from "./catalog";
import { expect, test, beforeEach, afterEach } from "vitest";
import { CatalogConfig, Catalog } from "./catalog";
import { FileDocument, JSONDocument, TextDocument } from "./document";

test("Test upsertDocuments inline text batch", { timeout: 10000 }, async () => {
const catalogName = `catalog-${Math.floor(Math.random() * 10000)}`;
let catalog: Catalog;

beforeEach(async () => {
const config: CatalogConfig = {
description: "foo bar",
instructions: ["a", "b"],
};

const catalog = await testClient.configureCatalog(catalogName, config);
const catalogName = `catalog-${Math.floor(Math.random() * 10000)}`;
catalog = await testClient.configureCatalog(catalogName, config);
});

afterEach(async () => {
if (catalog) {
await catalog.delete();
}
});

test("Test upsertDocuments inline text batch", { timeout: 10000 }, async () => {
const docs: TextDocument[] = [
{
documentId: "1",
Expand All @@ -33,20 +42,9 @@ test("Test upsertDocuments inline text batch", { timeout: 10000 }, async () => {

const docCount = await catalog.documentCount();
expect(docCount).toBe(2);

await catalog.delete();
});

test("Test upsertDocuments inline JSON batch", { timeout: 10000 }, async () => {
const catalogName = `catalog-${Math.floor(Math.random() * 10000)}`;

const config: CatalogConfig = {
description: "foo bar",
instructions: ["a", "b"],
};

const catalog = await testClient.configureCatalog(catalogName, config);

const docs: JSONDocument[] = [
{
documentId: "1",
Expand Down Expand Up @@ -74,23 +72,12 @@ test("Test upsertDocuments inline JSON batch", { timeout: 10000 }, async () => {

const docCount = await catalog.documentCount();
expect(docCount).toBe(2);

await catalog.delete();
});

test(
"Test upsertDocuments with files and catalog.truncate",
{ timeout: 20000 },
async () => {
const catalogName = `catalog-${Math.floor(Math.random() * 10000)}`;

const config: CatalogConfig = {
description: "foo bar",
instructions: ["a", "b"],
};

const catalog = await testClient.configureCatalog(catalogName, config);

const docs: FileDocument[] = [
{
documentId: "1",
Expand All @@ -117,8 +104,6 @@ test(

docCount = await catalog.documentCount();
expect(docCount).toBe(0);

await catalog.delete();
},
);

Expand Down Expand Up @@ -164,20 +149,9 @@ test("Test update documents", { timeout: 10000 }, async () => {

docCount = await catalog.documentCount();
expect(docCount).toBe(2);

await catalog.delete();
});

test("Test get and delete documents", { timeout: 10000 }, async () => {
const catalogName = `catalog-${Math.floor(Math.random() * 10000)}`;

const config: CatalogConfig = {
description: "foo bar",
instructions: ["a", "b"],
};

const catalog = await testClient.configureCatalog(catalogName, config);

const docs: TextDocument[] = [
{
documentId: "1",
Expand Down Expand Up @@ -212,20 +186,9 @@ test("Test get and delete documents", { timeout: 10000 }, async () => {

docCount = await catalog.documentCount();
expect(docCount).toBe(1);

await catalog.delete();
});

test("Test catalog.listDocuments", { timeout: 10000 }, async () => {
const catalogName = `catalog-${Math.floor(Math.random() * 10000)}`;

const config: CatalogConfig = {
description: "foo bar",
instructions: ["a", "b"],
};

const catalog = await testClient.configureCatalog(catalogName, config);

const docs: JSONDocument[] = [];

for (let i = 0; i < 70; i++) {
Expand Down Expand Up @@ -259,6 +222,4 @@ test("Test catalog.listDocuments", { timeout: 10000 }, async () => {
expect(listDocsResult.documents.length).toBe(70);
listDocsResult = await listDocsResult.nextPage();
expect(listDocsResult.documents.length).toBe(0);

await catalog.delete();
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"test:prod": "pulumi env run npm-test npm run test",
"test:fast:dev": "CORTEX_API_URL=http://localhost:3001 pulumi env run npm-test npm run test:fast",
"test:fast:prod": "pulumi env run npm-test npm run test:fast",
"test:indexers:dev": "CORTEX_API_URL=http://localhost:3001 pulumi env run npm-test vitest indexers.test.ts"
"test:indexers:dev": "CORTEX_API_URL=http://localhost:3001 pulumi env run npm-test vitest indexers.test.ts",
"test:document:dev": "CORTEX_API_URL=http://localhost:3001 pulumi env run npm-test vitest document.test.ts"
},
"keywords": [],
"author": "",
Expand Down