|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { computeMessageDurationStart } from "./ChatView.logic"; |
| 3 | + |
| 4 | +describe("computeMessageDurationStart", () => { |
| 5 | + it("returns message createdAt when there is no preceding user message", () => { |
| 6 | + const result = computeMessageDurationStart([ |
| 7 | + { id: "a1", role: "assistant", createdAt: "2026-01-01T00:00:05Z", completedAt: "2026-01-01T00:00:10Z" }, |
| 8 | + ]); |
| 9 | + expect(result).toEqual(new Map([["a1", "2026-01-01T00:00:05Z"]])); |
| 10 | + }); |
| 11 | + |
| 12 | + it("uses the user message createdAt for the first assistant response", () => { |
| 13 | + const result = computeMessageDurationStart([ |
| 14 | + { id: "u1", role: "user", createdAt: "2026-01-01T00:00:00Z" }, |
| 15 | + { id: "a1", role: "assistant", createdAt: "2026-01-01T00:00:30Z", completedAt: "2026-01-01T00:00:30Z" }, |
| 16 | + ]); |
| 17 | + expect(result).toEqual( |
| 18 | + new Map([ |
| 19 | + ["u1", "2026-01-01T00:00:00Z"], // user: own createdAt |
| 20 | + ["a1", "2026-01-01T00:00:00Z"], // assistant: user's createdAt |
| 21 | + ]), |
| 22 | + ); |
| 23 | + }); |
| 24 | + |
| 25 | + it("uses the previous assistant completedAt for subsequent assistant responses", () => { |
| 26 | + const result = computeMessageDurationStart([ |
| 27 | + { id: "u1", role: "user", createdAt: "2026-01-01T00:00:00Z" }, |
| 28 | + { id: "a1", role: "assistant", createdAt: "2026-01-01T00:00:30Z", completedAt: "2026-01-01T00:00:30Z" }, |
| 29 | + { id: "a2", role: "assistant", createdAt: "2026-01-01T00:00:55Z", completedAt: "2026-01-01T00:00:55Z" }, |
| 30 | + ]); |
| 31 | + expect(result).toEqual( |
| 32 | + new Map([ |
| 33 | + ["u1", "2026-01-01T00:00:00Z"], // user: own createdAt |
| 34 | + ["a1", "2026-01-01T00:00:00Z"], // first assistant: from user (duration = 30s) |
| 35 | + ["a2", "2026-01-01T00:00:30Z"], // second assistant: from first assistant's completedAt (duration = 25s) |
| 36 | + ]), |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it("does not advance the boundary for a streaming message without completedAt", () => { |
| 41 | + const result = computeMessageDurationStart([ |
| 42 | + { id: "u1", role: "user", createdAt: "2026-01-01T00:00:00Z" }, |
| 43 | + { id: "a1", role: "assistant", createdAt: "2026-01-01T00:00:30Z" }, // streaming, no completedAt |
| 44 | + { id: "a2", role: "assistant", createdAt: "2026-01-01T00:00:55Z", completedAt: "2026-01-01T00:00:55Z" }, |
| 45 | + ]); |
| 46 | + expect(result).toEqual( |
| 47 | + new Map([ |
| 48 | + ["u1", "2026-01-01T00:00:00Z"], // user |
| 49 | + ["a1", "2026-01-01T00:00:00Z"], // streaming assistant: from user |
| 50 | + ["a2", "2026-01-01T00:00:00Z"], // next assistant: still from user (boundary not advanced) |
| 51 | + ]), |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it("resets the boundary on a new user message", () => { |
| 56 | + const result = computeMessageDurationStart([ |
| 57 | + { id: "u1", role: "user", createdAt: "2026-01-01T00:00:00Z" }, |
| 58 | + { id: "a1", role: "assistant", createdAt: "2026-01-01T00:00:30Z", completedAt: "2026-01-01T00:00:30Z" }, |
| 59 | + { id: "u2", role: "user", createdAt: "2026-01-01T00:01:00Z" }, |
| 60 | + { id: "a2", role: "assistant", createdAt: "2026-01-01T00:01:20Z", completedAt: "2026-01-01T00:01:20Z" }, |
| 61 | + ]); |
| 62 | + expect(result).toEqual( |
| 63 | + new Map([ |
| 64 | + ["u1", "2026-01-01T00:00:00Z"], // first user |
| 65 | + ["a1", "2026-01-01T00:00:00Z"], // first assistant: from first user |
| 66 | + ["u2", "2026-01-01T00:01:00Z"], // second user: own createdAt |
| 67 | + ["a2", "2026-01-01T00:01:00Z"], // second assistant: from second user (not first assistant) |
| 68 | + ]), |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it("handles system messages without affecting the boundary", () => { |
| 73 | + const result = computeMessageDurationStart([ |
| 74 | + { id: "u1", role: "user", createdAt: "2026-01-01T00:00:00Z" }, |
| 75 | + { id: "s1", role: "system", createdAt: "2026-01-01T00:00:01Z" }, |
| 76 | + { id: "a1", role: "assistant", createdAt: "2026-01-01T00:00:30Z", completedAt: "2026-01-01T00:00:30Z" }, |
| 77 | + ]); |
| 78 | + expect(result).toEqual( |
| 79 | + new Map([ |
| 80 | + ["u1", "2026-01-01T00:00:00Z"], // user |
| 81 | + ["s1", "2026-01-01T00:00:00Z"], // system: inherits user boundary |
| 82 | + ["a1", "2026-01-01T00:00:00Z"], // assistant: from user |
| 83 | + ]), |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it("returns empty map for empty input", () => { |
| 88 | + expect(computeMessageDurationStart([])).toEqual(new Map()); |
| 89 | + }); |
| 90 | +}); |
0 commit comments