File size: 9,486 Bytes
3e05655
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { test } from "@playwright/test"
import {
  defineVisualRegions,
  reportVisualStability,
  startVisualProbe,
  stopVisualProbe,
  visualPlan,
} from "../../utils/visual-stability"
import {
  assistantMessage,
  partUpdated,
  setupTimeline,
  shell,
  textPart,
  userMessage,
  waitForVisualSettle,
} from "./fixture"

const profiles = [
  {
    name: "empty running to completed",
    updates: [{ state: "completed" as const, output: "", delay: 350 }],
  },
  {
    name: "50 lines arriving incrementally",
    updates: [
      { state: "running" as const, output: lines(1), delay: 100 },
      { state: "running" as const, output: lines(10), delay: 160 },
      { state: "running" as const, output: lines(25), delay: 90 },
      { state: "running" as const, output: lines(50), delay: 220 },
      { state: "completed" as const, output: lines(50), delay: 500 },
    ],
  },
  {
    name: "wide ANSI and CRLF output",
    updates: [
      {
        state: "running" as const,
        output: Array.from({ length: 20 }, (_, index) => `\u001b[32mline ${index}\u001b[0m ${"wide-".repeat(30)}`).join(
          "\r\n",
        ),
        delay: 240,
      },
      {
        state: "completed" as const,
        output: Array.from({ length: 20 }, (_, index) => `line ${index} ${"wide-".repeat(30)}`).join("\n"),
        delay: 500,
      },
    ],
  },
] as const

for (const profile of profiles) {
  test(`keeps rows stable for shell ${profile.name}`, async ({ page }, testInfo) => {
    const shellID = `prt_matrix_${profiles.indexOf(profile)}_01_shell`
    const followingID = `prt_matrix_${profiles.indexOf(profile)}_02_following`
    const timeline = await setupTimeline(page, {
      messages: [
        userMessage(),
        assistantMessage([shell(shellID, "running"), textPart(followingID, "Following shell row")], {
          completed: false,
        }),
      ],
      settings: { shellToolPartsExpanded: true },
      cpuRate: 4,
      seedHistory: true,
    })
    const scroller = page.locator(".scroll-view__viewport", { has: page.locator("[data-timeline-row]") })
    await scroller.evaluate((element) => (element.scrollTop = element.scrollHeight))
    await waitForVisualSettle(page, [
      `[data-timeline-part-id="${shellID}"]`,
      `[data-timeline-part-id="${followingID}"]`,
    ])
    const regions = defineVisualRegions({
      shell: { selector: `[data-timeline-part-id="${shellID}"]`, closest: '[data-timeline-row="AssistantPart"]' },
      following: {
        selector: `[data-timeline-part-id="${followingID}"]`,
        closest: '[data-timeline-row="AssistantPart"]',
      },
    })
    await startVisualProbe(page, regions)
    for (const update of profile.updates) {
      await timeline.send(partUpdated(shell(shellID, update.state, update.output)), update.delay)
    }
    const trace = await stopVisualProbe<keyof typeof regions>(page)
    await reportVisualStability(
      testInfo,
      `shell-${profiles.indexOf(profile)}`,
      trace,
      visualPlan(
        regions,
        [
          { type: "required", regions: ["shell", "following"] },
          { type: "unique", regions: ["shell", "following"] },
          { type: "stable", regions: ["shell", "following"] },
          { type: "opacity", regions: "all" },
          { type: "continuity", regions: "all" },
          { type: "motion", regions: "all", maxPositionReversals: 0, maxReversals: 1 },
          { type: "label-stability", regions: "all" },
          { type: "preserve-bottom-anchor" },
          { type: "flow", regions: ["shell", "following"] },
        ],
        { perMarker: true },
      ),
    )
  })
}

test("keeps following row stable when a collapsed shell receives 50 lines", async ({ page }, testInfo) => {
  const shellID = "prt_matrix_collapsed_01_shell"
  const followingID = "prt_matrix_collapsed_02_following"
  const timeline = await setupTimeline(page, {
    messages: [
      userMessage(),
      assistantMessage([shell(shellID, "running"), textPart(followingID, "Following collapsed shell")], {
        completed: false,
      }),
    ],
    settings: { shellToolPartsExpanded: false },
    cpuRate: 4,
    seedHistory: true,
  })
  await waitForVisualSettle(page, [`[data-timeline-part-id="${shellID}"]`, `[data-timeline-part-id="${followingID}"]`])
  const regions = defineVisualRegions({
    shell: { selector: `[data-timeline-part-id="${shellID}"]`, closest: '[data-timeline-row="AssistantPart"]' },
    following: { selector: `[data-timeline-part-id="${followingID}"]`, closest: '[data-timeline-row="AssistantPart"]' },
  })
  await startVisualProbe(page, regions)
  await timeline.send(partUpdated(shell(shellID, "running", lines(50))), 240)
  await timeline.send(partUpdated(shell(shellID, "completed", lines(50))), 500)
  const trace = await stopVisualProbe<keyof typeof regions>(page)
  await reportVisualStability(
    testInfo,
    "collapsed-shell",
    trace,
    visualPlan(
      regions,
      [
        { type: "required", regions: ["shell", "following"] },
        { type: "unique", regions: ["shell", "following"] },
        { type: "stable", regions: ["shell", "following"] },
        { type: "opacity", regions: "all" },
        { type: "continuity", regions: "all" },
        { type: "motion", regions: ["following"], maxPositionReversals: 0 },
        { type: "label-stability", regions: "all" },
        { type: "flow", regions: ["shell", "following"] },
      ],
      { perMarker: true },
    ),
  )
})

test("keeps rows stable when a running shell becomes an error", async ({ page }, testInfo) => {
  const shellID = "prt_matrix_error_01_shell"
  const followingID = "prt_matrix_error_02_following"
  const timeline = await setupTimeline(page, {
    messages: [
      userMessage(),
      assistantMessage([shell(shellID, "running", lines(10)), textPart(followingID, "Following failed shell")], {
        completed: false,
      }),
    ],
    settings: { shellToolPartsExpanded: true },
    cpuRate: 4,
    seedHistory: true,
  })
  await waitForVisualSettle(page, [`[data-timeline-part-id="${shellID}"]`, `[data-timeline-part-id="${followingID}"]`])
  const regions = defineVisualRegions({
    shell: { selector: `[data-timeline-part-id="${shellID}"]`, closest: '[data-timeline-row="AssistantPart"]' },
    following: { selector: `[data-timeline-part-id="${followingID}"]`, closest: '[data-timeline-row="AssistantPart"]' },
  })
  await startVisualProbe(page, regions)
  await timeline.send(
    partUpdated({
      ...shell(shellID, "error"),
      state: {
        status: "error",
        input: { command: `echo ${shellID}` },
        error: "Command failed after output",
        metadata: {},
        time: { start: 1700000001000, end: 1700000002000 },
      },
    }),
    500,
  )
  const trace = await stopVisualProbe<keyof typeof regions>(page)
  await reportVisualStability(
    testInfo,
    "shell-error",
    trace,
    visualPlan(
      regions,
      [
        { type: "required", regions: ["shell", "following"] },
        { type: "unique", regions: ["shell", "following"] },
        { type: "stable", regions: ["shell", "following"] },
        { type: "opacity", regions: "all" },
        { type: "continuity", regions: "all" },
        { type: "motion", regions: ["following"], maxPositionReversals: 0 },
        { type: "label-stability", regions: "all" },
        { type: "preserve-bottom-anchor" },
        { type: "flow", regions: ["shell", "following"] },
      ],
      { perMarker: true },
    ),
  )
})

test("keeps rows stable when later text arrives before shell output", async ({ page }, testInfo) => {
  const shellID = "prt_late_text_01_shell"
  const followingID = "prt_late_text_02_following"
  const timeline = await setupTimeline(page, {
    messages: [userMessage(), assistantMessage([shell(shellID, "running")], { completed: false })],
    settings: { shellToolPartsExpanded: true },
    cpuRate: 4,
    seedHistory: true,
  })
  await waitForVisualSettle(page, [`[data-timeline-part-id="${shellID}"]`])
  const regions = defineVisualRegions({
    shell: { selector: `[data-timeline-part-id="${shellID}"]`, closest: '[data-timeline-row="AssistantPart"]' },
    following: {
      selector: `[data-timeline-part-id="${followingID}"]`,
      closest: '[data-timeline-row="AssistantPart"]',
    },
  })
  await startVisualProbe(page, regions)
  await timeline.send(partUpdated(textPart(followingID, "Later assistant content arrived before shell output.")), 240)
  await timeline.send(partUpdated(shell(shellID, "running", lines(20))), 300)
  await timeline.send(partUpdated(shell(shellID, "completed", lines(20))), 600)
  const trace = await stopVisualProbe<keyof typeof regions>(page)
  await reportVisualStability(
    testInfo,
    "late-text-before-shell-output",
    trace,
    visualPlan(
      regions,
      [
        { type: "required", regions: ["shell", "following"] },
        { type: "unique", regions: ["shell", "following"] },
        { type: "stable", regions: ["shell"] },
        { type: "opacity", regions: "all" },
        { type: "continuity", regions: "all" },
        { type: "motion", regions: "all", maxPositionReversals: 0 },
        { type: "label-stability", regions: "all" },
        { type: "preserve-bottom-anchor" },
        { type: "flow", regions: ["shell", "following"] },
      ],
      { perMarker: true },
    ),
  )
})

function lines(count: number) {
  return Array.from({ length: count }, (_, index) => `line ${index + 1}`).join("\n")
}