forked from veridatum-labs/earnproof-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.service.spec.ts
More file actions
661 lines (550 loc) · 23.4 KB
/
Copy pathsession.service.spec.ts
File metadata and controls
661 lines (550 loc) · 23.4 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
import { UnauthorizedException } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { SessionService } from "./session.service";
import { FixedClock } from "../../test/time/fixed-clock";
// ---------------------------------------------------------------------------
// Minimal Prisma mock — every method is a jest.fn() returning sensible defaults
// ---------------------------------------------------------------------------
function makePrismaMock() {
return {
authSession: {
create: jest.fn(),
findUnique: jest.fn(),
update: jest.fn(),
updateMany: jest.fn(),
deleteMany: jest.fn(),
},
$transaction: jest.fn(),
};
}
const config = {
getOrThrow: () => "test_secret_xyz",
} as unknown as ConfigService;
const validToken = `${"A".repeat(16)}.${"a".repeat(64)}`;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Build a valid-looking active session row. */
function activeSession(overrides: Partial<{
id: string;
userId: string;
tokenHash: string;
revokedAt: Date | null;
expiresAt: Date;
}> = {}) {
return {
id: overrides.id ?? "sess_1",
userId: overrides.userId ?? "user_1",
tokenHash: overrides.tokenHash ?? "hash",
revokedAt: overrides.revokedAt ?? null,
expiresAt: overrides.expiresAt ?? new Date(Date.now() + 60_000),
};
}
// ---------------------------------------------------------------------------
// SessionService.create
// ---------------------------------------------------------------------------
describe("SessionService.create", () => {
it("returns an opaque token, sessionId and expiresAt", async () => {
const prisma = makePrismaMock();
prisma.authSession.create.mockResolvedValue({});
const svc = new SessionService(prisma as never, config);
const result = await svc.create({
id: "user_1",
walletAddress: "G".padEnd(56, "A"),
walletHash: "sha256:abc",
role: "WORKER",
});
expect(result.token).toMatch(/^[A-Za-z0-9_-]+\.[a-f0-9]{64}$/);
expect(result.sessionId).toBeTruthy();
expect(result.expiresAt.getTime()).toBeGreaterThan(Date.now());
});
it("stores only the hash — the raw token is never written to DB", async () => {
const prisma = makePrismaMock();
prisma.authSession.create.mockResolvedValue({});
const svc = new SessionService(prisma as never, config);
const { token } = await svc.create({
id: "user_1",
walletAddress: "G".padEnd(56, "A"),
walletHash: "sha256:abc",
role: "WORKER",
});
const [call] = prisma.authSession.create.mock.calls;
const storedData = call[0].data as Record<string, unknown>;
// The raw token must not appear anywhere in the stored data.
expect(JSON.stringify(storedData)).not.toContain(token);
// tokenHash must be present and must differ from the token.
expect(storedData.tokenHash).toBeTruthy();
expect(storedData.tokenHash).not.toEqual(token);
});
it("uses the supplied ttl", async () => {
const prisma = makePrismaMock();
prisma.authSession.create.mockResolvedValue({});
const svc = new SessionService(prisma as never, config);
const before = Date.now();
const { expiresAt } = await svc.create(
{ id: "u", walletAddress: "G".padEnd(56, "A"), walletHash: "h", role: "WORKER" },
3600,
);
const diff = expiresAt.getTime() - before;
expect(diff).toBeGreaterThanOrEqual(3600 * 1000 - 50);
expect(diff).toBeLessThanOrEqual(3600 * 1000 + 500);
});
});
// ---------------------------------------------------------------------------
// SessionService.validate
// ---------------------------------------------------------------------------
describe("SessionService.validate", () => {
it("returns sessionId and userId for a valid token", async () => {
const prisma = makePrismaMock();
const sess = activeSession();
prisma.authSession.findUnique.mockResolvedValue(sess);
prisma.authSession.update.mockResolvedValue({});
const svc = new SessionService(prisma as never, config);
// Create a real token so the format check passes.
prisma.authSession.create.mockResolvedValue({});
const { token } = await svc.create({
id: "user_1",
walletAddress: "G".padEnd(56, "A"),
walletHash: "sha256:abc",
role: "WORKER",
});
// Re-wire findUnique to return our fake session for whatever hash is queried.
prisma.authSession.findUnique.mockResolvedValue(sess);
const result = await svc.validate(token);
expect(result.userId).toBe("user_1");
expect(result.sessionId).toBe("sess_1");
});
it("rejects a malformed token (no dot separator)", async () => {
const prisma = makePrismaMock();
const svc = new SessionService(prisma as never, config);
await expect(svc.validate("nodottoken")).rejects.toThrow(
UnauthorizedException,
);
expect(prisma.authSession.findUnique).not.toHaveBeenCalled();
});
it("rejects an empty string token", async () => {
const prisma = makePrismaMock();
const svc = new SessionService(prisma as never, config);
await expect(svc.validate("")).rejects.toThrow(UnauthorizedException);
});
it("rejects a token whose session row is missing", async () => {
const prisma = makePrismaMock();
prisma.authSession.findUnique.mockResolvedValue(null);
const svc = new SessionService(prisma as never, config);
await expect(svc.validate(validToken)).rejects.toThrow(
"Session not found",
);
});
it("rejects a revoked session", async () => {
const prisma = makePrismaMock();
prisma.authSession.findUnique.mockResolvedValue(
activeSession({ revokedAt: new Date() }),
);
const svc = new SessionService(prisma as never, config);
await expect(svc.validate(validToken)).rejects.toThrow(
"Session has been revoked",
);
});
it("rejects an expired session", async () => {
const prisma = makePrismaMock();
prisma.authSession.findUnique.mockResolvedValue(
activeSession({ expiresAt: new Date(Date.now() - 1000) }),
);
const svc = new SessionService(prisma as never, config);
await expect(svc.validate(validToken)).rejects.toThrow(
"Session has expired",
);
});
it("updates lastUsedAt on successful validation", async () => {
const prisma = makePrismaMock();
const sess = activeSession();
prisma.authSession.findUnique.mockResolvedValue(sess);
prisma.authSession.update.mockResolvedValue({});
prisma.authSession.create.mockResolvedValue({});
const svc = new SessionService(prisma as never, config);
const { token } = await svc.create({
id: "user_1",
walletAddress: "G".padEnd(56, "A"),
walletHash: "sha256:abc",
role: "WORKER",
});
prisma.authSession.findUnique.mockResolvedValue(sess);
await svc.validate(token);
// Give the fire-and-forget update a tick to fire.
await new Promise((r) => setImmediate(r));
expect(prisma.authSession.update).toHaveBeenCalledWith(
expect.objectContaining({
where: { id: sess.id },
data: expect.objectContaining({ lastUsedAt: expect.any(Date) }),
}),
);
});
});
// ---------------------------------------------------------------------------
// SessionService.tryIdentify
// ---------------------------------------------------------------------------
describe("SessionService.tryIdentify", () => {
it("returns a session identity for a live persisted session", async () => {
const prisma = makePrismaMock();
prisma.authSession.findUnique.mockResolvedValue(
activeSession({ id: "sess_live", userId: "user_live" }),
);
const svc = new SessionService(prisma as never, config);
await expect(svc.tryIdentify(validToken)).resolves.toEqual({
sessionId: "sess_live",
userId: "user_live",
});
});
it("returns null for a malformed token without querying storage", async () => {
const prisma = makePrismaMock();
const svc = new SessionService(prisma as never, config);
await expect(svc.tryIdentify("not-a-session")).resolves.toBeNull();
expect(prisma.authSession.findUnique).not.toHaveBeenCalled();
});
it("returns null for missing, revoked, or expired sessions", async () => {
const prisma = makePrismaMock();
const svc = new SessionService(prisma as never, config);
prisma.authSession.findUnique.mockResolvedValueOnce(null);
await expect(svc.tryIdentify(validToken)).resolves.toBeNull();
prisma.authSession.findUnique.mockResolvedValueOnce(
activeSession({ revokedAt: new Date() }),
);
await expect(svc.tryIdentify(validToken)).resolves.toBeNull();
prisma.authSession.findUnique.mockResolvedValueOnce(
activeSession({ expiresAt: new Date(Date.now() - 1000) }),
);
await expect(svc.tryIdentify(validToken)).resolves.toBeNull();
});
it("returns null if session storage is temporarily unavailable", async () => {
const prisma = makePrismaMock();
prisma.authSession.findUnique.mockRejectedValue(new Error("database down"));
const svc = new SessionService(prisma as never, config);
await expect(svc.tryIdentify(validToken)).resolves.toBeNull();
});
it("does not update lastUsedAt", async () => {
const prisma = makePrismaMock();
prisma.authSession.findUnique.mockResolvedValue(activeSession());
const svc = new SessionService(prisma as never, config);
await svc.tryIdentify(validToken);
expect(prisma.authSession.update).not.toHaveBeenCalled();
});
});
// ---------------------------------------------------------------------------
// SessionService.revoke
// ---------------------------------------------------------------------------
describe("SessionService.revoke", () => {
it("sets revokedAt on the targeted session", async () => {
const prisma = makePrismaMock();
prisma.authSession.updateMany.mockResolvedValue({ count: 1 });
const svc = new SessionService(prisma as never, config);
await svc.revoke("sess_1");
expect(prisma.authSession.updateMany).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({ id: "sess_1", revokedAt: null }),
data: expect.objectContaining({ revokedAt: expect.any(Date) }),
}),
);
});
it("is idempotent — revoking twice does not throw", async () => {
const prisma = makePrismaMock();
// Second call matches 0 rows — still resolves cleanly.
prisma.authSession.updateMany
.mockResolvedValueOnce({ count: 1 })
.mockResolvedValueOnce({ count: 0 });
const svc = new SessionService(prisma as never, config);
await expect(svc.revoke("sess_1")).resolves.toBeUndefined();
await expect(svc.revoke("sess_1")).resolves.toBeUndefined();
});
});
// ---------------------------------------------------------------------------
// SessionService.rotate
// ---------------------------------------------------------------------------
describe("SessionService.rotate", () => {
function runTransactions(prisma: ReturnType<typeof makePrismaMock>) {
prisma.$transaction.mockImplementation(
async (callback: (transaction: typeof prisma) => Promise<unknown>) =>
callback(prisma),
);
}
it("creates a new session and revokes the old one atomically", async () => {
const prisma = makePrismaMock();
prisma.authSession.findUnique.mockResolvedValue(
activeSession({ id: "old_sess" }),
);
runTransactions(prisma);
prisma.authSession.create.mockResolvedValue({});
prisma.authSession.updateMany.mockResolvedValue({ count: 1 });
const svc = new SessionService(prisma as never, config);
const result = await svc.rotate("old_sess", {
id: "user_1",
walletAddress: "G".padEnd(56, "A"),
walletHash: "sha256:abc",
role: "WORKER",
});
expect(result.token).toMatch(/^[A-Za-z0-9_-]+\.[a-f0-9]{64}$/);
expect(result.sessionId).not.toBe("old_sess");
expect(result.expiresAt.getTime()).toBeGreaterThan(Date.now());
expect(prisma.$transaction).toHaveBeenCalledTimes(1);
expect(prisma.authSession.create).toHaveBeenCalledTimes(1);
expect(prisma.authSession.updateMany).toHaveBeenCalledWith(
expect.objectContaining({
where: { id: "old_sess", userId: "user_1", revokedAt: null },
data: expect.objectContaining({ revokedAt: expect.any(Date) }),
}),
);
});
it("rejects rotation of an already-revoked session", async () => {
const prisma = makePrismaMock();
prisma.authSession.findUnique.mockResolvedValue(
activeSession({ id: "old_sess", revokedAt: new Date() }),
);
runTransactions(prisma);
const svc = new SessionService(prisma as never, config);
await expect(
svc.rotate("old_sess", {
id: "user_1",
walletAddress: "G".padEnd(56, "A"),
walletHash: "sha256:abc",
role: "WORKER",
}),
).rejects.toThrow("Cannot rotate an already-revoked session");
});
it("rejects rotation of a missing session", async () => {
const prisma = makePrismaMock();
prisma.authSession.findUnique.mockResolvedValue(null);
runTransactions(prisma);
const svc = new SessionService(prisma as never, config);
await expect(
svc.rotate("ghost_sess", {
id: "user_1",
walletAddress: "G".padEnd(56, "A"),
walletHash: "sha256:abc",
role: "WORKER",
}),
).rejects.toThrow("Session not found");
});
it("issues a token that is different from the original", async () => {
const prisma = makePrismaMock();
prisma.authSession.create.mockResolvedValue({});
prisma.authSession.findUnique.mockResolvedValue(
activeSession({ id: "old_sess" }),
);
runTransactions(prisma);
prisma.authSession.updateMany.mockResolvedValue({ count: 1 });
const svc = new SessionService(prisma as never, config);
const originalToken = "old_sess.aaaa".padEnd(67, "0");
const rotated = await svc.rotate("old_sess", {
id: "user_1",
walletAddress: "G".padEnd(56, "A"),
walletHash: "sha256:abc",
role: "WORKER",
});
expect(rotated.token).not.toBe(originalToken);
});
it("allows only one concurrent rotation of the same session", async () => {
const prisma = makePrismaMock();
prisma.authSession.findUnique.mockResolvedValue(
activeSession({ id: "old_sess" }),
);
prisma.authSession.create.mockResolvedValue({});
prisma.authSession.updateMany
.mockResolvedValueOnce({ count: 1 })
.mockResolvedValueOnce({ count: 0 });
runTransactions(prisma);
const svc = new SessionService(prisma as never, config);
const user = {
id: "user_1",
walletAddress: "G".padEnd(56, "A"),
walletHash: "sha256:abc",
role: "WORKER",
};
const results = await Promise.allSettled([
svc.rotate("old_sess", user),
svc.rotate("old_sess", user),
]);
expect(results.filter(({ status }) => status === "fulfilled")).toHaveLength(
1,
);
expect(results.filter(({ status }) => status === "rejected")).toHaveLength(
1,
);
});
});
// ---------------------------------------------------------------------------
// SessionService.revokeAll
// ---------------------------------------------------------------------------
describe("SessionService.revokeAll", () => {
it("revokes all active sessions for a user", async () => {
const prisma = makePrismaMock();
prisma.authSession.updateMany.mockResolvedValue({ count: 3 });
const svc = new SessionService(prisma as never, config);
await svc.revokeAll("user_1");
expect(prisma.authSession.updateMany).toHaveBeenCalledWith({
where: { userId: "user_1", revokedAt: null },
data: { revokedAt: expect.any(Date) },
});
});
});
// ---------------------------------------------------------------------------
// SessionService.deleteExpired (retention/cleanup)
// ---------------------------------------------------------------------------
describe("SessionService.deleteExpired", () => {
it("deletes rows with expiresAt before the cutoff and returns count", async () => {
const prisma = makePrismaMock();
prisma.authSession.deleteMany.mockResolvedValue({ count: 7 });
const svc = new SessionService(prisma as never, config);
const count = await svc.deleteExpired(new Date("2030-01-01"));
expect(count).toBe(7);
expect(prisma.authSession.deleteMany).toHaveBeenCalledWith({
where: { expiresAt: { lt: new Date("2030-01-01") } },
});
});
it("defaults cutoff to now when no argument is provided", async () => {
const prisma = makePrismaMock();
prisma.authSession.deleteMany.mockResolvedValue({ count: 0 });
const svc = new SessionService(prisma as never, config);
const before = new Date();
await svc.deleteExpired();
const call = prisma.authSession.deleteMany.mock.calls[0][0] as {
where: { expiresAt: { lt: Date } };
};
const cutoff = call.where.expiresAt.lt;
expect(cutoff.getTime()).toBeGreaterThanOrEqual(before.getTime() - 10);
expect(cutoff.getTime()).toBeLessThanOrEqual(Date.now() + 10);
});
});
// ---------------------------------------------------------------------------
// Concurrent revocation — simulates a race between two logout requests
// ---------------------------------------------------------------------------
describe("SessionService concurrent revocation", () => {
it("handles two simultaneous revocations without throwing", async () => {
const prisma = makePrismaMock();
// First call revokes (count: 1), second is a no-op (count: 0).
prisma.authSession.updateMany
.mockResolvedValueOnce({ count: 1 })
.mockResolvedValueOnce({ count: 0 });
const svc = new SessionService(prisma as never, config);
const [r1, r2] = await Promise.allSettled([
svc.revoke("sess_1"),
svc.revoke("sess_1"),
]);
expect(r1.status).toBe("fulfilled");
expect(r2.status).toBe("fulfilled");
});
});
// ---------------------------------------------------------------------------
// Clock-skew and expiry-boundary coverage (earnproof-backend#66)
//
// Uses FixedClock so the exact-before / exact-at / exact-after boundary is
// deterministic — no real sleeps, no timing flakiness.
// ---------------------------------------------------------------------------
describe("SessionService expiry boundary (deterministic clock)", () => {
const TTL_SECONDS = 3600;
it("accepts a session strictly before its expiry instant", async () => {
const clock = new FixedClock("2030-01-01T00:00:00.000Z");
const prisma = makePrismaMock();
prisma.authSession.create.mockResolvedValue({});
const svc = new SessionService(prisma as never, config, clock);
const { token, expiresAt } = await svc.create(
{ id: "u", walletAddress: "G".padEnd(56, "A"), walletHash: "h", role: "WORKER" },
TTL_SECONDS,
);
// One millisecond before expiry: still valid.
clock.set(expiresAt.getTime() - 1);
prisma.authSession.findUnique.mockResolvedValue(
activeSession({ expiresAt }),
);
prisma.authSession.update.mockResolvedValue({});
await expect(svc.validate(token)).resolves.toEqual(
expect.objectContaining({ userId: "user_1" }),
);
});
it("rejects a session exactly at its expiry instant (inclusive boundary)", async () => {
const clock = new FixedClock("2030-01-01T00:00:00.000Z");
const prisma = makePrismaMock();
prisma.authSession.create.mockResolvedValue({});
const svc = new SessionService(prisma as never, config, clock);
const { token, expiresAt } = await svc.create(
{ id: "u", walletAddress: "G".padEnd(56, "A"), walletHash: "h", role: "WORKER" },
TTL_SECONDS,
);
// Exactly at expiresAt: expiresAt <= now, so this must be rejected —
// documents the boundary as inclusive (session.service.ts's own
// `session.expiresAt <= this.clock.now()` check).
clock.set(expiresAt.getTime());
prisma.authSession.findUnique.mockResolvedValue(
activeSession({ expiresAt }),
);
await expect(svc.validate(token)).rejects.toThrow("Session has expired");
});
it("rejects a session one millisecond after its expiry instant", async () => {
const clock = new FixedClock("2030-01-01T00:00:00.000Z");
const prisma = makePrismaMock();
prisma.authSession.create.mockResolvedValue({});
const svc = new SessionService(prisma as never, config, clock);
const { token, expiresAt } = await svc.create(
{ id: "u", walletAddress: "G".padEnd(56, "A"), walletHash: "h", role: "WORKER" },
TTL_SECONDS,
);
clock.set(expiresAt.getTime() + 1);
prisma.authSession.findUnique.mockResolvedValue(
activeSession({ expiresAt }),
);
await expect(svc.validate(token)).rejects.toThrow("Session has expired");
});
it("survives forward clock skew without throwing outside validate's own expiry check", async () => {
// Forward skew: the clock jumps far ahead between create() and
// validate() (e.g. NTP correction on the server). The only effect
// should be that the session is (correctly) treated as expired — no
// exception other than the documented UnauthorizedException.
const clock = new FixedClock("2030-01-01T00:00:00.000Z");
const prisma = makePrismaMock();
prisma.authSession.create.mockResolvedValue({});
const svc = new SessionService(prisma as never, config, clock);
const { token, expiresAt } = await svc.create(
{ id: "u", walletAddress: "G".padEnd(56, "A"), walletHash: "h", role: "WORKER" },
TTL_SECONDS,
);
clock.advanceMs(365 * 24 * 60 * 60 * 1000); // +1 year
prisma.authSession.findUnique.mockResolvedValue(
activeSession({ expiresAt }),
);
await expect(svc.validate(token)).rejects.toThrow(UnauthorizedException);
});
it("survives backward clock skew (still resolves a well-formed expiresAt)", async () => {
// Backward skew: the clock jumps into the past. create() must still
// compute a well-formed expiresAt (ttl seconds after whatever "now"
// the clock reports), and a session created and validated entirely
// within that skewed window must still be treated as valid.
const clock = new FixedClock("2030-01-01T00:00:00.000Z");
clock.advanceMs(-365 * 24 * 60 * 60 * 1000); // -1 year, before create()
const prisma = makePrismaMock();
prisma.authSession.create.mockResolvedValue({});
const svc = new SessionService(prisma as never, config, clock);
const { token, expiresAt } = await svc.create(
{ id: "u", walletAddress: "G".padEnd(56, "A"), walletHash: "h", role: "WORKER" },
TTL_SECONDS,
);
expect(expiresAt.getTime()).toBe(clock.nowMs() + TTL_SECONDS * 1000);
prisma.authSession.findUnique.mockResolvedValue(
activeSession({ expiresAt }),
);
prisma.authSession.update.mockResolvedValue({});
await expect(svc.validate(token)).resolves.toEqual(
expect.objectContaining({ userId: "user_1" }),
);
});
it("deleteExpired's default cutoff tracks the injected clock, not the real system clock", async () => {
// Regression guard for the injectable-clock refactor itself: the
// default-parameter cutoff must come from `clock.now()`, not a bare
// `new Date()` that would silently ignore an injected FixedClock.
const clock = new FixedClock("2030-06-15T12:00:00.000Z");
const prisma = makePrismaMock();
prisma.authSession.deleteMany.mockResolvedValue({ count: 0 });
const svc = new SessionService(prisma as never, config, clock);
await svc.deleteExpired();
expect(prisma.authSession.deleteMany).toHaveBeenCalledWith({
where: { expiresAt: { lt: new Date("2030-06-15T12:00:00.000Z") } },
});
});
});