-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add tests for factor and config
- Loading branch information
1 parent
a4ff997
commit 4d49c24
Showing
18 changed files
with
506 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
# SOON | ||
# SOON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
//todo | ||
export * from "./src/rag-chat"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { Upstash } from "@upstash/sdk"; | ||
import { describe, expect, test } from "bun:test"; | ||
import type { ClientFactoryConfig } from "./client-factory"; | ||
import { ClientFactory } from "./client-factory"; | ||
|
||
describe("ClientFactory Tests", () => { | ||
const config: ClientFactoryConfig = { | ||
email: process.env.UPSTASH_EMAIL!, | ||
token: process.env.UPSTASH_TOKEN!, | ||
region: "us-east-1", | ||
redis: "test-rag-chat-client-factor-redis", | ||
vector: "test-rag-chat-client-factor-vector", | ||
}; | ||
const upstash = new Upstash(config); | ||
const clientFactory = new ClientFactory(config); | ||
|
||
test( | ||
"Redis client initialization", | ||
async () => { | ||
const initialized = await clientFactory.init({ redis: true }); | ||
expect(initialized).toHaveProperty("redis"); | ||
expect(initialized.vector).toBeUndefined(); | ||
|
||
await upstash.deleteRedisDatabase("test-rag-chat-client-factor-redis"); | ||
}, | ||
{ timeout: 20_000 } | ||
); | ||
|
||
test( | ||
"Vector client initialization", | ||
async () => { | ||
const initialized = await clientFactory.init({ vector: true }); | ||
expect(initialized).toHaveProperty("vector"); | ||
expect(initialized.redis).toBeUndefined(); | ||
|
||
await upstash.deleteVectorIndex("test-rag-chat-client-factor-vector"); | ||
}, | ||
{ timeout: 20_000 } | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type { Index, Redis } from "@upstash/sdk"; | ||
import type { Config } from "./config"; | ||
|
||
import { Upstash } from "@upstash/sdk"; | ||
import { RedisClient } from "./clients/redis"; | ||
import { VectorClient } from "./clients/vector"; | ||
import { InternalUpstashError } from "./error/internal"; | ||
|
||
export type ClientFactoryConfig = Pick<Config, "email" | "token" | "vector" | "redis" | "region">; | ||
export class ClientFactory { | ||
private upstashSDK: Upstash; | ||
private config: ClientFactoryConfig; | ||
|
||
constructor(config: ClientFactoryConfig) { | ||
this.upstashSDK = new Upstash(config); | ||
this.config = config; | ||
} | ||
private createVectorClient(): Promise<Index | undefined> { | ||
return new VectorClient({ | ||
upstashSDK: this.upstashSDK, | ||
indexNameOrInstance: this.config.vector, | ||
region: this.config.region, | ||
}).getVectorClient(); | ||
} | ||
|
||
private createRedisClient(): Promise<Redis | undefined> { | ||
return new RedisClient({ | ||
upstashSDK: this.upstashSDK, | ||
redisDbNameOrInstance: this.config.redis, | ||
region: this.config.region, | ||
}).getRedisClient(); | ||
} | ||
|
||
/** Allows initting only desired clients. Reason is that in some cases like `HistoryService` we only require | ||
* redis client, but in `RAGChat` we need all three clients to work efficiently. | ||
* | ||
* @default redis: false | ||
* @default vector: false | ||
* @default ratelimit: false | ||
*/ | ||
async init<TInit extends { redis?: boolean; vector?: boolean }>( | ||
options: TInit = { redis: false, vector: false } as TInit | ||
): Promise<{ | ||
redis: TInit["redis"] extends true ? Redis : undefined; | ||
vector: TInit["vector"] extends true ? Index : undefined; | ||
}> { | ||
let redis: Redis | undefined; | ||
let vector: Index | undefined; | ||
|
||
if (options.redis) { | ||
redis = await this.createRedisClient(); | ||
if (!redis) { | ||
throw new InternalUpstashError("Couldn't initialize Redis client"); | ||
} | ||
} | ||
|
||
if (options.vector) { | ||
vector = await this.createVectorClient(); | ||
if (!vector) { | ||
throw new InternalUpstashError("Couldn't initialize Vector client"); | ||
} | ||
} | ||
|
||
return { redis, vector } as { | ||
redis: TInit["redis"] extends true ? Redis : undefined; | ||
vector: TInit["vector"] extends true ? Index : undefined; | ||
}; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.