Public Beta
Not set

Experience

Changes how pages explain things, never what you can do.

More

Experience

Changes how pages explain things, never what you can do.

Sign inGet started

Study Assistant v0.2.1 · generic

Archive SHA-256 536deca3b50ebaa926488a4a9df7410b559c62f42190fb06d23b1e5b1cdfae14 · 27.4 KB · built Nov 27, 2026

tests/study-assistant.test.ts

import { describe, it, expect } from "vitest";
// @ts-expect-error -- plain JavaScript agent module without type declarations
import agent, { handleMcpMessage, manifest, validateManifest } from "../src/index.js";

const NOW = "2026-11-25T12:00:00.000Z";

const NOTES = [
  "# Biology",
  "",
  "## Cells",
  "",
  "The cell is the basic unit of life. All living things are made of cells.",
  "",
  "Q: What organelle produces ATP?",
  "A: The mitochondrion",
  "",
  "**Q:** What holds genetic material?",
  "**A:** The nucleus",
  "",
  "- Ribosome — makes proteins",
  "- Cell membrane – controls what enters and leaves the cell",
  "- **Golgi apparatus** -- packages proteins",
  "Lysosome :: breaks down waste",
  "",
  "The result — surprisingly — was good.",
  "",
  "## Genetics",
  "",
  "DNA stores hereditary information. It is a double helix.",
  "",
  "Q: Unanswered question?",
  "",
  "Some prose paragraph after.",
  "",
  "A: orphan answer",
  "",
  "```",
  "Q: inside code",
  "A: ignored",
  "```",
  "",
  "Q: What holds genetic material?",
  "A: The nucleus",
  "",
].join("\n");

type Card = { id: string; front: string; back: string; tags: string[]; kind: string; due: string; interval_days: number; ease: number; repetitions: number; lapses: number };
type Question = { n: number; card_id: string; prompt: string; choices: string[]; answer_index: number; answer: string };

async function cards(): Promise<Card[]> {
  return (await agent.call("make_flashcards", { text: NOTES, tags: ["bio"], now: NOW })).cards;
}

describe("study-assistant manifest", () => {
  it("passes validateManifest with no permissions", () => {
    expect(validateManifest(manifest)).toEqual([]);
    expect(manifest.id).toBe("agentrove.study-assistant");
    expect(manifest.permissions).toEqual({ filesystem: "none", network: [], credentials: [] });
  });
});

describe("make_flashcards", () => {
  it("extracts Q/A pairs, term lines and heading cards, skipping code, orphans and duplicates", async () => {
    const out = await agent.call("make_flashcards", { text: NOTES, tags: ["bio"], now: NOW });
    expect(out.counts).toEqual({ qa: 2, term: 4, heading: 2, total: 8 });
    expect(out.cards.map((c: Card) => [c.kind, c.front, c.back])).toEqual([
      ["heading", "Cells", "The cell is the basic unit of life."],
      ["qa", "What organelle produces ATP?", "The mitochondrion"],
      ["qa", "What holds genetic material?", "The nucleus"],
      ["term", "Ribosome", "makes proteins"],
      ["term", "Cell membrane", "controls what enters and leaves the cell"],
      ["term", "Golgi apparatus", "packages proteins"],
      ["term", "Lysosome", "breaks down waste"],
      ["heading", "Genetics", "DNA stores hereditary information."],
    ]);
    expect(out.cards[1]).toMatchObject({ tags: ["bio", "cells"], due: NOW, interval_days: 0, ease: 2.5, repetitions: 0, lapses: 0, source_line: 7 });
    expect(out.cards[1].id).toMatch(/^c-[0-9a-f]{12}$/);
    expect(out.cards[7].tags).toEqual(["bio", "genetics"]);
    expect(out.skipped).toEqual([
      { line: 24, reason: "Question without an answer (expected an 'A:' line)" },
      { line: 28, reason: "Answer without a preceding 'Q:' line" },
      { line: 35, reason: `Duplicate of card ${out.cards[2].id}` },
    ]);
    // Same input, same ids (stable, content-derived).
    expect((await cards()).map((c) => c.id)).toEqual(out.cards.map((c: Card) => c.id));
  });

  it("supports multi-line answers, deck names and disabling heading cards", async () => {
    const text = "# Topic\n\nIntro sentence here. More.\n\nQ: List two primes\nA: 2\n3\n";
    const out = await agent.call("make_flashcards", { text, deck: "Math", include_headings: false, now: NOW });
    expect(out.cards).toHaveLength(1);
    expect(out.cards[0]).toMatchObject({ front: "List two primes", back: "2\n3", deck: "Math", tags: ["topic"] });
  });

  it("reads bold-colon and list-colon definitions, but not labels such as Note: (0.2.0)", async () => {
    const text = [
      "## Membranes",
      "",
      "- **Diffusion**: net movement of particles from high to low concentration.",
      "- **Osmosis:** diffusion of water across a selectively permeable membrane.",
      "__Active transport__: movement against a gradient using ATP.",
      "- Endocytosis: the cell engulfs material in a vesicle.",
      "- Note: chapter 4 has the diagrams.",
      "- Lecture at 10:30",
      "- This list item has far too many words before its colon: so it is not a term",
      "Remember: plain colon lines outside lists stay prose.",
    ].join("\n");
    const out = await agent.call("make_flashcards", { text, now: NOW });
    expect(out.cards.map((c: { front: string; back: string }) => [c.front, c.back])).toEqual([
      ["Diffusion", "net movement of particles from high to low concentration."],
      ["Osmosis", "diffusion of water across a selectively permeable membrane."],
      ["Active transport", "movement against a gradient using ATP."],
      ["Endocytosis", "the cell engulfs material in a vesicle."],
    ]);
    expect(out.counts).toMatchObject({ term: 4, heading: 0 });
  });

  it("pairs a heading only with the paragraph directly under it (0.2.0)", async () => {
    const text = [
      "## Organelles",
      "",
      "Mitochondria — site of aerobic respiration.",
      "",
      "Note: the textbook calls the Golgi the Golgi body.",
      "",
      "## Cell division",
      "",
      "Q: How many chromosomes are in a human somatic cell?",
      "A: 46.",
      "",
      "Mitosis is covered again in unit 5.",
      "",
      "## Respiration",
      "",
      "Aerobic respiration releases energy from glucose using oxygen. It happens in mitochondria.",
    ].join("\n");
    const out = await agent.call("make_flashcards", { text, now: NOW });
    const headingCards = out.cards.filter((c: { kind: string }) => c.kind === "heading");
    expect(headingCards.map((c: { front: string; back: string }) => [c.front, c.back])).toEqual([["Respiration", "Aerobic respiration releases energy from glucose using oxygen."]]);
    expect(out.counts).toMatchObject({ qa: 1, term: 1, heading: 1, total: 3 });
  });

  it("returns no cards for empty or card-free notes and rejects bad input", async () => {
    expect(await agent.call("make_flashcards", { text: "", now: NOW })).toEqual({ cards: [], skipped: [], counts: { qa: 0, term: 0, heading: 0, total: 0 } });
    await expect(agent.call("make_flashcards", {})).rejects.toMatchObject({ code: "invalid_input" });
    await expect(agent.call("make_flashcards", { text: "x", tags: ["has space"] })).rejects.toMatchObject({ code: "invalid_input" });
  });
});

describe("review (SM-2)", () => {
  const review = (card: Record<string, unknown>, quality: number, now = NOW) => agent.call("review", { card, quality, now });

  it("follows the SM-2 interval sequence 1, 6, ceil(I*EF) and updates ease", async () => {
    let card: Record<string, unknown> = { id: "c1", front: "f", back: "b", tags: ["t"], ease: 2.5, repetitions: 0, interval_days: 0, lapses: 0 };
    const steps: Array<[number, number, number, number]> = [];
    let now = Date.parse(NOW);
    for (const q of [5, 4, 3, 5]) {
      const r = await review(card, q, new Date(now).toISOString());
      card = r.card;
      steps.push([r.card.repetitions, r.card.interval_days, r.card.ease, q]);
      now = Date.parse(r.card.due);
    }
    expect(steps).toEqual([
      [1, 1, 2.6, 5],
      [2, 6, 2.6, 4],
      [3, 16, 2.46, 3],
      [4, 40, 2.56, 5],
    ]);
    expect(card).toMatchObject({ id: "c1", front: "f", back: "b", tags: ["t"], lapses: 0 });
  });

  it("handles lapses: repetitions reset, interval 1 day, ease reduced, lapse counted", async () => {
    const r = await review({ id: "c2", ease: 2.56, repetitions: 4, interval_days: 40, lapses: 1 }, 2);
    expect(r.lapse).toBe(true);
    expect(r.repeat_today).toBe(true);
    expect(r.card).toMatchObject({ repetitions: 0, interval_days: 1, ease: 2.24, lapses: 2, due: "2026-11-26T12:00:00.000Z", last_reviewed: NOW });
    expect(r.previous).toEqual({ interval_days: 40, ease: 2.56, repetitions: 4 });
    const blackout = await review({ id: "c3" }, 0);
    expect(blackout.card.ease).toBe(1.7);
    const floor = await review({ id: "c4", ease: 1.3, repetitions: 3, interval_days: 10 }, 0);
    expect(floor.card.ease).toBe(1.3);
    const afterLapse = await review(r.card, 4, r.card.due);
    expect(afterLapse.card).toMatchObject({ repetitions: 1, interval_days: 1 });
  });

  it("adds whole 24-hour days in UTC (no DST shifts) and defaults now from context", async () => {
    const r = await review({ id: "d", repetitions: 1, interval_days: 1 }, 5, "2026-03-07T12:00:00Z");
    expect(r.card.due).toBe("2026-03-13T12:00:00.000Z");
    const ctxNow = await agent.call("review", { card: { id: "e" }, quality: 4 }, { now: () => new Date("2026-10-24T23:30:00Z") });
    expect(ctxNow.card.due).toBe("2026-10-25T23:30:00.000Z");
    expect(ctxNow.repeat_today).toBe(false);
  });

  it("rejects invalid quality and card state", async () => {
    for (const quality of [6, -1, 2.5]) await expect(review({ id: "x" }, quality)).rejects.toMatchObject({ code: "invalid_input" });
    await expect(review({ id: "x", ease: 1.2 }, 3)).rejects.toMatchObject({ code: "invalid_input" });
    await expect(review({ id: "x", repetitions: -1 }, 3)).rejects.toMatchObject({ code: "invalid_input" });
    await expect(agent.call("review", { quality: 3 })).rejects.toMatchObject({ code: "invalid_input" });
  });
});

describe("quiz", () => {
  it("builds deterministic multiple-choice questions from other cards' answers", async () => {
    const deck = await cards();
    const backs = new Set(deck.map((c) => c.back));
    const q1 = await agent.call("quiz", { cards: deck, count: 4, choices: 4, seed: 42 });
    expect(q1.questions).toHaveLength(4);
    for (const q of q1.questions as Question[]) {
      const card = deck.find((c) => c.id === q.card_id)!;
      expect(q.prompt).toBe(card.front);
      expect(q.choices).toHaveLength(4);
      expect(new Set(q.choices).size).toBe(4);
      expect(q.choices[q.answer_index]).toBe(card.back);
      for (const c of q.choices) expect(backs.has(c)).toBe(true);
    }
    expect(new Set(q1.questions.map((q: Question) => q.card_id)).size).toBe(4);
    expect(await agent.call("quiz", { cards: deck, count: 4, choices: 4, seed: 42 })).toEqual(q1);
    const q2 = await agent.call("quiz", { cards: deck, count: 4, choices: 4, seed: "another seed" });
    expect(q2.questions).not.toEqual(q1.questions);
  });

  it("prefers same-tag distractors", async () => {
    const mk = (id: string, tag: string) => ({ id, front: `F${id}`, back: `B${id}`, tags: [tag] });
    const deck = [mk("1", "a"), mk("2", "a"), mk("3", "a"), mk("4", "b"), mk("5", "b"), mk("6", "b")];
    const out = await agent.call("quiz", { cards: deck, count: 6, choices: 3, seed: 7 });
    for (const q of out.questions as Question[]) {
      const tag = deck.find((c) => c.id === q.card_id)!.tags[0];
      const sameTagBacks = deck.filter((c) => c.tags[0] === tag).map((c) => c.back);
      expect(q.choices.every((c) => sameTagBacks.includes(c))).toBe(true);
    }
  });

  it("ignores deck-wide tags and prefers distractors of the same card kind (0.2.0)", async () => {
    const mk = (id: string, kind: string, tags: string[]) => ({ id, front: `F${id}`, back: `B${id}`, kind, tags: ["course", ...tags] });
    const deck = [mk("1", "term", ["cells"]), mk("2", "term", ["organelles"]), mk("3", "term", ["organelles"]), mk("4", "qa", ["cells"]), mk("5", "qa", ["division"]), mk("6", "qa", ["division"])];
    const out = await agent.call("quiz", { cards: deck, count: 6, choices: 3, seed: 3 });
    expect(out.questions).toHaveLength(6);
    for (const q of out.questions as Question[]) {
      const card = deck.find((c) => c.id === q.card_id)!;
      const sameKind = deck.filter((c) => c.kind === card.kind).map((c) => c.back);
      expect(q.choices.every((c) => sameKind.includes(c)), q.card_id).toBe(true);
    }
  });

  it("skips cards without distinct distractors, filters due cards and validates input", async () => {
    const same = await agent.call("quiz", { cards: [{ id: "a", front: "x", back: "Same" }, { id: "b", front: "y", back: "same " }] });
    expect(same.questions).toEqual([]);
    expect(same.notes.join(" ")).toMatch(/no other card has a different answer/);
    const few = await agent.call("quiz", { cards: [{ id: "a", front: "x", back: "1" }, { id: "b", front: "y", back: "2" }], choices: 4 });
    expect(few.questions[0].choices).toHaveLength(2);
    expect(few.notes.join(" ")).toMatch(/only 2 choices/);
    const due = await agent.call("quiz", {
      cards: [
        { id: "a", front: "x", back: "1", due: "2026-11-20T00:00:00Z" },
        { id: "b", front: "y", back: "2", due: "2026-12-20T00:00:00Z" },
      ],
      due_only: true,
      now: NOW,
    });
    expect(due.questions.map((q: Question) => q.card_id)).toEqual(["a"]);
    expect(due.available_cards).toBe(1);
    await expect(agent.call("quiz", { cards: [] })).rejects.toMatchObject({ code: "invalid_input" });
    await expect(agent.call("quiz", { cards: [{ id: "a", front: "x", back: "1" }, { id: "a", front: "y", back: "2" }] })).rejects.toMatchObject({ code: "invalid_input" });
    await expect(agent.call("quiz", { cards: [{ id: "a", front: "x", back: "1" }], choices: 9 })).rejects.toMatchObject({ code: "invalid_input" });
  });
});

describe("MCP stdio protocol", () => {
  it("lists and calls tools", async () => {
    const list = await handleMcpMessage(agent, { jsonrpc: "2.0", id: 1, method: "tools/list" });
    expect(list.result.tools.map((t: { name: string }) => t.name)).toEqual(["make_flashcards", "review", "quiz"]);
    const res = await handleMcpMessage(agent, { jsonrpc: "2.0", id: 2, method: "tools/call", params: { name: "review", arguments: { card: { id: "z" }, quality: 5, now: NOW } } });
    expect(res.result.isError).toBe(false);
    expect(res.result.structuredContent.card.interval_days).toBe(1);
    const bad = await handleMcpMessage(agent, { jsonrpc: "2.0", id: 3, method: "tools/call", params: { name: "review", arguments: { card: { id: "z" }, quality: 9 } } });
    expect(bad.result.isError).toBe(true);
  });
});

Find an agent

Do not include passwords, keys, customer data or other confidential details.
Rule-based matching is free during the public beta (fair use).