-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
feat: [Issue-185] Token Provider Tests #365
Changes from 11 commits
64f3231
9042e7b
d778e60
c93510c
4d7e0aa
a3eb3ed
14f1cff
7940692
780cfb2
5a338d0
055c609
5e24871
caa13f7
6fbde25
ff28e99
329db11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v23.1.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
TEST_DATABASE_CLIENT=sqlite | ||
NODE_ENV=test | ||
MAIN_WALLET_ADDRESS=TEST_MAIN_WALLET_ADDRESS_VALUE | ||
OPENAI_API_KEY=TEST_OPENAI_API_KEY_VALUE |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules | ||
dist | ||
elizaConfig.yaml | ||
custom_actions/ | ||
custom_actions/ | ||
cache/ |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
describe("Basic Test Suite", () => { | ||
it("should run a basic test", () => { | ||
expect(true).toBe(true); | ||
}); | ||
|
||
it("should have access to environment variables", () => { | ||
expect(process.env.NODE_ENV).toBe("test"); | ||
expect(process.env.TEST_DATABASE_CLIENT).toBe("sqlite"); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { type UUID } from "@ai16z/eliza/src/types.ts"; | ||
|
||
export const SERVER_URL = "http://localhost:7998"; | ||
export const SUPABASE_URL = "https://pronvzrzfwsptkojvudd.supabase.co"; | ||
export const SUPABASE_ANON_KEY = | ||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InByb252enJ6ZndzcHRrb2p2dWRkIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDY4NTYwNDcsImV4cCI6MjAyMjQzMjA0N30.I6_-XrqssUb2SWYg5DjsUqSodNS3_RPoET3-aPdqywM"; | ||
export const TEST_EMAIL = "testuser123@gmail.com"; | ||
export const TEST_PASSWORD = "testuser123@gmail.com"; | ||
export const TEST_EMAIL_2 = "testuser234@gmail.com"; | ||
export const TEST_PASSWORD_2 = "testuser234@gmail.com"; | ||
|
||
export const zeroUuid = "00000000-0000-0000-0000-000000000000" as UUID; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { SqliteDatabaseAdapter, loadVecExtensions } from "@ai16z/adapter-sqlite"; | ||
import { SqlJsDatabaseAdapter } from "@ai16z/adapter-sqljs"; | ||
import { SupabaseDatabaseAdapter } from "@ai16z/adapter-supabase"; | ||
import { DatabaseAdapter } from "../database.ts"; | ||
import { AgentRuntime } from "../runtime.ts"; | ||
import { | ||
Action, | ||
Evaluator, | ||
ModelProviderName, | ||
Provider, | ||
} from "../types.ts"; | ||
import { | ||
SUPABASE_ANON_KEY, | ||
SUPABASE_URL, | ||
TEST_EMAIL, | ||
TEST_PASSWORD, | ||
zeroUuid, | ||
} from "./constants.ts"; | ||
import { User } from "./types.ts"; | ||
|
||
export async function createRuntime({ | ||
env, | ||
conversationLength, | ||
evaluators = [], | ||
actions = [], | ||
providers = [], | ||
}: { | ||
env?: Record<string, string> | NodeJS.ProcessEnv; | ||
conversationLength?: number; | ||
evaluators?: Evaluator[]; | ||
actions?: Action[]; | ||
providers?: Provider[]; | ||
}) { | ||
let adapter: DatabaseAdapter; | ||
let user: User; | ||
let session: { | ||
user: User; | ||
}; | ||
|
||
switch (env?.TEST_DATABASE_CLIENT as string) { | ||
case "sqljs": | ||
{ | ||
const module = await import("sql.js"); | ||
|
||
const initSqlJs = module.default; | ||
|
||
// SQLite adapter | ||
const SQL = await initSqlJs({}); | ||
const db = new SQL.Database(); | ||
|
||
adapter = new SqlJsDatabaseAdapter(db); | ||
|
||
// Load sqlite-vss | ||
loadVecExtensions((adapter as SqlJsDatabaseAdapter).db); | ||
// Create a test user and session | ||
user = { | ||
id: zeroUuid, | ||
email: "test@example.com", | ||
} as User; | ||
session = { | ||
user: user, | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: user can be inlined here |
||
} | ||
break; | ||
case "supabase": { | ||
const module = await import("@supabase/supabase-js"); | ||
|
||
const { createClient } = module; | ||
|
||
const supabase = createClient( | ||
env?.SUPABASE_URL ?? SUPABASE_URL, | ||
env?.SUPABASE_SERVICE_API_KEY ?? SUPABASE_ANON_KEY | ||
); | ||
|
||
const { data } = await supabase.auth.signInWithPassword({ | ||
email: TEST_EMAIL!, | ||
password: TEST_PASSWORD!, | ||
}); | ||
|
||
user = data.user as User; | ||
session = data.session as unknown as { user: User }; | ||
|
||
if (!session) { | ||
const response = await supabase.auth.signUp({ | ||
email: TEST_EMAIL!, | ||
password: TEST_PASSWORD!, | ||
}); | ||
|
||
// Change the name of the user | ||
const { error } = await supabase | ||
.from("accounts") | ||
.update({ name: "Test User" }) | ||
.eq("id", response.data.user?.id); | ||
|
||
if (error) { | ||
throw new Error( | ||
"Create runtime error: " + JSON.stringify(error) | ||
); | ||
} | ||
|
||
user = response.data.user as User; | ||
session = response.data.session as unknown as { user: User }; | ||
} | ||
|
||
adapter = new SupabaseDatabaseAdapter( | ||
env?.SUPABASE_URL ?? SUPABASE_URL, | ||
env?.SUPABASE_SERVICE_API_KEY ?? SUPABASE_ANON_KEY | ||
); | ||
} | ||
case "sqlite": | ||
default: | ||
{ | ||
const module = await import("better-sqlite3"); | ||
|
||
const Database = module.default; | ||
|
||
// SQLite adapter | ||
adapter = new SqliteDatabaseAdapter(new Database(":memory:")); | ||
|
||
// Load sqlite-vss | ||
await loadVecExtensions((adapter as SqliteDatabaseAdapter).db); | ||
// Create a test user and session | ||
user = { | ||
id: zeroUuid, | ||
email: "test@example.com", | ||
} as User; | ||
session = { | ||
user: user, | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
} | ||
break; | ||
} | ||
|
||
const runtime = new AgentRuntime({ | ||
serverUrl: "https://api.openai.com/v1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably a const we can use here |
||
conversationLength, | ||
token: env!.OPENAI_API_KEY!, | ||
modelProvider: ModelProviderName.OPENAI, | ||
actions: actions ?? [], | ||
evaluators: evaluators ?? [], | ||
providers: providers ?? [], | ||
databaseAdapter: adapter, | ||
}); | ||
|
||
return { user, session, runtime }; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import dotenv from "dotenv"; | ||
import path from "path"; | ||
|
||
// Load test environment variables | ||
const envPath = path.resolve(__dirname, "../../.env.test"); | ||
console.log('Current directory:', __dirname); | ||
console.log('Trying to load env from:', envPath); | ||
const result = dotenv.config({ path: envPath }); | ||
if (result.error) { | ||
console.error('Error loading .env.test:', result.error); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface User { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am surprised that this type hasn't already been declared and can't be reused. Could be mistaken but wanted to point it out thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type is intended to generically represent the different User types used across packages in the agent runtime based on whether we're using sqlite, supabase or sqljs. I couldn't find an abstract type that currently exists for this, but I'd be happy to replace it here if there is one. If there isn't one then I agree this would be helpful to have a real type that does this instead of this one sitting in a test file :) |
||
id: string; | ||
email?: string; | ||
phone?: string; | ||
role?: string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
describe('Environment Setup', () => { | ||
it('should verify .env.test file exists', () => { | ||
const possiblePaths = [ | ||
path.join(process.cwd(), '.env.test'), | ||
path.join(process.cwd(), 'packages/core/.env.test'), | ||
path.join(__dirname, '../../.env.test'), | ||
path.join(__dirname, '../.env.test'), | ||
path.join(__dirname, '.env.test'), | ||
]; | ||
|
||
console.log('Current working directory:', process.cwd()); | ||
console.log('__dirname:', __dirname); | ||
|
||
const existingPaths = possiblePaths.filter(p => { | ||
const exists = fs.existsSync(p); | ||
console.log(`Path ${p} exists: ${exists}`); | ||
return exists; | ||
}); | ||
|
||
expect(existingPaths.length).toBeGreaterThan(0); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this