Skip to content

Commit

Permalink
Add basic tests for config init
Browse files Browse the repository at this point in the history
  • Loading branch information
justyns committed Jul 13, 2024
1 parent e7f82a8 commit b51c41a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ silverbullet-ai.plug.js
docs/_public
!docs/_plug
SECRETS.md
cov_profile
test?space*
12 changes: 7 additions & 5 deletions sbai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@ import { editor, markdown, space, system } from "$sb/syscalls.ts";
import { query } from "$sbplugs/query/api.ts";
import { decodeBase64 } from "https://deno.land/[email protected]/encoding/base64.ts";
import { getPageLength, getSelectedTextOrNote } from "./src/editorUtils.ts";
import type {
ChatMessage,
EmbeddingModelConfig,
ImageGenerationOptions,
ImageModelConfig,
ModelConfig,
} from "./src/types.ts";
import {
aiSettings,
ChatMessage,
chatSystemPrompt,
configureSelectedEmbeddingModel,
configureSelectedImageModel,
configureSelectedModel,
currentAIProvider,
currentEmbeddingProvider,
currentImageProvider,
EmbeddingModelConfig,
ImageModelConfig,
initializeOpenAI,
initIfNeeded,
ModelConfig,
setSelectedEmbeddingModel,
setSelectedImageModel,
setSelectedTextModel,
} from "./src/init.ts";
import { ImageGenerationOptions } from "./src/interfaces.ts";
import {
convertPageToMessages,
enrichChatMessages,
Expand Down
73 changes: 73 additions & 0 deletions src/init.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
import "./mocks/syscalls.ts";
import { aiSettings, getAndConfigureModel, initializeOpenAI } from "./init.ts";

const settingsPageSample = `
Mock settings, yay
\`\`\`yaml
ai:
indexEmbeddings: true
indexEmbeddingsExcludePages:
- passwords
indexEmbeddingsExcludeStrings:
- foo
chat:
bakeMessages: false
customEnrichFunctions:
- enrichWithURL
textModels:
- name: gpt-4o
provider: openai
modelName: gpt-4o
- name: gemini-pro
modelName: gemini-pro
provider: gemini
baseUrl: https://api.gemini.ai/v1
secretName: GOOGLE_AI_STUDIO_KEY
embeddingModels:
- name: text-embedding-3-small
provider: openai
modelName: text-embedding-3-small
- name: ollama-all-minilm
modelName: all-minilm
provider: ollama
baseUrl: http://localhost:11434
requireAuth: false
\`\`\`
`;

const secretsPageSample = `
Mock secrets, yay
\`\`\`yaml
GOOGLE_AI_STUDIO_KEY: foo
OPENAI_API_KEY: bar
\`\`\`
`;

Deno.test("initializeOpenAI should set aiSettings correctly", async () => {
await syscall("mock.setPage", "SETTINGS", settingsPageSample);
await syscall("mock.setPage", "SECRETS", secretsPageSample);
await initializeOpenAI();
assertEquals(
aiSettings.textModels.length,
2,
"initializeOpenAI did not set the correct number of text models",
);
assertEquals(
aiSettings.textModels[0].name,
"gpt-4o",
"initializeOpenAI did not set the correct text model name",
);
});

Deno.test("initializeOpenAI should configure the selected model", async () => {
await syscall("mock.setPage", "SETTINGS", settingsPageSample);
await syscall("mock.setPage", "SECRETS", secretsPageSample);
await initializeOpenAI();
await getAndConfigureModel();
assertEquals(
aiSettings.textModels[0].name,
"gpt-4o",
"getAndConfigureModel did not configure the correct text model",
);
});
31 changes: 30 additions & 1 deletion src/mocks/syscalls.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
import { parse } from "$common/markdown_parser/parse_tree.ts";
import { extendedMarkdownLanguage } from "$common/markdown_parser/parser.ts";
import { YAML } from "$lib/deps_server.ts";

let editorText = "Mock data";
(globalThis as any).editorText;

let pages: { [key: string]: string } = {};
(globalThis as any).pages;

let currentEnv: string = "server";
(globalThis as any).currentEnv;

globalThis.syscall = async (name: string, ...args: readonly any[]) => {
switch (name) {
// I tried a lot of things to get this working differently, but
// ended up with just keeping this in a variable that can be changed
// in the tests.
case "mock.setText":
editorText = args[0];
return await Promise.resolve(editorText);
break;
case "editor.getText":
return await Promise.resolve(editorText);

case "mock.setPage":
pages[args[0]] = args[1];
break;
case "space.readPage":
// console.log("space.readPage", args);
return await Promise.resolve(pages[args[0]]);

case "mock.setEnv":
currentEnv = args[0];
break;
case "system.getEnv":
return await Promise.resolve(currentEnv);

// Pass through to the real functions
case "markdown.parseMarkdown":
return await Promise.resolve(parse(extendedMarkdownLanguage, args[0]));
case "yaml.parse":
return await Promise.resolve(YAML.parse(args[0]));
default:
throw Error(`Missing mock for: ${name}`);
}
Expand Down

0 comments on commit b51c41a

Please sign in to comment.