Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created June 20, 2025 11:23
Show Gist options
  • Save mizchi/e63ac6722d7e14e95f117e03243116f5 to your computer and use it in GitHub Desktop.
Save mizchi/e63ac6722d7e14e95f117e03243116f5 to your computer and use it in GitHub Desktop.
// node --test ask-claude.test.ts
import { test } from "node:test";
import { query } from "@anthropic-ai/claude-code";
test("Ask", async () => {
const EXPECTED = "paris";
const abortController = new AbortController();
for await (const message of query({
prompt: "What is the capital of France?",
options: { maxTurns: 1, abortController },
})) {
if (message.type === "assistant") {
const c = message.message.content;
const res: string = Array.isArray(c) ? c.map((c) => c.text).join("") : c;
if (res.toLowerCase().includes(EXPECTED)) {
abortController.abort();
return;
}
}
}
throw new Error("No response");
});
test("Read with tools", async () => {
const EXPECTED = "APE";
const abortController = new AbortController();
for await (const message of query({
prompt: "Read README.md and tell me about it",
options: { maxTurns: 7, allowedTools: ["Read(*)"], cwd: process.cwd() },
})) {
if (message.type === "result" && message.subtype === "success") {
if (message.result.includes(EXPECTED)) {
abortController.abort();
return;
}
}
}
throw new Error("No response");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment