@wopr-network/platform-core 1.35.0 → 1.35.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/billing/crypto/btc/settler.js +3 -2
- package/dist/billing/crypto/evm/eth-settler.js +3 -1
- package/dist/billing/crypto/unified-checkout.js +1 -1
- package/dist/email/handlebars-renderer.test.d.ts +1 -0
- package/dist/email/handlebars-renderer.test.js +174 -0
- package/dist/fleet/fleet-notification-listener.test.d.ts +1 -0
- package/dist/fleet/fleet-notification-listener.test.js +244 -0
- package/dist/fleet/init-fleet-updater.d.ts +1 -1
- package/dist/fleet/init-fleet-updater.js +19 -3
- package/dist/fleet/init-fleet-updater.test.d.ts +1 -0
- package/dist/fleet/init-fleet-updater.test.js +186 -0
- package/dist/trpc/admin-fleet-update-router.test.d.ts +1 -0
- package/dist/trpc/admin-fleet-update-router.test.js +164 -0
- package/package.json +1 -1
- package/src/billing/crypto/btc/settler.ts +3 -2
- package/src/billing/crypto/evm/eth-settler.ts +3 -1
- package/src/billing/crypto/unified-checkout.ts +1 -1
- package/src/email/handlebars-renderer.test.ts +209 -0
- package/src/fleet/fleet-notification-listener.test.ts +322 -0
- package/src/fleet/init-fleet-updater.test.ts +234 -0
- package/src/fleet/init-fleet-updater.ts +22 -4
- package/src/trpc/admin-fleet-update-router.test.ts +201 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import type { INotificationPreferencesRepository, NotificationPrefs } from "../email/notification-repository-types.js";
|
|
3
|
+
import type { NotificationService } from "../email/notification-service.js";
|
|
4
|
+
import type { BotFleetEvent } from "./fleet-event-emitter.js";
|
|
5
|
+
import { FleetEventEmitter } from "./fleet-event-emitter.js";
|
|
6
|
+
import { initFleetNotificationListener } from "./fleet-notification-listener.js";
|
|
7
|
+
|
|
8
|
+
vi.mock("../config/logger.js", () => ({
|
|
9
|
+
logger: {
|
|
10
|
+
info: vi.fn(),
|
|
11
|
+
error: vi.fn(),
|
|
12
|
+
warn: vi.fn(),
|
|
13
|
+
debug: vi.fn(),
|
|
14
|
+
},
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// Helpers
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
const DEBOUNCE_MS = 100;
|
|
22
|
+
|
|
23
|
+
function makePrefs(overrides: Partial<NotificationPrefs> = {}): NotificationPrefs {
|
|
24
|
+
return {
|
|
25
|
+
billing_low_balance: true,
|
|
26
|
+
billing_receipts: true,
|
|
27
|
+
billing_auto_topup: true,
|
|
28
|
+
agent_channel_disconnect: true,
|
|
29
|
+
agent_status_changes: false,
|
|
30
|
+
account_role_changes: true,
|
|
31
|
+
account_team_invites: true,
|
|
32
|
+
fleet_updates: true,
|
|
33
|
+
...overrides,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function makePrefsRepo(prefs: NotificationPrefs = makePrefs()): INotificationPreferencesRepository {
|
|
38
|
+
return {
|
|
39
|
+
get: vi.fn().mockResolvedValue(prefs),
|
|
40
|
+
update: vi.fn().mockResolvedValue(undefined),
|
|
41
|
+
} as unknown as INotificationPreferencesRepository;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function makeNotificationService(): NotificationService {
|
|
45
|
+
return {
|
|
46
|
+
notifyFleetUpdateComplete: vi.fn(),
|
|
47
|
+
notifyFleetUpdateAvailable: vi.fn(),
|
|
48
|
+
notifyLowBalance: vi.fn(),
|
|
49
|
+
} as unknown as NotificationService;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function botUpdated(tenantId: string, version = "v1.2.0"): BotFleetEvent {
|
|
53
|
+
return {
|
|
54
|
+
type: "bot.updated",
|
|
55
|
+
botId: `bot-${Math.random().toString(36).slice(2, 6)}`,
|
|
56
|
+
tenantId,
|
|
57
|
+
timestamp: new Date().toISOString(),
|
|
58
|
+
version,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function botUpdateFailed(tenantId: string, version = "v1.2.0"): BotFleetEvent {
|
|
63
|
+
return {
|
|
64
|
+
type: "bot.update_failed",
|
|
65
|
+
botId: `bot-${Math.random().toString(36).slice(2, 6)}`,
|
|
66
|
+
tenantId,
|
|
67
|
+
timestamp: new Date().toISOString(),
|
|
68
|
+
version,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
// Tests
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
describe("initFleetNotificationListener", () => {
|
|
77
|
+
let emitter: FleetEventEmitter;
|
|
78
|
+
|
|
79
|
+
beforeEach(() => {
|
|
80
|
+
vi.useFakeTimers();
|
|
81
|
+
emitter = new FleetEventEmitter();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
afterEach(() => {
|
|
85
|
+
vi.useRealTimers();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("ignores non-bot events (node events)", async () => {
|
|
89
|
+
const notificationService = makeNotificationService();
|
|
90
|
+
const preferences = makePrefsRepo();
|
|
91
|
+
|
|
92
|
+
initFleetNotificationListener({
|
|
93
|
+
eventEmitter: emitter,
|
|
94
|
+
notificationService,
|
|
95
|
+
preferences,
|
|
96
|
+
resolveEmail: vi.fn().mockResolvedValue("user@example.com"),
|
|
97
|
+
debounceMs: DEBOUNCE_MS,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
emitter.emit({
|
|
101
|
+
type: "node.provisioned",
|
|
102
|
+
nodeId: "node-1",
|
|
103
|
+
timestamp: new Date().toISOString(),
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
await vi.advanceTimersByTimeAsync(DEBOUNCE_MS + 50);
|
|
107
|
+
|
|
108
|
+
expect(notificationService.notifyFleetUpdateComplete).not.toHaveBeenCalled();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("ignores non-update bot events (bot.started, bot.stopped)", async () => {
|
|
112
|
+
const notificationService = makeNotificationService();
|
|
113
|
+
const preferences = makePrefsRepo();
|
|
114
|
+
|
|
115
|
+
initFleetNotificationListener({
|
|
116
|
+
eventEmitter: emitter,
|
|
117
|
+
notificationService,
|
|
118
|
+
preferences,
|
|
119
|
+
resolveEmail: vi.fn().mockResolvedValue("user@example.com"),
|
|
120
|
+
debounceMs: DEBOUNCE_MS,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
emitter.emit({
|
|
124
|
+
type: "bot.started",
|
|
125
|
+
botId: "bot-1",
|
|
126
|
+
tenantId: "t1",
|
|
127
|
+
timestamp: new Date().toISOString(),
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
emitter.emit({
|
|
131
|
+
type: "bot.stopped",
|
|
132
|
+
botId: "bot-2",
|
|
133
|
+
tenantId: "t1",
|
|
134
|
+
timestamp: new Date().toISOString(),
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
await vi.advanceTimersByTimeAsync(DEBOUNCE_MS + 50);
|
|
138
|
+
|
|
139
|
+
expect(notificationService.notifyFleetUpdateComplete).not.toHaveBeenCalled();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("aggregates multiple bot.updated events per tenant into one notification", async () => {
|
|
143
|
+
const notificationService = makeNotificationService();
|
|
144
|
+
const preferences = makePrefsRepo();
|
|
145
|
+
const resolveEmail = vi.fn().mockResolvedValue("owner@example.com");
|
|
146
|
+
|
|
147
|
+
initFleetNotificationListener({
|
|
148
|
+
eventEmitter: emitter,
|
|
149
|
+
notificationService,
|
|
150
|
+
preferences,
|
|
151
|
+
resolveEmail,
|
|
152
|
+
debounceMs: DEBOUNCE_MS,
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// Emit 3 successful updates for the same tenant
|
|
156
|
+
emitter.emit(botUpdated("t1", "v2.0.0"));
|
|
157
|
+
emitter.emit(botUpdated("t1", "v2.0.0"));
|
|
158
|
+
emitter.emit(botUpdated("t1", "v2.0.0"));
|
|
159
|
+
|
|
160
|
+
// No notification yet — debounce hasn't fired
|
|
161
|
+
expect(notificationService.notifyFleetUpdateComplete).not.toHaveBeenCalled();
|
|
162
|
+
|
|
163
|
+
await vi.advanceTimersByTimeAsync(DEBOUNCE_MS + 50);
|
|
164
|
+
|
|
165
|
+
expect(notificationService.notifyFleetUpdateComplete).toHaveBeenCalledOnce();
|
|
166
|
+
expect(notificationService.notifyFleetUpdateComplete).toHaveBeenCalledWith(
|
|
167
|
+
"t1",
|
|
168
|
+
"owner@example.com",
|
|
169
|
+
"v2.0.0",
|
|
170
|
+
3, // succeeded
|
|
171
|
+
0, // failed
|
|
172
|
+
);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("sends summary with correct succeeded/failed counts after debounce", async () => {
|
|
176
|
+
const notificationService = makeNotificationService();
|
|
177
|
+
const preferences = makePrefsRepo();
|
|
178
|
+
const resolveEmail = vi.fn().mockResolvedValue("owner@example.com");
|
|
179
|
+
|
|
180
|
+
initFleetNotificationListener({
|
|
181
|
+
eventEmitter: emitter,
|
|
182
|
+
notificationService,
|
|
183
|
+
preferences,
|
|
184
|
+
resolveEmail,
|
|
185
|
+
debounceMs: DEBOUNCE_MS,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
emitter.emit(botUpdated("t1", "v3.0.0"));
|
|
189
|
+
emitter.emit(botUpdated("t1", "v3.0.0"));
|
|
190
|
+
emitter.emit(botUpdateFailed("t1", "v3.0.0"));
|
|
191
|
+
|
|
192
|
+
await vi.advanceTimersByTimeAsync(DEBOUNCE_MS + 50);
|
|
193
|
+
|
|
194
|
+
expect(notificationService.notifyFleetUpdateComplete).toHaveBeenCalledWith(
|
|
195
|
+
"t1",
|
|
196
|
+
"owner@example.com",
|
|
197
|
+
"v3.0.0",
|
|
198
|
+
2, // succeeded
|
|
199
|
+
1, // failed
|
|
200
|
+
);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("updates version from subsequent events", async () => {
|
|
204
|
+
const notificationService = makeNotificationService();
|
|
205
|
+
const preferences = makePrefsRepo();
|
|
206
|
+
const resolveEmail = vi.fn().mockResolvedValue("owner@example.com");
|
|
207
|
+
|
|
208
|
+
initFleetNotificationListener({
|
|
209
|
+
eventEmitter: emitter,
|
|
210
|
+
notificationService,
|
|
211
|
+
preferences,
|
|
212
|
+
resolveEmail,
|
|
213
|
+
debounceMs: DEBOUNCE_MS,
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// First event has v1.0.0, subsequent event changes to v1.1.0
|
|
217
|
+
emitter.emit(botUpdated("t1", "v1.0.0"));
|
|
218
|
+
emitter.emit(botUpdated("t1", "v1.1.0"));
|
|
219
|
+
|
|
220
|
+
await vi.advanceTimersByTimeAsync(DEBOUNCE_MS + 50);
|
|
221
|
+
|
|
222
|
+
expect(notificationService.notifyFleetUpdateComplete).toHaveBeenCalledWith(
|
|
223
|
+
"t1",
|
|
224
|
+
"owner@example.com",
|
|
225
|
+
"v1.1.0", // updated to latest version
|
|
226
|
+
2,
|
|
227
|
+
0,
|
|
228
|
+
);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("checks fleet_updates preference — skips if disabled", async () => {
|
|
232
|
+
const notificationService = makeNotificationService();
|
|
233
|
+
const preferences = makePrefsRepo(makePrefs({ fleet_updates: false }));
|
|
234
|
+
const resolveEmail = vi.fn().mockResolvedValue("owner@example.com");
|
|
235
|
+
|
|
236
|
+
initFleetNotificationListener({
|
|
237
|
+
eventEmitter: emitter,
|
|
238
|
+
notificationService,
|
|
239
|
+
preferences,
|
|
240
|
+
resolveEmail,
|
|
241
|
+
debounceMs: DEBOUNCE_MS,
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
emitter.emit(botUpdated("t1"));
|
|
245
|
+
|
|
246
|
+
await vi.advanceTimersByTimeAsync(DEBOUNCE_MS + 50);
|
|
247
|
+
|
|
248
|
+
expect(preferences.get).toHaveBeenCalledWith("t1");
|
|
249
|
+
expect(notificationService.notifyFleetUpdateComplete).not.toHaveBeenCalled();
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("skips if email resolver returns null", async () => {
|
|
253
|
+
const notificationService = makeNotificationService();
|
|
254
|
+
const preferences = makePrefsRepo();
|
|
255
|
+
const resolveEmail = vi.fn().mockResolvedValue(null);
|
|
256
|
+
|
|
257
|
+
initFleetNotificationListener({
|
|
258
|
+
eventEmitter: emitter,
|
|
259
|
+
notificationService,
|
|
260
|
+
preferences,
|
|
261
|
+
resolveEmail,
|
|
262
|
+
debounceMs: DEBOUNCE_MS,
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
emitter.emit(botUpdated("t1"));
|
|
266
|
+
|
|
267
|
+
await vi.advanceTimersByTimeAsync(DEBOUNCE_MS + 50);
|
|
268
|
+
|
|
269
|
+
expect(resolveEmail).toHaveBeenCalledWith("t1");
|
|
270
|
+
expect(notificationService.notifyFleetUpdateComplete).not.toHaveBeenCalled();
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("async shutdown flushes pending notifications", async () => {
|
|
274
|
+
const notificationService = makeNotificationService();
|
|
275
|
+
const preferences = makePrefsRepo();
|
|
276
|
+
const resolveEmail = vi.fn().mockResolvedValue("owner@example.com");
|
|
277
|
+
|
|
278
|
+
const shutdown = initFleetNotificationListener({
|
|
279
|
+
eventEmitter: emitter,
|
|
280
|
+
notificationService,
|
|
281
|
+
preferences,
|
|
282
|
+
resolveEmail,
|
|
283
|
+
debounceMs: DEBOUNCE_MS,
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
emitter.emit(botUpdated("t1", "v5.0.0"));
|
|
287
|
+
emitter.emit(botUpdated("t2", "v5.0.0"));
|
|
288
|
+
|
|
289
|
+
// Don't advance timers — flush via shutdown instead
|
|
290
|
+
await shutdown();
|
|
291
|
+
|
|
292
|
+
// Both tenants should have been flushed
|
|
293
|
+
expect(notificationService.notifyFleetUpdateComplete).toHaveBeenCalledTimes(2);
|
|
294
|
+
|
|
295
|
+
const calls = vi.mocked(notificationService.notifyFleetUpdateComplete).mock.calls;
|
|
296
|
+
const tenantIds = calls.map((c) => c[0]);
|
|
297
|
+
expect(tenantIds).toContain("t1");
|
|
298
|
+
expect(tenantIds).toContain("t2");
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it("no further events after shutdown", async () => {
|
|
302
|
+
const notificationService = makeNotificationService();
|
|
303
|
+
const preferences = makePrefsRepo();
|
|
304
|
+
const resolveEmail = vi.fn().mockResolvedValue("owner@example.com");
|
|
305
|
+
|
|
306
|
+
const shutdown = initFleetNotificationListener({
|
|
307
|
+
eventEmitter: emitter,
|
|
308
|
+
notificationService,
|
|
309
|
+
preferences,
|
|
310
|
+
resolveEmail,
|
|
311
|
+
debounceMs: DEBOUNCE_MS,
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
await shutdown();
|
|
315
|
+
|
|
316
|
+
// Events after shutdown should not trigger anything
|
|
317
|
+
emitter.emit(botUpdated("t1"));
|
|
318
|
+
await vi.advanceTimersByTimeAsync(DEBOUNCE_MS + 50);
|
|
319
|
+
|
|
320
|
+
expect(notificationService.notifyFleetUpdateComplete).not.toHaveBeenCalled();
|
|
321
|
+
});
|
|
322
|
+
});
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests the profile-filtering + onManualTenantsSkipped logic used inside initFleetUpdater.
|
|
3
|
+
*
|
|
4
|
+
* We can't import initFleetUpdater directly because it constructs ImagePoller,
|
|
5
|
+
* ContainerUpdater, etc. which pull in heavy dependencies that cause hangs.
|
|
6
|
+
* Instead, we replicate the getUpdatableProfiles closure from initFleetUpdater
|
|
7
|
+
* and test it in isolation via a RolloutOrchestrator with mock deps.
|
|
8
|
+
*/
|
|
9
|
+
import { describe, expect, it, vi } from "vitest";
|
|
10
|
+
import type { IBotProfileRepository } from "./bot-profile-repository.js";
|
|
11
|
+
import { RolloutOrchestrator } from "./rollout-orchestrator.js";
|
|
12
|
+
import type { IRolloutStrategy } from "./rollout-strategy.js";
|
|
13
|
+
import type { ITenantUpdateConfigRepository } from "./tenant-update-config-repository.js";
|
|
14
|
+
import type { BotProfile } from "./types.js";
|
|
15
|
+
import type { ContainerUpdater } from "./updater.js";
|
|
16
|
+
import type { VolumeSnapshotManager } from "./volume-snapshot-manager.js";
|
|
17
|
+
|
|
18
|
+
vi.mock("../config/logger.js", () => ({
|
|
19
|
+
logger: {
|
|
20
|
+
info: vi.fn(),
|
|
21
|
+
error: vi.fn(),
|
|
22
|
+
warn: vi.fn(),
|
|
23
|
+
debug: vi.fn(),
|
|
24
|
+
},
|
|
25
|
+
}));
|
|
26
|
+
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Helpers
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
function makeProfileWithFields(fields: { id: string; tenantId: string; updatePolicy: "auto" | "manual" }): BotProfile {
|
|
32
|
+
return {
|
|
33
|
+
...fields,
|
|
34
|
+
image: "ghcr.io/org/img:latest",
|
|
35
|
+
} as unknown as BotProfile;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function makeProfileRepo(profiles: BotProfile[]): IBotProfileRepository {
|
|
39
|
+
return {
|
|
40
|
+
list: vi.fn().mockResolvedValue(profiles),
|
|
41
|
+
get: vi
|
|
42
|
+
.fn()
|
|
43
|
+
.mockImplementation((id: string) =>
|
|
44
|
+
Promise.resolve(profiles.find((p) => (p as unknown as { id: string }).id === id) ?? null),
|
|
45
|
+
),
|
|
46
|
+
save: vi.fn().mockResolvedValue(undefined),
|
|
47
|
+
delete: vi.fn().mockResolvedValue(false),
|
|
48
|
+
} as unknown as IBotProfileRepository;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function makeConfigRepo(
|
|
52
|
+
configs: Record<string, { mode: "auto" | "manual"; preferredHourUtc: number }> = {},
|
|
53
|
+
): ITenantUpdateConfigRepository {
|
|
54
|
+
return {
|
|
55
|
+
get: vi.fn().mockImplementation((tenantId: string) => {
|
|
56
|
+
const cfg = configs[tenantId];
|
|
57
|
+
return Promise.resolve(cfg ? { tenantId, ...cfg, updatedAt: Date.now() } : null);
|
|
58
|
+
}),
|
|
59
|
+
upsert: vi.fn().mockResolvedValue(undefined),
|
|
60
|
+
listAutoEnabled: vi.fn().mockResolvedValue([]),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function makeMockUpdater(): ContainerUpdater {
|
|
65
|
+
return {
|
|
66
|
+
updateBot: vi.fn().mockResolvedValue({
|
|
67
|
+
botId: "",
|
|
68
|
+
success: true,
|
|
69
|
+
previousImage: "",
|
|
70
|
+
newImage: "",
|
|
71
|
+
previousDigest: null,
|
|
72
|
+
newDigest: null,
|
|
73
|
+
rolledBack: false,
|
|
74
|
+
}),
|
|
75
|
+
} as unknown as ContainerUpdater;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function makeMockSnapshotManager(): VolumeSnapshotManager {
|
|
79
|
+
return {
|
|
80
|
+
snapshot: vi.fn(),
|
|
81
|
+
restore: vi.fn(),
|
|
82
|
+
delete: vi.fn(),
|
|
83
|
+
} as unknown as VolumeSnapshotManager;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function makeMockStrategy(): IRolloutStrategy {
|
|
87
|
+
return {
|
|
88
|
+
nextBatch: vi.fn().mockImplementation((remaining: BotProfile[]) => remaining),
|
|
89
|
+
pauseDuration: vi.fn().mockReturnValue(0),
|
|
90
|
+
onBotFailure: vi.fn().mockReturnValue("skip" as const),
|
|
91
|
+
maxRetries: vi.fn().mockReturnValue(0),
|
|
92
|
+
healthCheckTimeout: vi.fn().mockReturnValue(0),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Builds the same getUpdatableProfiles closure that initFleetUpdater creates,
|
|
98
|
+
* so we can test the filtering + callback logic without importing the heavy module.
|
|
99
|
+
*/
|
|
100
|
+
function buildGetUpdatableProfiles(
|
|
101
|
+
profileRepo: IBotProfileRepository,
|
|
102
|
+
configRepo: ITenantUpdateConfigRepository | undefined,
|
|
103
|
+
onManualTenantsSkipped: ((tenantIds: string[], imageTag: string) => void) | undefined,
|
|
104
|
+
imageTag = "v1.2.3",
|
|
105
|
+
): () => Promise<BotProfile[]> {
|
|
106
|
+
return async () => {
|
|
107
|
+
const profiles = await profileRepo.list();
|
|
108
|
+
|
|
109
|
+
const manualPolicyIds: string[] = [];
|
|
110
|
+
const nonManualPolicy = profiles.filter((p) => {
|
|
111
|
+
if (p.updatePolicy === "manual") {
|
|
112
|
+
manualPolicyIds.push(p.tenantId);
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
return true;
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
if (!configRepo) {
|
|
119
|
+
if (manualPolicyIds.length > 0 && onManualTenantsSkipped) {
|
|
120
|
+
onManualTenantsSkipped([...new Set(manualPolicyIds)], imageTag);
|
|
121
|
+
}
|
|
122
|
+
return nonManualPolicy;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const configManualIds: string[] = [];
|
|
126
|
+
const results = await Promise.all(
|
|
127
|
+
nonManualPolicy.map(async (p) => {
|
|
128
|
+
const tenantCfg = await configRepo.get(p.tenantId);
|
|
129
|
+
if (tenantCfg && tenantCfg.mode === "manual") {
|
|
130
|
+
configManualIds.push(p.tenantId);
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
return p;
|
|
134
|
+
}),
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
const allManualIds = [...manualPolicyIds, ...configManualIds];
|
|
138
|
+
if (allManualIds.length > 0 && onManualTenantsSkipped) {
|
|
139
|
+
onManualTenantsSkipped([...new Set(allManualIds)], imageTag);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return results.filter((p): p is BotProfile => p !== null);
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// ---------------------------------------------------------------------------
|
|
147
|
+
// Tests
|
|
148
|
+
// ---------------------------------------------------------------------------
|
|
149
|
+
|
|
150
|
+
describe("initFleetUpdater — onManualTenantsSkipped", () => {
|
|
151
|
+
it("callback fires with tenant IDs of manual-mode tenants (policy-based)", async () => {
|
|
152
|
+
const profiles = [
|
|
153
|
+
makeProfileWithFields({ id: "b1", tenantId: "t-manual", updatePolicy: "manual" }),
|
|
154
|
+
makeProfileWithFields({ id: "b2", tenantId: "t-auto", updatePolicy: "auto" }),
|
|
155
|
+
];
|
|
156
|
+
const profileRepo = makeProfileRepo(profiles);
|
|
157
|
+
const onManualTenantsSkipped = vi.fn();
|
|
158
|
+
|
|
159
|
+
const orchestrator = new RolloutOrchestrator({
|
|
160
|
+
updater: makeMockUpdater(),
|
|
161
|
+
snapshotManager: makeMockSnapshotManager(),
|
|
162
|
+
strategy: makeMockStrategy(),
|
|
163
|
+
getUpdatableProfiles: buildGetUpdatableProfiles(profileRepo, undefined, onManualTenantsSkipped),
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
await orchestrator.rollout();
|
|
167
|
+
|
|
168
|
+
expect(onManualTenantsSkipped).toHaveBeenCalledWith(["t-manual"], "v1.2.3");
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("callback deduplicates tenant IDs", async () => {
|
|
172
|
+
const profiles = [
|
|
173
|
+
makeProfileWithFields({ id: "b1", tenantId: "t-dup", updatePolicy: "manual" }),
|
|
174
|
+
makeProfileWithFields({ id: "b2", tenantId: "t-dup", updatePolicy: "manual" }),
|
|
175
|
+
makeProfileWithFields({ id: "b3", tenantId: "t-auto", updatePolicy: "auto" }),
|
|
176
|
+
];
|
|
177
|
+
const profileRepo = makeProfileRepo(profiles);
|
|
178
|
+
const onManualTenantsSkipped = vi.fn();
|
|
179
|
+
|
|
180
|
+
const orchestrator = new RolloutOrchestrator({
|
|
181
|
+
updater: makeMockUpdater(),
|
|
182
|
+
snapshotManager: makeMockSnapshotManager(),
|
|
183
|
+
strategy: makeMockStrategy(),
|
|
184
|
+
getUpdatableProfiles: buildGetUpdatableProfiles(profileRepo, undefined, onManualTenantsSkipped),
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
await orchestrator.rollout();
|
|
188
|
+
|
|
189
|
+
expect(onManualTenantsSkipped).toHaveBeenCalledWith(["t-dup"], "v1.2.3");
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("callback not called when no manual tenants exist", async () => {
|
|
193
|
+
const profiles = [
|
|
194
|
+
makeProfileWithFields({ id: "b1", tenantId: "t1", updatePolicy: "auto" }),
|
|
195
|
+
makeProfileWithFields({ id: "b2", tenantId: "t2", updatePolicy: "auto" }),
|
|
196
|
+
];
|
|
197
|
+
const profileRepo = makeProfileRepo(profiles);
|
|
198
|
+
const onManualTenantsSkipped = vi.fn();
|
|
199
|
+
|
|
200
|
+
const orchestrator = new RolloutOrchestrator({
|
|
201
|
+
updater: makeMockUpdater(),
|
|
202
|
+
snapshotManager: makeMockSnapshotManager(),
|
|
203
|
+
strategy: makeMockStrategy(),
|
|
204
|
+
getUpdatableProfiles: buildGetUpdatableProfiles(profileRepo, undefined, onManualTenantsSkipped),
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
await orchestrator.rollout();
|
|
208
|
+
|
|
209
|
+
expect(onManualTenantsSkipped).not.toHaveBeenCalled();
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("callback fires with config-repo manual tenants when configRepo is provided", async () => {
|
|
213
|
+
const profiles = [
|
|
214
|
+
makeProfileWithFields({ id: "b1", tenantId: "t-cfg-manual", updatePolicy: "auto" }),
|
|
215
|
+
makeProfileWithFields({ id: "b2", tenantId: "t-cfg-auto", updatePolicy: "auto" }),
|
|
216
|
+
];
|
|
217
|
+
const profileRepo = makeProfileRepo(profiles);
|
|
218
|
+
const configRepo = makeConfigRepo({
|
|
219
|
+
"t-cfg-manual": { mode: "manual", preferredHourUtc: 3 },
|
|
220
|
+
});
|
|
221
|
+
const onManualTenantsSkipped = vi.fn();
|
|
222
|
+
|
|
223
|
+
const orchestrator = new RolloutOrchestrator({
|
|
224
|
+
updater: makeMockUpdater(),
|
|
225
|
+
snapshotManager: makeMockSnapshotManager(),
|
|
226
|
+
strategy: makeMockStrategy(),
|
|
227
|
+
getUpdatableProfiles: buildGetUpdatableProfiles(profileRepo, configRepo, onManualTenantsSkipped),
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
await orchestrator.rollout();
|
|
231
|
+
|
|
232
|
+
expect(onManualTenantsSkipped).toHaveBeenCalledWith(["t-cfg-manual"], "v1.2.3");
|
|
233
|
+
});
|
|
234
|
+
});
|
|
@@ -40,7 +40,7 @@ export interface FleetUpdaterConfig {
|
|
|
40
40
|
/** Optional fleet event emitter. When provided, bot.updated / bot.update_failed events are emitted. */
|
|
41
41
|
eventEmitter?: FleetEventEmitter;
|
|
42
42
|
/** Called with manual-mode tenant IDs when a new image is available but they are excluded from rollout. */
|
|
43
|
-
onManualTenantsSkipped?: (tenantIds: string[]) => void;
|
|
43
|
+
onManualTenantsSkipped?: (tenantIds: string[], imageTag: string) => void;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
export interface FleetUpdaterHandle {
|
|
@@ -91,6 +91,11 @@ export function initFleetUpdater(
|
|
|
91
91
|
const snapshotManager = new VolumeSnapshotManager(docker, snapshotDir);
|
|
92
92
|
const strategy = createRolloutStrategy(strategyType, strategyOptions);
|
|
93
93
|
|
|
94
|
+
// Captured by the onUpdateAvailable handler and read by getUpdatableProfiles.
|
|
95
|
+
// Set before each orchestrator.rollout() call so the callback receives the
|
|
96
|
+
// image tag that triggered this rollout rather than a stale "latest" placeholder.
|
|
97
|
+
let currentImageTag = "latest";
|
|
98
|
+
|
|
94
99
|
const orchestrator = new RolloutOrchestrator({
|
|
95
100
|
updater,
|
|
96
101
|
snapshotManager,
|
|
@@ -110,7 +115,7 @@ export function initFleetUpdater(
|
|
|
110
115
|
|
|
111
116
|
if (!configRepo) {
|
|
112
117
|
if (manualPolicyIds.length > 0 && onManualTenantsSkipped) {
|
|
113
|
-
onManualTenantsSkipped([...new Set(manualPolicyIds)]);
|
|
118
|
+
onManualTenantsSkipped([...new Set(manualPolicyIds)], currentImageTag);
|
|
114
119
|
}
|
|
115
120
|
return nonManualPolicy;
|
|
116
121
|
}
|
|
@@ -131,7 +136,7 @@ export function initFleetUpdater(
|
|
|
131
136
|
|
|
132
137
|
const allManualIds = [...manualPolicyIds, ...configManualIds];
|
|
133
138
|
if (allManualIds.length > 0 && onManualTenantsSkipped) {
|
|
134
|
-
onManualTenantsSkipped([...new Set(allManualIds)]);
|
|
139
|
+
onManualTenantsSkipped([...new Set(allManualIds)], currentImageTag);
|
|
135
140
|
}
|
|
136
141
|
|
|
137
142
|
return results.filter((p) => p !== null);
|
|
@@ -169,11 +174,24 @@ export function initFleetUpdater(
|
|
|
169
174
|
// Wire the detection → orchestration pipeline.
|
|
170
175
|
// Any digest change triggers a fleet-wide rollout because the managed image
|
|
171
176
|
// is shared across all tenants — one new digest means all bots need updating.
|
|
172
|
-
poller.onUpdateAvailable = async (
|
|
177
|
+
poller.onUpdateAvailable = async (botId: string, _newDigest: string) => {
|
|
173
178
|
if (orchestrator.isRolling) {
|
|
174
179
|
logger.debug("Skipping update trigger — rollout already in progress");
|
|
175
180
|
return;
|
|
176
181
|
}
|
|
182
|
+
|
|
183
|
+
// Resolve the image tag from the bot that triggered the update so that
|
|
184
|
+
// onManualTenantsSkipped receives the real version instead of "latest".
|
|
185
|
+
try {
|
|
186
|
+
const triggeringProfile = await profileStore.get(botId);
|
|
187
|
+
if (triggeringProfile) {
|
|
188
|
+
const img = triggeringProfile.image;
|
|
189
|
+
currentImageTag = img.includes(":") ? (img.split(":").pop() ?? "latest") : "latest";
|
|
190
|
+
}
|
|
191
|
+
} catch {
|
|
192
|
+
// Best-effort — currentImageTag stays at previous value
|
|
193
|
+
}
|
|
194
|
+
|
|
177
195
|
logger.info("New image digest detected — starting fleet-wide rollout");
|
|
178
196
|
await orchestrator.rollout().catch((err) => {
|
|
179
197
|
logger.error("Rollout failed", { err });
|