KScript docs
EN

Testing

Faking a language model

Code that asks Claude through @llm, llm or $chat->ask() can be tested without a key or the network. @fake_llm(replies) answers every question for the rest of the run, and keeps what was asked. Give it a list of answers, used in order, or a function that is given each prompt. A map answers a question asked with a schema:

<import package="KScript.AI" />

<fn name="triage" params="report: string" value="@llm(&quot;Triage this report: { $report }&quot;, schema: { category: 'bug|feature|question' })" />

<test name="a crash report is a bug">
  <let name="fake" value="@fake_llm([{ category: 'bug' }])" />
  <expect value="@triage('The app crashes on start')" has="{ category: 'bug' }" />
  <expect value="$fake->prompts()" equals="['Triage this report: The app crashes on start']" />
</test>

<test name="the reply can depend on the prompt">
  <do>@fake_llm(fn(prompt) => $prompt->contains('French') ? 'Bonjour' : 'Hello')</do>
  <expect value="@llm('Say hello in French')" equals="'Bonjour'" />
</test>
import KScript.AI

fn triage(report: string) => @llm("Triage this report: { $report }", schema: { category: 'bug|feature|question' })

test(name: 'a crash report is a bug') {
  $fake = @fake_llm([{ category: 'bug' }])
  expect(@triage('The app crashes on start'), has: { category: 'bug' })
  expect($fake->prompts(), equals: ['Triage this report: The app crashes on start'])
}

test(name: 'the reply can depend on the prompt') {
  @fake_llm(fn(prompt) => $prompt->contains('French') ? 'Bonjour' : 'Hello')
  expect(@llm('Say hello in French'), equals: 'Bonjour')
}

$fake->count() says how many times the model was asked. When a list runs out, the next question fails with the prompt that had no answer.