|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import type { CommitPolicyInput } from "./commit-policy"; |
| 4 | +import { applyCommitPolicy } from "./commit-policy"; |
| 5 | +import type { WriteContract } from "./index"; |
| 6 | + |
| 7 | +const defaultContract: WriteContract = { |
| 8 | + requiredSlots: ["day", "time"], |
| 9 | + intentKind: "plan" |
| 10 | +}; |
| 11 | + |
| 12 | +function buildInput(overrides: Partial<CommitPolicyInput>): CommitPolicyInput { |
| 13 | + return { |
| 14 | + turnType: "planning_request", |
| 15 | + extractedValues: {}, |
| 16 | + confidence: {}, |
| 17 | + unresolvable: [], |
| 18 | + priorResolvedSlots: {}, |
| 19 | + activeContract: defaultContract, |
| 20 | + ...overrides |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +describe("applyCommitPolicy", () => { |
| 25 | + it("does not commit slots for informational turns", () => { |
| 26 | + const result = applyCommitPolicy(buildInput({ |
| 27 | + turnType: "informational", |
| 28 | + extractedValues: { time: "15:00" }, |
| 29 | + confidence: { time: 0.95 } |
| 30 | + })); |
| 31 | + |
| 32 | + expect(result.committedSlots.time).toBeUndefined(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("does not commit slots for confirmation turns", () => { |
| 36 | + const result = applyCommitPolicy(buildInput({ |
| 37 | + turnType: "confirmation", |
| 38 | + extractedValues: { time: "15:00" }, |
| 39 | + confidence: { time: 0.95 } |
| 40 | + })); |
| 41 | + |
| 42 | + expect(result.committedSlots.time).toBeUndefined(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("commits slots above confidence threshold for planning_request", () => { |
| 46 | + const result = applyCommitPolicy(buildInput({ |
| 47 | + turnType: "planning_request", |
| 48 | + extractedValues: { time: "17:00", day: "tomorrow" }, |
| 49 | + confidence: { time: 0.9, day: 0.85 } |
| 50 | + })); |
| 51 | + |
| 52 | + expect(result.committedSlots.time).toBe("17:00"); |
| 53 | + expect(result.committedSlots.day).toBe("tomorrow"); |
| 54 | + expect(result.needsClarification).toEqual([]); |
| 55 | + }); |
| 56 | + |
| 57 | + it("routes low confidence extraction to needsClarification", () => { |
| 58 | + const result = applyCommitPolicy(buildInput({ |
| 59 | + turnType: "planning_request", |
| 60 | + extractedValues: { time: "17:00" }, |
| 61 | + confidence: { time: 0.6 } |
| 62 | + })); |
| 63 | + |
| 64 | + expect(result.committedSlots.time).toBeUndefined(); |
| 65 | + expect(result.needsClarification).toContain("time"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("does not commit correction below correction threshold", () => { |
| 69 | + const result = applyCommitPolicy(buildInput({ |
| 70 | + turnType: "clarification_answer", |
| 71 | + extractedValues: { time: "15:00" }, |
| 72 | + confidence: { time: 0.8 }, |
| 73 | + priorResolvedSlots: { time: "14:00" } |
| 74 | + })); |
| 75 | + |
| 76 | + expect(result.committedSlots.time).toBe("14:00"); |
| 77 | + expect(result.needsClarification).toContain("time"); |
| 78 | + }); |
| 79 | + |
| 80 | + it("commits correction at or above correction threshold", () => { |
| 81 | + const result = applyCommitPolicy(buildInput({ |
| 82 | + turnType: "clarification_answer", |
| 83 | + extractedValues: { time: "15:00" }, |
| 84 | + confidence: { time: 0.92 }, |
| 85 | + priorResolvedSlots: { time: "14:00" } |
| 86 | + })); |
| 87 | + |
| 88 | + expect(result.committedSlots.time).toBe("15:00"); |
| 89 | + expect(result.needsClarification).not.toContain("time"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("routes unresolvable slots to needsClarification", () => { |
| 93 | + const result = applyCommitPolicy(buildInput({ |
| 94 | + turnType: "clarification_answer", |
| 95 | + extractedValues: {}, |
| 96 | + unresolvable: ["time"] |
| 97 | + })); |
| 98 | + |
| 99 | + expect(result.committedSlots.time).toBeUndefined(); |
| 100 | + expect(result.needsClarification).toContain("time"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("resets prior slots on contract change", () => { |
| 104 | + const result = applyCommitPolicy(buildInput({ |
| 105 | + turnType: "planning_request", |
| 106 | + extractedValues: { day: "friday" }, |
| 107 | + confidence: { day: 0.9 }, |
| 108 | + priorResolvedSlots: { time: "14:00", day: "tomorrow" }, |
| 109 | + activeContract: { requiredSlots: ["day", "time"], intentKind: "edit" }, |
| 110 | + priorContract: { requiredSlots: ["day", "time"], intentKind: "plan" } |
| 111 | + })); |
| 112 | + |
| 113 | + expect(result.committedSlots.time).toBeUndefined(); |
| 114 | + expect(result.committedSlots.day).toBe("friday"); |
| 115 | + }); |
| 116 | + |
| 117 | + it("does not reset slots when contract intentKind is unchanged", () => { |
| 118 | + const result = applyCommitPolicy(buildInput({ |
| 119 | + turnType: "planning_request", |
| 120 | + extractedValues: { day: "friday" }, |
| 121 | + confidence: { day: 0.9 }, |
| 122 | + priorResolvedSlots: { time: "14:00" }, |
| 123 | + activeContract: { requiredSlots: ["day", "time"], intentKind: "plan" }, |
| 124 | + priorContract: { requiredSlots: ["day"], intentKind: "plan" } |
| 125 | + })); |
| 126 | + |
| 127 | + expect(result.committedSlots.time).toBe("14:00"); |
| 128 | + expect(result.committedSlots.day).toBe("friday"); |
| 129 | + }); |
| 130 | + |
| 131 | + it("derives missingSlots from post-commit state", () => { |
| 132 | + const result = applyCommitPolicy(buildInput({ |
| 133 | + turnType: "planning_request", |
| 134 | + extractedValues: { day: "tomorrow" }, |
| 135 | + confidence: { day: 0.9 }, |
| 136 | + activeContract: { requiredSlots: ["day", "time"], intentKind: "plan" } |
| 137 | + })); |
| 138 | + |
| 139 | + expect(result.missingSlots).toEqual(["time"]); |
| 140 | + expect(result.missingSlots).not.toContain("day"); |
| 141 | + }); |
| 142 | + |
| 143 | + it("reports all required slots as missing when nothing is committed", () => { |
| 144 | + const result = applyCommitPolicy(buildInput({ |
| 145 | + turnType: "planning_request", |
| 146 | + extractedValues: {}, |
| 147 | + activeContract: { requiredSlots: ["day", "time", "target"], intentKind: "plan" } |
| 148 | + })); |
| 149 | + |
| 150 | + expect(result.missingSlots).toEqual(["day", "time", "target"]); |
| 151 | + }); |
| 152 | + |
| 153 | + it("preserves prior resolved slots when new turn adds more", () => { |
| 154 | + const result = applyCommitPolicy(buildInput({ |
| 155 | + turnType: "clarification_answer", |
| 156 | + extractedValues: { time: "17:00" }, |
| 157 | + confidence: { time: 0.9 }, |
| 158 | + priorResolvedSlots: { day: "tomorrow" } |
| 159 | + })); |
| 160 | + |
| 161 | + expect(result.committedSlots.day).toBe("tomorrow"); |
| 162 | + expect(result.committedSlots.time).toBe("17:00"); |
| 163 | + }); |
| 164 | + |
| 165 | + it("commits slots for edit_request turn type", () => { |
| 166 | + const result = applyCommitPolicy(buildInput({ |
| 167 | + turnType: "edit_request", |
| 168 | + extractedValues: { time: "10:00" }, |
| 169 | + confidence: { time: 0.88 }, |
| 170 | + activeContract: { requiredSlots: ["time"], intentKind: "edit" } |
| 171 | + })); |
| 172 | + |
| 173 | + expect(result.committedSlots.time).toBe("10:00"); |
| 174 | + expect(result.missingSlots).toEqual([]); |
| 175 | + }); |
| 176 | + |
| 177 | + it("treats missing confidence as zero", () => { |
| 178 | + const result = applyCommitPolicy(buildInput({ |
| 179 | + turnType: "planning_request", |
| 180 | + extractedValues: { time: "17:00" }, |
| 181 | + confidence: {} |
| 182 | + })); |
| 183 | + |
| 184 | + expect(result.committedSlots.time).toBeUndefined(); |
| 185 | + expect(result.needsClarification).toContain("time"); |
| 186 | + }); |
| 187 | + |
| 188 | + it("handles unresolvable slots not in extractedValues", () => { |
| 189 | + const result = applyCommitPolicy(buildInput({ |
| 190 | + turnType: "clarification_answer", |
| 191 | + extractedValues: { day: "friday" }, |
| 192 | + confidence: { day: 0.9 }, |
| 193 | + unresolvable: ["time"] |
| 194 | + })); |
| 195 | + |
| 196 | + expect(result.committedSlots.day).toBe("friday"); |
| 197 | + expect(result.needsClarification).toContain("time"); |
| 198 | + }); |
| 199 | +}); |
0 commit comments