File size: 3,252 Bytes
6dec997
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { describe, expect, it } from "vitest";
import { z } from "zod";
import {
  ContractParseError,
  ContractValidationError,
  generateContract,
  staticModel
} from "../src/index.js";

const Profile = z.object({
  name: z.string(),
  seniority: z.enum(["junior", "mid", "senior"]),
  skills: z.array(z.string()).min(1)
});

describe("generateContract", () => {
  it("returns validated data from strict JSON", async () => {
    const result = await generateContract({
      model: staticModel([
        JSON.stringify({ name: "Mira", seniority: "senior", skills: ["infra"] })
      ]),
      schema: Profile,
      prompt: "Extract the profile."
    });

    expect(result.data.name).toBe("Mira");
    expect(result.attempts).toBe(1);
  });

  it("extracts JSON from a fenced response", async () => {
    const result = await generateContract({
      model: staticModel([
        "```json\n{\"name\":\"Lee\",\"seniority\":\"mid\",\"skills\":[\"ml\"]}\n```"
      ]),
      schema: Profile,
      prompt: "Extract the profile."
    });

    expect(result.data).toEqual({
      name: "Lee",
      seniority: "mid",
      skills: ["ml"]
    });
  });

  it("repairs validation failures using a retry prompt", async () => {
    const prompts: string[] = [];

    const result = await generateContract({
      model: {
        async generate(prompt) {
          prompts.push(prompt);
          if (prompts.length === 1) {
            return JSON.stringify({ name: "Ari", seniority: "principal", skills: [] });
          }

          return JSON.stringify({ name: "Ari", seniority: "senior", skills: ["platform"] });
        }
      },
      schema: Profile,
      prompt: "Extract the profile.",
      retries: 1
    });

    expect(result.data.seniority).toBe("senior");
    expect(result.attempts).toBe(2);
    expect(prompts[1]).toContain("Return only corrected JSON");
    expect(result.replay.attempts[0]?.issues).toHaveLength(2);
  });

  it("emits useful events", async () => {
    const events: string[] = [];

    await generateContract({
      model: staticModel([
        JSON.stringify({ name: "Noor", seniority: "junior", skills: ["ops"] })
      ]),
      schema: Profile,
      prompt: "Extract the profile.",
      onEvent(event) {
        events.push(event.type);
      }
    });

    expect(events).toEqual(["attempt", "success"]);
  });

  it("throws validation errors with replay data", async () => {
    await expect(
      generateContract({
        model: staticModel([JSON.stringify({ name: "Kai", seniority: "staff", skills: [] })]),
        schema: Profile,
        prompt: "Extract the profile."
      })
    ).rejects.toMatchObject({
      name: "ContractValidationError",
      replay: {
        attempts: [
          expect.objectContaining({
            attempt: 1,
            rawText: expect.any(String)
          })
        ]
      }
    } satisfies Partial<ContractValidationError>);
  });

  it("throws parse errors after retries are exhausted", async () => {
    await expect(
      generateContract({
        model: staticModel(["not json", "still not json"]),
        schema: Profile,
        prompt: "Extract the profile.",
        retries: 1
      })
    ).rejects.toBeInstanceOf(ContractParseError);
  });
});