|
| 1 | +import { describe, it, expect } from "@jest/globals"; |
| 2 | +import { hasInterruptedStream } from "./retryEligibility"; |
| 3 | +import type { DisplayedMessage } from "@/types/message"; |
| 4 | + |
| 5 | +describe("hasInterruptedStream", () => { |
| 6 | + it("returns false for empty messages", () => { |
| 7 | + expect(hasInterruptedStream([])).toBe(false); |
| 8 | + }); |
| 9 | + |
| 10 | + it("returns true for stream-error message", () => { |
| 11 | + const messages: DisplayedMessage[] = [ |
| 12 | + { |
| 13 | + type: "user", |
| 14 | + id: "user-1", |
| 15 | + historyId: "user-1", |
| 16 | + content: "Hello", |
| 17 | + historySequence: 1, |
| 18 | + }, |
| 19 | + { |
| 20 | + type: "stream-error", |
| 21 | + id: "error-1", |
| 22 | + historyId: "assistant-1", |
| 23 | + error: "Connection failed", |
| 24 | + errorType: "network", |
| 25 | + historySequence: 2, |
| 26 | + }, |
| 27 | + ]; |
| 28 | + expect(hasInterruptedStream(messages)).toBe(true); |
| 29 | + }); |
| 30 | + |
| 31 | + it("returns true for partial assistant message", () => { |
| 32 | + const messages: DisplayedMessage[] = [ |
| 33 | + { |
| 34 | + type: "user", |
| 35 | + id: "user-1", |
| 36 | + historyId: "user-1", |
| 37 | + content: "Hello", |
| 38 | + historySequence: 1, |
| 39 | + }, |
| 40 | + { |
| 41 | + type: "assistant", |
| 42 | + id: "assistant-1", |
| 43 | + historyId: "assistant-1", |
| 44 | + content: "Incomplete response", |
| 45 | + historySequence: 2, |
| 46 | + streamSequence: 0, |
| 47 | + isStreaming: false, |
| 48 | + isPartial: true, |
| 49 | + isLastPartOfMessage: true, |
| 50 | + isCompacted: false, |
| 51 | + }, |
| 52 | + ]; |
| 53 | + expect(hasInterruptedStream(messages)).toBe(true); |
| 54 | + }); |
| 55 | + |
| 56 | + it("returns true for partial tool message", () => { |
| 57 | + const messages: DisplayedMessage[] = [ |
| 58 | + { |
| 59 | + type: "user", |
| 60 | + id: "user-1", |
| 61 | + historyId: "user-1", |
| 62 | + content: "Hello", |
| 63 | + historySequence: 1, |
| 64 | + }, |
| 65 | + { |
| 66 | + type: "tool", |
| 67 | + id: "tool-1", |
| 68 | + historyId: "assistant-1", |
| 69 | + toolName: "bash", |
| 70 | + toolCallId: "call-1", |
| 71 | + args: { script: "echo test" }, |
| 72 | + status: "interrupted", |
| 73 | + isPartial: true, |
| 74 | + historySequence: 2, |
| 75 | + streamSequence: 0, |
| 76 | + isLastPartOfMessage: true, |
| 77 | + }, |
| 78 | + ]; |
| 79 | + expect(hasInterruptedStream(messages)).toBe(true); |
| 80 | + }); |
| 81 | + |
| 82 | + it("returns true for partial reasoning message", () => { |
| 83 | + const messages: DisplayedMessage[] = [ |
| 84 | + { |
| 85 | + type: "user", |
| 86 | + id: "user-1", |
| 87 | + historyId: "user-1", |
| 88 | + content: "Hello", |
| 89 | + historySequence: 1, |
| 90 | + }, |
| 91 | + { |
| 92 | + type: "reasoning", |
| 93 | + id: "reasoning-1", |
| 94 | + historyId: "assistant-1", |
| 95 | + content: "Let me think...", |
| 96 | + historySequence: 2, |
| 97 | + streamSequence: 0, |
| 98 | + isStreaming: false, |
| 99 | + isPartial: true, |
| 100 | + isLastPartOfMessage: true, |
| 101 | + }, |
| 102 | + ]; |
| 103 | + expect(hasInterruptedStream(messages)).toBe(true); |
| 104 | + }); |
| 105 | + |
| 106 | + it("returns false for completed messages", () => { |
| 107 | + const messages: DisplayedMessage[] = [ |
| 108 | + { |
| 109 | + type: "user", |
| 110 | + id: "user-1", |
| 111 | + historyId: "user-1", |
| 112 | + content: "Hello", |
| 113 | + historySequence: 1, |
| 114 | + }, |
| 115 | + { |
| 116 | + type: "assistant", |
| 117 | + id: "assistant-1", |
| 118 | + historyId: "assistant-1", |
| 119 | + content: "Complete response", |
| 120 | + historySequence: 2, |
| 121 | + streamSequence: 0, |
| 122 | + isStreaming: false, |
| 123 | + isPartial: false, |
| 124 | + isLastPartOfMessage: true, |
| 125 | + isCompacted: false, |
| 126 | + }, |
| 127 | + ]; |
| 128 | + expect(hasInterruptedStream(messages)).toBe(false); |
| 129 | + }); |
| 130 | + |
| 131 | + it("returns true when last message is user message (app restarted during slow model)", () => { |
| 132 | + const messages: DisplayedMessage[] = [ |
| 133 | + { |
| 134 | + type: "user", |
| 135 | + id: "user-1", |
| 136 | + historyId: "user-1", |
| 137 | + content: "Hello", |
| 138 | + historySequence: 1, |
| 139 | + }, |
| 140 | + { |
| 141 | + type: "assistant", |
| 142 | + id: "assistant-1", |
| 143 | + historyId: "assistant-1", |
| 144 | + content: "Complete response", |
| 145 | + historySequence: 2, |
| 146 | + streamSequence: 0, |
| 147 | + isStreaming: false, |
| 148 | + isPartial: false, |
| 149 | + isLastPartOfMessage: true, |
| 150 | + isCompacted: false, |
| 151 | + }, |
| 152 | + { |
| 153 | + type: "user", |
| 154 | + id: "user-2", |
| 155 | + historyId: "user-2", |
| 156 | + content: "Another question", |
| 157 | + historySequence: 3, |
| 158 | + }, |
| 159 | + ]; |
| 160 | + expect(hasInterruptedStream(messages)).toBe(true); |
| 161 | + }); |
| 162 | + |
| 163 | + it("returns true when user message has no response (slow model scenario)", () => { |
| 164 | + const messages: DisplayedMessage[] = [ |
| 165 | + { |
| 166 | + type: "user", |
| 167 | + id: "user-1", |
| 168 | + historyId: "user-1", |
| 169 | + content: "Hello", |
| 170 | + historySequence: 1, |
| 171 | + }, |
| 172 | + ]; |
| 173 | + expect(hasInterruptedStream(messages)).toBe(true); |
| 174 | + }); |
| 175 | +}); |
0 commit comments