|
| 1 | +type BraintrustModule = Record<string, unknown>; |
| 2 | + |
| 3 | +function assert( |
| 4 | + condition: unknown, |
| 5 | + message: string, |
| 6 | +): asserts condition is true { |
| 7 | + if (!condition) { |
| 8 | + throw new Error(message); |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 13 | + return typeof value === "object" && value !== null && !Array.isArray(value); |
| 14 | +} |
| 15 | + |
| 16 | +export function getTestRunId(): string { |
| 17 | + const testRunId = Deno.env.get("BRAINTRUST_E2E_RUN_ID"); |
| 18 | + assert(testRunId, "BRAINTRUST_E2E_RUN_ID must be set"); |
| 19 | + return testRunId; |
| 20 | +} |
| 21 | + |
| 22 | +export function scopedName(base: string): string { |
| 23 | + return `${base}-${getTestRunId() |
| 24 | + .toLowerCase() |
| 25 | + .replace(/[^a-z0-9-]/g, "-")}`; |
| 26 | +} |
| 27 | + |
| 28 | +export function expectNamedExports( |
| 29 | + module: BraintrustModule, |
| 30 | + exportNames: string[], |
| 31 | +): void { |
| 32 | + for (const exportName of exportNames) { |
| 33 | + assert(module[exportName], `Expected export "${exportName}" to exist`); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +export function expectBuildType( |
| 38 | + module: BraintrustModule, |
| 39 | + expectedBuildType: string, |
| 40 | +): void { |
| 41 | + const testingOnly = module._exportsForTestingOnly; |
| 42 | + assert(isRecord(testingOnly), "_exportsForTestingOnly must exist"); |
| 43 | + |
| 44 | + const isomorph = testingOnly.isomorph; |
| 45 | + assert(isRecord(isomorph), "_exportsForTestingOnly.isomorph must exist"); |
| 46 | + assert( |
| 47 | + isomorph.buildType === expectedBuildType, |
| 48 | + `Expected build type "${expectedBuildType}" but got "${String(isomorph.buildType)}"`, |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +export function expectMustacheTemplate(module: BraintrustModule): void { |
| 53 | + const Prompt = module.Prompt as |
| 54 | + | (new (...args: unknown[]) => { |
| 55 | + build: ( |
| 56 | + args: Record<string, unknown>, |
| 57 | + options: { templateFormat: string }, |
| 58 | + ) => { messages?: Array<{ content?: string }> }; |
| 59 | + }) |
| 60 | + | undefined; |
| 61 | + |
| 62 | + assert(Prompt, "Prompt export must exist"); |
| 63 | + |
| 64 | + const prompt = new Prompt( |
| 65 | + { |
| 66 | + name: "mustache-test", |
| 67 | + slug: "mustache-test", |
| 68 | + prompt_data: { |
| 69 | + prompt: { |
| 70 | + type: "chat", |
| 71 | + messages: [{ role: "user", content: "Hello, {{name}}!" }], |
| 72 | + }, |
| 73 | + options: { model: "gpt-4" }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + {}, |
| 77 | + false, |
| 78 | + ); |
| 79 | + |
| 80 | + const result = prompt.build( |
| 81 | + { name: "World" }, |
| 82 | + { templateFormat: "mustache" }, |
| 83 | + ); |
| 84 | + assert( |
| 85 | + result.messages?.[0]?.content === "Hello, World!", |
| 86 | + "Mustache template rendering failed", |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +export function expectNunjucksTemplateUnavailable( |
| 91 | + module: BraintrustModule, |
| 92 | +): void { |
| 93 | + const Prompt = module.Prompt as |
| 94 | + | (new (...args: unknown[]) => { |
| 95 | + build: ( |
| 96 | + args: Record<string, unknown>, |
| 97 | + options: { templateFormat: string }, |
| 98 | + ) => unknown; |
| 99 | + }) |
| 100 | + | undefined; |
| 101 | + |
| 102 | + assert(Prompt, "Prompt export must exist"); |
| 103 | + |
| 104 | + const prompt = new Prompt( |
| 105 | + { |
| 106 | + name: "nunjucks-test", |
| 107 | + slug: "nunjucks-test", |
| 108 | + prompt_data: { |
| 109 | + prompt: { |
| 110 | + type: "chat", |
| 111 | + messages: [ |
| 112 | + { |
| 113 | + role: "user", |
| 114 | + content: |
| 115 | + "Items: {% for item in items %}{{ item.name }}{% if not loop.last %}, {% endif %}{% endfor %}", |
| 116 | + }, |
| 117 | + ], |
| 118 | + }, |
| 119 | + options: { model: "gpt-4" }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + {}, |
| 123 | + false, |
| 124 | + ); |
| 125 | + |
| 126 | + let errorMessage: string | undefined; |
| 127 | + try { |
| 128 | + prompt.build( |
| 129 | + { |
| 130 | + items: [{ name: "apple" }, { name: "banana" }, { name: "cherry" }], |
| 131 | + }, |
| 132 | + { templateFormat: "nunjucks" }, |
| 133 | + ); |
| 134 | + } catch (error) { |
| 135 | + errorMessage = error instanceof Error ? error.message : String(error); |
| 136 | + } |
| 137 | + |
| 138 | + assert( |
| 139 | + errorMessage?.includes("requires @braintrust/template-nunjucks"), |
| 140 | + `Expected missing nunjucks package error, got: ${errorMessage ?? "no error"}`, |
| 141 | + ); |
| 142 | +} |
| 143 | + |
| 144 | +export async function expectEvalWorks(module: BraintrustModule): Promise<void> { |
| 145 | + const Eval = module.Eval as |
| 146 | + | (( |
| 147 | + name: string, |
| 148 | + definition: Record<string, unknown>, |
| 149 | + options: Record<string, unknown>, |
| 150 | + ) => Promise<Record<string, unknown>>) |
| 151 | + | undefined; |
| 152 | + |
| 153 | + assert(Eval, "Eval export must exist"); |
| 154 | + |
| 155 | + const evalData = [ |
| 156 | + { input: "Alice", expected: "Hi Alice" }, |
| 157 | + { input: "Bob", expected: "Hi Bob" }, |
| 158 | + { input: "Charlie", expected: "Hi Charlie" }, |
| 159 | + ]; |
| 160 | + |
| 161 | + const result = await Eval( |
| 162 | + "deno-local-eval", |
| 163 | + { |
| 164 | + data: evalData, |
| 165 | + task: async (input: string) => `Hi ${input}`, |
| 166 | + scores: [ |
| 167 | + ({ expected, output }: { expected: string; output: string }) => ({ |
| 168 | + name: "exact_match", |
| 169 | + score: output === expected ? 1 : 0, |
| 170 | + }), |
| 171 | + ], |
| 172 | + }, |
| 173 | + { |
| 174 | + noSendLogs: true, |
| 175 | + returnResults: true, |
| 176 | + }, |
| 177 | + ); |
| 178 | + |
| 179 | + const summary = result.summary; |
| 180 | + const results = result.results; |
| 181 | + assert(Array.isArray(results), "Eval results must be an array"); |
| 182 | + assert( |
| 183 | + results.length === evalData.length, |
| 184 | + "Eval returned the wrong row count", |
| 185 | + ); |
| 186 | + assert(isRecord(summary), "Eval summary must exist"); |
| 187 | + assert(isRecord(summary.scores), "Eval summary scores must exist"); |
| 188 | + |
| 189 | + const exactMatch = summary.scores.exact_match; |
| 190 | + assert(isRecord(exactMatch), "Eval exact_match summary must exist"); |
| 191 | + assert(exactMatch.score === 1, "Eval exact_match summary must be 1"); |
| 192 | +} |
0 commit comments