Skip to content

Commit

Permalink
test running
Browse files Browse the repository at this point in the history
  • Loading branch information
normand1 committed Nov 16, 2024
1 parent d778e60 commit c93510c
Show file tree
Hide file tree
Showing 32 changed files with 1,862 additions and 117 deletions.
2 changes: 2 additions & 0 deletions packages/core/.env.test
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
TEST_DATABASE_CLIENT=sqlite
NODE_ENV=test
MAIN_WALLET_ADDRESS=testwalletaddressfM3U31goWWrCpF59CHWNhnCJ9Vyh
OPENAI_API_KEY=OPENAI_TEST_KEY
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":[],"expiry":1731784276120}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"ownerBalance":"100","creatorBalance":"50","ownerPercentage":10,"creatorPercentage":5,"top10HolderBalance":"200","top10HolderPercent":20},"expiry":1731784617753}
29 changes: 0 additions & 29 deletions packages/core/jest.config.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"watch": "tsc --watch",
"dev": "tsup --format esm --dts --watch",
"build:docs": "cd docs && pnpm run build",
"test": "vitest run",
"test:watch": "vitest"
"test": "NODE_ENV=test vitest run",
"test:watch": "NODE_ENV=test vitest"
},
"author": "",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/embedding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { models } from "./models.ts";
import { IAgentRuntime, ModelProviderName, ModelClass } from "./types.ts";
import fs from "fs";
import { trimTokens } from "./generation.ts";
import settings from "./settings.ts";
import { settings } from "./settings.ts";

function getRootPath() {
const __filename = fileURLToPath(import.meta.url);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
parseJSONObjectFromText,
parseShouldRespondFromText,
} from "./parsing.ts";
import settings from "./settings.ts";
import { settings } from "./settings.ts";
import {
Content,
IAgentRuntime,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import settings from "./settings.ts";
import { settings } from "./settings.ts";
import { Models, ModelProviderName, ModelClass } from "./types.ts";

export const models: Models = {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { formatActors, formatMessages, getActorDetails } from "./messages.ts";
import { parseJsonArrayFromText } from "./parsing.ts";
import { formatPosts } from "./posts.ts";
import { getProviders } from "./providers.ts";
import settings from "./settings.ts";
import { settings } from "./settings.ts";
import {
Character,
Goal,
Expand Down
76 changes: 58 additions & 18 deletions packages/core/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,94 @@ import { config } from "dotenv";
import fs from "fs";
import path from "path";

export interface Settings {
MAIN_WALLET_ADDRESS: string;
OPENAI_API_KEY: string;
USE_OPENAI_EMBEDDING: string;
SYSTEM_PROMPT: string;
OPENROUTER_MODEL: string;
SMALL_OPENROUTER_MODEL: string;
MEDIUM_OPENROUTER_MODEL: string;
LARGE_OPENROUTER_MODEL: string;
OLLAMA_MODEL: string;
LARGE_OLLAMA_MODEL: string;
MEDIUM_OLLAMA_MODEL: string;
SMALL_OLLAMA_MODEL: string;
OLLAMA_SERVER_URL: string;
OLLAMA_EMBEDDING_MODEL: string;
RPC_URL: string;
BASE_MINT: string;
BACKEND_URL: string;
BACKEND_TOKEN: string;
BIRDEYE_API_KEY: string;
HELIUS_API_KEY: string;
SERVER_PORT: string;
CAPSOLVER_API_KEY: string;
CUDA_PATH: string;
}

/**
* Recursively searches for a .env file starting from the current directory
* and moving up through parent directories
* @param {string} [startDir=process.cwd()] - Starting directory for the search
* @returns {string|null} Path to the nearest .env file or null if not found
*/
export function findNearestEnvFile(startDir = process.cwd()) {
console.error('DEBUG - Starting env file search');
console.error('DEBUG - Current working directory:', process.cwd());

// In test environment, use the known working path
if (process.env.NODE_ENV === 'test' || process.env.VITEST) {
const testPath = path.join(process.cwd(), '.env.test');
console.error('DEBUG - Checking test path:', testPath);
if (fs.existsSync(testPath)) {
console.error('DEBUG - Found test env file at:', testPath);
return testPath;
}
}

// Look for regular .env
let currentDir = startDir;
const envFile = process.env.NODE_ENV === 'test' ? '.env.test' : '.env';

// Continue searching until we reach the root directory
while (currentDir !== path.parse(currentDir).root) {
const envPath = path.join(currentDir, ".env");

const envPath = path.join(currentDir, envFile);
console.error('DEBUG - Checking path:', envPath);
if (fs.existsSync(envPath)) {
console.error('DEBUG - Found env file at:', envPath);
return envPath;
}

// Move up to parent directory
currentDir = path.dirname(currentDir);
}

// Check root directory as well
const rootEnvPath = path.join(path.parse(currentDir).root, ".env");
return fs.existsSync(rootEnvPath) ? rootEnvPath : null;
console.error('DEBUG - No env file found after checking all paths');
console.error('DEBUG - Final cwd:', process.cwd());
console.error('DEBUG - Final NODE_ENV:', process.env.NODE_ENV);
return null;
}

/**
* Loads environment variables from the nearest .env file
* @returns {Object} Environment variables object
* @throws {Error} If no .env file is found
*/
export function loadEnvConfig() {
console.error('DEBUG - loadEnvConfig called');
console.error('DEBUG - Current working directory:', process.cwd());
console.error('DEBUG - NODE_ENV:', process.env.NODE_ENV);

const envPath = findNearestEnvFile();

if (!envPath) {
throw new Error("No .env file found in current or parent directories.");
}

// Load the .env file
console.error('DEBUG - Loading env file from:', envPath);
const result = config({ path: envPath });

if (result.error) {
throw new Error(`Error loading .env file: ${result.error}`);
}

console.log(`Loaded .env file from: ${envPath}`);
return process.env;
console.error('DEBUG - Successfully loaded env file');

// Populate the settings object with the environment variables
Object.assign(settings, process.env);
}

export const settings = loadEnvConfig();
export default settings;
export const settings: Settings = {} as Settings;
12 changes: 12 additions & 0 deletions packages/core/src/test_resources/basic.test.ts
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");
});
});
36 changes: 36 additions & 0 deletions packages/core/src/test_resources/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// getCachedEmbeddings
// check cache.json for embedding where the key is a stringified version of the memory and the value is a number array
import fs from "fs";
export const getCachedEmbeddings = async (text: string) => {
if (!fs.existsSync("./embedding-cache.json")) {
fs.writeFileSync("./embedding-cache.json", "{}");
}
// read cache.json
const cache = JSON.parse(
fs.readFileSync("./embedding-cache.json", "utf8") as string
);
// stringify the memory
const key = JSON.stringify(text);
// return the value of the memory
return cache[key];
};

export const writeCachedEmbedding = async (
text: string,
embedding: number[]
) => {
// check if ./embedding-cache.json exists, if it doesn't, write {} to it
if (!fs.existsSync("./embedding-cache.json")) {
fs.writeFileSync("./embedding-cache.json", "{}");
}
// read cache.json
const cache = JSON.parse(
fs.readFileSync("./embedding-cache.json", "utf8") as string
);
// stringify the memory
const key = JSON.stringify(text);
// write the value of the memory
cache[key] = embedding;
// write the cache to cache.json
fs.writeFileSync("./embedding-cache.json", JSON.stringify(cache));
};
12 changes: 12 additions & 0 deletions packages/core/src/test_resources/constants.ts
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 = "[email protected]";
export const TEST_PASSWORD = "[email protected]";
export const TEST_EMAIL_2 = "[email protected]";
export const TEST_PASSWORD_2 = "[email protected]";

export const zeroUuid = "00000000-0000-0000-0000-000000000000" as UUID;
Loading

0 comments on commit c93510c

Please sign in to comment.