-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannel-message-ops-e2e.test.ts
More file actions
301 lines (264 loc) · 9.17 KB
/
Copy pathchannel-message-ops-e2e.test.ts
File metadata and controls
301 lines (264 loc) · 9.17 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import {
describe,
it,
beforeEach,
afterEach,
beforeAll,
afterAll,
expect,
} from "vitest";
import {
E2E_API_KEY,
SHOULD_SKIP_E2E,
cleanupTrackedResources,
setupTestFailureHandler,
resetTestTracking,
} from "../../helpers/e2e-test-helper.js";
import { runCommand } from "../../helpers/command-helpers.js";
import { parseNdjsonLines } from "../../helpers/ndjson.js";
import {
SHOULD_SKIP_MUTABLE_TESTS,
setupMutableMessagesRule,
teardownMutableMessagesRule,
getMutableChannelName,
publishAndGetSerial,
} from "../../helpers/e2e-mutable-messages.js";
describe.skipIf(SHOULD_SKIP_E2E || SHOULD_SKIP_MUTABLE_TESTS)(
"Channel Message Operations E2E Tests",
() => {
let channelName: string;
let messageSerial: string;
beforeAll(async () => {
// Create channel rule with mutableMessages enabled
await setupMutableMessagesRule();
// Publish a test message and get its serial for use in all tests
channelName = getMutableChannelName("msg-ops");
messageSerial = await publishAndGetSerial(channelName, "test-msg");
});
afterAll(async () => {
await teardownMutableMessagesRule();
});
beforeEach(() => {
resetTestTracking();
});
afterEach(async () => {
await cleanupTrackedResources();
});
it(
"should update a message via channels update",
{ timeout: 60000 },
async () => {
setupTestFailureHandler("should update a message via channels update");
const result = await runCommand(
[
"channels",
"update",
channelName,
messageSerial,
"updated-text",
"--json",
],
{
env: { ABLY_API_KEY: E2E_API_KEY || "" },
timeoutMs: 30000,
},
);
expect(result.exitCode).toBe(0);
const records = parseNdjsonLines(result.stdout);
const parsed = records.find((r) => r.type === "result") ?? records[0];
expect(parsed.success).toBe(true);
expect(parsed.message).toBeDefined();
},
);
it(
"should append to a message via channels append",
{ timeout: 60000 },
async () => {
setupTestFailureHandler(
"should append to a message via channels append",
);
const result = await runCommand(
[
"channels",
"append",
channelName,
messageSerial,
"appended-text",
"--json",
],
{
env: { ABLY_API_KEY: E2E_API_KEY || "" },
timeoutMs: 30000,
},
);
expect(result.exitCode).toBe(0);
const records = parseNdjsonLines(result.stdout);
const parsed = records.find((r) => r.type === "result") ?? records[0];
expect(parsed.success).toBe(true);
expect(parsed.message).toBeDefined();
},
);
it(
"should retrieve a message via channels get-message",
{ timeout: 60000 },
async () => {
setupTestFailureHandler(
"should retrieve a message via channels get-message",
);
// Use a fresh channel/serial so we don't see updates from other tests
const getChannel = getMutableChannelName("msg-get");
const serial = await publishAndGetSerial(getChannel, "fresh-message");
const result = await runCommand(
["channels", "get-message", getChannel, serial, "--json"],
{
env: { ABLY_API_KEY: E2E_API_KEY || "" },
timeoutMs: 30000,
},
);
expect(result.exitCode).toBe(0);
const records = parseNdjsonLines(result.stdout);
const parsed = records.find((r) => r.type === "result") ?? records[0];
expect(parsed.success).toBe(true);
expect(parsed.message).toBeDefined();
const message = parsed.message as Record<string, unknown>;
expect(message.serial).toBe(serial);
expect(message.data).toBe("fresh-message");
// Timestamp must be ISO 8601 (history-style normalisation)
expect(message.timestamp).toMatch(
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/,
);
},
);
it(
"should return the latest version after an update via channels get-message",
{ timeout: 60000 },
async () => {
setupTestFailureHandler(
"should return the latest version after an update via channels get-message",
);
// Publish, update, then verify get-message returns the updated payload
const updateChannel = getMutableChannelName("msg-get-after-update");
const serial = await publishAndGetSerial(updateChannel, "original");
const updateResult = await runCommand(
[
"channels",
"update",
updateChannel,
serial,
"edited-text",
"--json",
],
{
env: { ABLY_API_KEY: E2E_API_KEY || "" },
timeoutMs: 30000,
},
);
expect(updateResult.exitCode).toBe(0);
// Retry get-message — update is eventually consistent
let latestMessage: Record<string, unknown> | undefined;
for (let attempt = 0; attempt < 10; attempt++) {
const getResult = await runCommand(
["channels", "get-message", updateChannel, serial, "--json"],
{
env: { ABLY_API_KEY: E2E_API_KEY || "" },
timeoutMs: 30000,
},
);
if (getResult.exitCode === 0) {
const records = parseNdjsonLines(getResult.stdout);
const parsed =
records.find((r) => r.type === "result") ?? records[0];
latestMessage = parsed.message as
| Record<string, unknown>
| undefined;
if (latestMessage?.data === "edited-text") break;
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}
expect(latestMessage).toBeDefined();
expect(latestMessage!.data).toBe("edited-text");
// The action must reflect that this is an update, not the original create
expect(latestMessage!.action).toBe("message.update");
// The version block must be populated and differ from the message serial
expect(latestMessage!.version).toBeDefined();
const version = latestMessage!.version as Record<string, unknown>;
expect(version.serial).toBeDefined();
expect(version.serial).not.toBe(serial);
},
);
it(
"should render human-readable output without --json",
{ timeout: 60000 },
async () => {
setupTestFailureHandler(
"should render human-readable output without --json",
);
const humanChannel = getMutableChannelName("msg-get-human");
const serial = await publishAndGetSerial(humanChannel, "human-text");
const result = await runCommand(
["channels", "get-message", humanChannel, serial],
{
env: { ABLY_API_KEY: E2E_API_KEY || "" },
timeoutMs: 30000,
},
);
expect(result.exitCode).toBe(0);
// Field labels rendered by formatMessagesOutput must appear
expect(result.stdout).toContain("Channel");
expect(result.stdout).toContain("Serial");
expect(result.stdout).toContain(serial);
expect(result.stdout).toContain("Data");
expect(result.stdout).toContain("human-text");
},
);
it(
"should fail with a non-zero exit code for an unknown serial",
{ timeout: 60000 },
async () => {
setupTestFailureHandler(
"should fail with a non-zero exit code for an unknown serial",
);
const result = await runCommand(
[
"channels",
"get-message",
channelName,
"0000000000-000@deadbeef:000",
"--json",
],
{
env: { ABLY_API_KEY: E2E_API_KEY || "" },
timeoutMs: 30000,
},
);
expect(result.exitCode).not.toBe(0);
const records = parseNdjsonLines(result.stdout);
const errorRecord = records.find((r) => r.type === "error");
expect(errorRecord).toBeDefined();
expect(errorRecord!.success).toBe(false);
},
);
it(
"should delete a message via channels delete",
{ timeout: 60000 },
async () => {
setupTestFailureHandler("should delete a message via channels delete");
// Publish a fresh message to delete (so we don't conflict with update/append tests)
const deleteChannel = getMutableChannelName("msg-delete");
const serial = await publishAndGetSerial(deleteChannel, "to-delete");
const result = await runCommand(
["channels", "delete", deleteChannel, serial, "--json"],
{
env: { ABLY_API_KEY: E2E_API_KEY || "" },
timeoutMs: 30000,
},
);
expect(result.exitCode).toBe(0);
const records = parseNdjsonLines(result.stdout);
const parsed = records.find((r) => r.type === "result") ?? records[0];
expect(parsed.success).toBe(true);
expect(parsed.message).toBeDefined();
},
);
},
);