Skip to content
File

Blob: src/workerd/api/tests/delete-all-deletes-alarm-test.js

javascript74 lines
1// Copyright (c) 2026 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4// Tests that the delete_all_deletes_alarm compat flag correctly controls whether
5// deleteAll() also deletes alarms. This file is used by two test services: one
6// with the flag enabled and one with it disabled. The EXPECT_ALARM_DELETED binding
7// tells this code which behavior to expect.
8 
9import * as assert from 'node:assert';
10 
11export class DurableObjectExample {
12 constructor(state) {
13 this.state = state;
14 }
15 
16 async fetch(request) {
17 const url = new URL(request.url);
18 const expectDeleted = url.searchParams.get('expectDeleted') === 'true';
19 
20 // Set an alarm for 10 minutes from now.
21 const alarmTime = Date.now() + 10 * 60 * 1000;
22 await this.state.storage.setAlarm(alarmTime);
23 assert.equal(await this.state.storage.getAlarm(), alarmTime);
24 
25 // Also put some KV data so deleteAll has something to delete.
26 await this.state.storage.put('key', 'value');
27 
28 // Call deleteAll().
29 await this.state.storage.deleteAll();
30 
31 // KV data should always be deleted.
32 assert.equal(await this.state.storage.get('key'), undefined);
33 
34 const alarmAfter = await this.state.storage.getAlarm();
35 
36 if (expectDeleted) {
37 // With the compat flag enabled, the alarm should be deleted.
38 assert.equal(
39 alarmAfter,
40 null,
41 `Expected alarm to be null after deleteAll(), got ${alarmAfter}`
42 );
43 } else {
44 // Without the compat flag, the alarm should be preserved.
45 assert.equal(
46 alarmAfter,
47 alarmTime,
48 `Expected alarm to be preserved after deleteAll(), got ${alarmAfter}`
49 );
50 // Clean up the alarm so it doesn't fire during the test.
51 await this.state.storage.deleteAlarm();
52 }
53 
54 return new Response('OK');
55 }
56 
57 async alarm() {
58 // Should not be invoked during the test.
59 throw new Error('alarm handler unexpectedly invoked');
60 }
61}
62 
63export const test = {
64 async test(ctrl, env, ctx) {
65 let id = env.ns.idFromName('A');
66 let obj = env.ns.get(id);
67 let res = await obj.fetch(
68 `http://foo/test?expectDeleted=${env.EXPECT_ALARM_DELETED}`
69 );
70 let text = await res.text();
71 assert.equal(text, 'OK');
72 },
73};