|
| 1 | +// Tests for CLI message text formatting helpers (renderMessageList, formatMessageCliText). |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { formatMessageCliText } from "./message-format.js"; |
| 4 | + |
| 5 | +function readResultPayload(payload: unknown) { |
| 6 | + return { |
| 7 | + kind: "action" as const, |
| 8 | + channel: "matrix" as const, |
| 9 | + action: "read" as const, |
| 10 | + handledBy: "plugin" as const, |
| 11 | + payload, |
| 12 | + dryRun: false, |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +function pinsResultPayload(payload: unknown) { |
| 17 | + return { |
| 18 | + kind: "action" as const, |
| 19 | + channel: "matrix" as const, |
| 20 | + action: "list-pins" as const, |
| 21 | + handledBy: "plugin" as const, |
| 22 | + payload, |
| 23 | + dryRun: false, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +function searchResultPayload(payload: unknown) { |
| 28 | + return { |
| 29 | + kind: "action" as const, |
| 30 | + channel: "discord" as const, |
| 31 | + action: "search" as const, |
| 32 | + handledBy: "plugin" as const, |
| 33 | + payload, |
| 34 | + dryRun: false, |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +function msg(id: string, ts: string, authorTag: string, content: string) { |
| 39 | + return { id, ts, authorTag, content }; |
| 40 | +} |
| 41 | + |
| 42 | +function textJoined(lines: string[]): string { |
| 43 | + return lines.join("\n"); |
| 44 | +} |
| 45 | + |
| 46 | +describe("formatMessageCliText displayLimit", () => { |
| 47 | + it("honors displayLimit for message read", () => { |
| 48 | + const messages = Array.from({ length: 40 }, (_, i) => |
| 49 | + msg(`id-${i}`, `2026-01-01T00:00:0${i % 10}.000Z`, `user-${i}`, `text-${i}`), |
| 50 | + ); |
| 51 | + |
| 52 | + const result = readResultPayload({ messages }); |
| 53 | + const out = textJoined(formatMessageCliText(result, { displayLimit: 15 })); |
| 54 | + |
| 55 | + // Should include items within the limit |
| 56 | + expect(out).toContain("text-0"); |
| 57 | + expect(out).toContain("text-14"); |
| 58 | + // Should NOT include items beyond the limit |
| 59 | + expect(out).not.toContain("text-15"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("honors displayLimit for list-pins", () => { |
| 63 | + const pins = Array.from({ length: 40 }, (_, i) => |
| 64 | + msg(`pin-${i}`, `2026-01-01T00:00:0${i % 10}.000Z`, `user-${i}`, `pin-text-${i}`), |
| 65 | + ); |
| 66 | + |
| 67 | + const result = pinsResultPayload({ pins }); |
| 68 | + const out = textJoined(formatMessageCliText(result, { displayLimit: 15 })); |
| 69 | + |
| 70 | + expect(out).toContain("pin-text-0"); |
| 71 | + expect(out).toContain("pin-text-14"); |
| 72 | + expect(out).not.toContain("pin-text-15"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("honors displayLimit for search", () => { |
| 76 | + const messages = Array.from({ length: 40 }, (_, i) => |
| 77 | + msg(`id-${i}`, `2026-01-01T00:00:0${i % 10}.000Z`, `user-${i}`, `search-${i}`), |
| 78 | + ); |
| 79 | + // Discord search returns messages as array-of-array |
| 80 | + const wrapped = messages.map((m) => [m]); |
| 81 | + |
| 82 | + const result = searchResultPayload({ results: { messages: wrapped } }); |
| 83 | + const out = textJoined(formatMessageCliText(result, { displayLimit: 15 })); |
| 84 | + |
| 85 | + expect(out).toContain("search-0"); |
| 86 | + expect(out).toContain("search-14"); |
| 87 | + expect(out).not.toContain("search-15"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("defaults to 25 when no displayLimit is provided", () => { |
| 91 | + const messages = Array.from({ length: 50 }, (_, i) => |
| 92 | + msg(`id-${i}`, `2026-01-01T00:00:0${i % 10}.000Z`, `user-${i}`, `text-${i}`), |
| 93 | + ); |
| 94 | + |
| 95 | + const result = readResultPayload({ messages }); |
| 96 | + const out = textJoined(formatMessageCliText(result)); |
| 97 | + |
| 98 | + expect(out).toContain("text-24"); |
| 99 | + expect(out).not.toContain("text-25"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("renders all rows when total is below displayLimit", () => { |
| 103 | + const messages = [ |
| 104 | + msg("id-1", "2026-01-01T00:00:00.000Z", "alice", "hello"), |
| 105 | + msg("id-2", "2026-01-01T00:00:01.000Z", "bob", "world"), |
| 106 | + ]; |
| 107 | + |
| 108 | + const result = readResultPayload({ messages }); |
| 109 | + const out = textJoined(formatMessageCliText(result, { displayLimit: 30 })); |
| 110 | + |
| 111 | + expect(out).toContain("hello"); |
| 112 | + expect(out).toContain("world"); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe("renderPaginationHint", () => { |
| 117 | + it("emits hint when payload has hasMore: true", () => { |
| 118 | + const messages = [msg("id-1", "2026-01-01T00:00:00.000Z", "alice", "hello")]; |
| 119 | + const result = readResultPayload({ messages, hasMore: true }); |
| 120 | + const out = textJoined(formatMessageCliText(result, { displayLimit: 5 })); |
| 121 | + |
| 122 | + expect(out).toContain("More results available"); |
| 123 | + }); |
| 124 | + |
| 125 | + it("emits hint when payload has nextBatch string", () => { |
| 126 | + const messages = [msg("id-1", "2026-01-01T00:00:00.000Z", "alice", "hello")]; |
| 127 | + const result = readResultPayload({ messages, nextBatch: "token-123" }); |
| 128 | + const out = textJoined(formatMessageCliText(result, { displayLimit: 5 })); |
| 129 | + |
| 130 | + expect(out).toContain("More results available"); |
| 131 | + }); |
| 132 | + |
| 133 | + it("emits hint when payload has @odata.nextLink string", () => { |
| 134 | + const messages = [msg("id-1", "2026-01-01T00:00:00.000Z", "alice", "hello")]; |
| 135 | + const result = readResultPayload({ |
| 136 | + messages, |
| 137 | + "@odata.nextLink": "https://graph.microsoft.com/v1.0/next", |
| 138 | + }); |
| 139 | + const out = textJoined(formatMessageCliText(result, { displayLimit: 5 })); |
| 140 | + |
| 141 | + expect(out).toContain("More results available"); |
| 142 | + }); |
| 143 | + |
| 144 | + it("emits hint when search results has hasMore without total_results", () => { |
| 145 | + const messages = [msg("id-1", "2026-01-01T00:00:00.000Z", "alice", "hello")]; |
| 146 | + // hasMore: true inside results — Discord does not always include total_results. |
| 147 | + const wrapped = messages.map((m) => [m]); |
| 148 | + const result = searchResultPayload({ |
| 149 | + results: { messages: wrapped, hasMore: true }, |
| 150 | + }); |
| 151 | + const out = textJoined(formatMessageCliText(result, { displayLimit: 5 })); |
| 152 | + |
| 153 | + expect(out).toContain("More results available"); |
| 154 | + }); |
| 155 | + |
| 156 | + it("emits hint when total_results exceeds returned count (Discord search)", () => { |
| 157 | + const messages = [msg("id-1", "2026-01-01T00:00:00.000Z", "alice", "hello")]; |
| 158 | + // Discord search wraps messages and total_results inside a results object. |
| 159 | + // total_results: 200 with 1 returned message → 199 more exist. |
| 160 | + const wrapped = messages.map((m) => [m]); |
| 161 | + const result = searchResultPayload({ |
| 162 | + results: { messages: wrapped, total_results: 200 }, |
| 163 | + }); |
| 164 | + const out = textJoined(formatMessageCliText(result, { displayLimit: 5 })); |
| 165 | + |
| 166 | + expect(out).toContain("More results available"); |
| 167 | + }); |
| 168 | + |
| 169 | + it("does NOT emit hint when total_results equals returned count (completed search)", () => { |
| 170 | + const messages = [ |
| 171 | + msg("id-1", "2026-01-01T00:00:00.000Z", "alice", "hello"), |
| 172 | + msg("id-2", "2026-01-01T00:00:01.000Z", "bob", "world"), |
| 173 | + ]; |
| 174 | + // total_results: 2 with 2 returned messages → search is complete. |
| 175 | + const wrapped = messages.map((m) => [m]); |
| 176 | + const result = searchResultPayload({ |
| 177 | + results: { messages: wrapped, total_results: 2 }, |
| 178 | + }); |
| 179 | + const out = textJoined(formatMessageCliText(result, { displayLimit: 5 })); |
| 180 | + |
| 181 | + expect(out).not.toContain("More results available"); |
| 182 | + }); |
| 183 | + |
| 184 | + it("does NOT emit hint when no pagination signal is present", () => { |
| 185 | + const messages = [msg("id-1", "2026-01-01T00:00:00.000Z", "alice", "hello")]; |
| 186 | + const result = readResultPayload({ messages }); |
| 187 | + const out = textJoined(formatMessageCliText(result, { displayLimit: 5 })); |
| 188 | + |
| 189 | + expect(out).not.toContain("More results available"); |
| 190 | + }); |
| 191 | +}); |
0 commit comments