|
| 1 | +# SPDX-FileCopyrightText: Sudo Apt Holdings LLC |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +defmodule Trinity.Sessions.PromptWindowTest do |
| 4 | + @moduledoc """ |
| 5 | + Slice 014 AC1 and AC2: the window a prompt is built from is the **recent** conversation. |
| 6 | +
|
| 7 | + No existing test reaches a session longer than the window, which is why this went unseen. Every |
| 8 | + assertion here is at a row count above it, because below it the defect is invisible by definition. |
| 9 | + """ |
| 10 | + use Trinity.DataCase, async: false |
| 11 | + |
| 12 | + alias Trinity.Sessions |
| 13 | + |
| 14 | + @window 500 |
| 15 | + @rows 520 |
| 16 | + |
| 17 | + defp session_with_rows!(n) do |
| 18 | + session = Trinity.Factory.session!() |
| 19 | + |
| 20 | + rows = |
| 21 | + for seq <- 1..n do |
| 22 | + now = DateTime.utc_now() |
| 23 | + |
| 24 | + %{ |
| 25 | + id: Trinity.UUID.generate(), |
| 26 | + session_id: session.id, |
| 27 | + seq: seq, |
| 28 | + role: if(rem(seq, 2) == 1, do: "user", else: "assistant"), |
| 29 | + content: "message #{seq}", |
| 30 | + parts: %{"text" => "message #{seq}"}, |
| 31 | + inserted_at: now, |
| 32 | + updated_at: now |
| 33 | + } |
| 34 | + end |
| 35 | + |
| 36 | + Trinity.Repo.insert_all(Trinity.Sessions.Message, rows) |
| 37 | + session |
| 38 | + end |
| 39 | + |
| 40 | + test "AC1: the window ends at the newest row, not the five-hundredth" do |
| 41 | + session = session_with_rows!(@rows) |
| 42 | + |
| 43 | + seqs = Sessions.recent_history(session.id, limit: @window) |> Enum.map(& &1.seq) |
| 44 | + |
| 45 | + assert List.last(seqs) == @rows, |
| 46 | + "the prompt window ends at seq #{List.last(seqs)} of #{@rows}. Past the window the " <> |
| 47 | + "assistant answers from the opening of the session with the current exchange missing" |
| 48 | + |
| 49 | + assert length(seqs) == @window |
| 50 | + assert List.first(seqs) == @rows - @window + 1 |
| 51 | + end |
| 52 | + |
| 53 | + test "AC2: the window reads oldest to newest, because a conversation is an order" do |
| 54 | + session = session_with_rows!(@rows) |
| 55 | + seqs = Sessions.recent_history(session.id, limit: @window) |> Enum.map(& &1.seq) |
| 56 | + |
| 57 | + assert seqs == Enum.sort(seqs), |
| 58 | + "the window came back newest-first, which would present the conversation backwards" |
| 59 | + end |
| 60 | + |
| 61 | + test "a session shorter than the window is unchanged by any of this" do |
| 62 | + session = session_with_rows!(10) |
| 63 | + seqs = Sessions.recent_history(session.id, limit: @window) |> Enum.map(& &1.seq) |
| 64 | + assert seqs == Enum.to_list(1..10) |
| 65 | + end |
| 66 | + |
| 67 | + test "AC3: history/2 still pages forwards from the beginning, for the callers that walk a session" do |
| 68 | + session = session_with_rows!(@rows) |
| 69 | + |
| 70 | + first_page = Sessions.history(session.id, limit: 10) |> Enum.map(& &1.seq) |
| 71 | + second_page = Sessions.history(session.id, limit: 10, offset: 10) |> Enum.map(& &1.seq) |
| 72 | + |
| 73 | + assert first_page == Enum.to_list(1..10), |
| 74 | + "history/2 is the forward pager the UI and the export use; this slice must not change it" |
| 75 | + |
| 76 | + assert second_page == Enum.to_list(11..20) |
| 77 | + end |
| 78 | +end |
0 commit comments