@yx333221c/dsh-question-pacing 0.1.0 → 0.1.1

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.
Files changed (2) hide show
  1. package/package.json +8 -3
  2. package/test/core.test.mjs +269 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yx333221c/dsh-question-pacing",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "description": "Make ask_user_question safe to use: a configurable countdown that returns pending instead of blocking, an asking-level control in the composer, and a prompt policy that publishes both.",
6
6
  "exports": {
@@ -17,6 +17,7 @@
17
17
  "cordis.patch.yml",
18
18
  "locale/*.json",
19
19
  "docs",
20
+ "test/*.test.mjs",
20
21
  "icon.svg"
21
22
  ],
22
23
  "scripts": {
@@ -28,11 +29,15 @@
28
29
  },
29
30
  "license": "MIT",
30
31
  "dsh": {
31
- "bundle": { "patch": "./cordis.patch.yml" },
32
+ "bundle": {
33
+ "patch": "./cordis.patch.yml"
34
+ },
32
35
  "client": {
33
36
  "platform": "web",
34
37
  "immediately": true,
35
- "inject": ["@deepseek-ai/dsh-client-ui-conversation"]
38
+ "inject": [
39
+ "@deepseek-ai/dsh-client-ui-conversation"
40
+ ]
36
41
  }
37
42
  }
38
43
  }
@@ -0,0 +1,269 @@
1
+ import assert from 'node:assert/strict'
2
+ import { test } from 'node:test'
3
+ import {
4
+ DEFAULT_TIER,
5
+ DEFAULT_WAIT_SECONDS,
6
+ MAX_WAIT_SECONDS,
7
+ MIN_WAIT_SECONDS,
8
+ STATE_VERSION,
9
+ TIERS,
10
+ WAIT_CHOICES,
11
+ applyPatch,
12
+ clampTier,
13
+ clampWaitSeconds,
14
+ hasOverride,
15
+ isEmptyScope,
16
+ isTier,
17
+ normalizeScope,
18
+ promptSection,
19
+ readState,
20
+ resolveSettings,
21
+ sessionKeyOf,
22
+ tierPolicy,
23
+ writeState,
24
+ } from '../core.js'
25
+
26
+ /** A state with one global value and one session override. */
27
+ function seeded() {
28
+ const base = readState({ version: STATE_VERSION, global: { tier: 'often', waitSeconds: 300 } })
29
+ return applyPatch(base, { scope: 'session', sessionId: 's1', tier: 'minimal' })
30
+ }
31
+
32
+ test('the five levels are stable ids in ascending order', () => {
33
+ assert.deepEqual(TIERS, ['minimal', 'rarely', 'balanced', 'often', 'mandatory'])
34
+ assert.equal(DEFAULT_TIER, 'balanced')
35
+ assert.ok(TIERS.includes(DEFAULT_TIER), 'the default level is one of the levels')
36
+ })
37
+
38
+ test('isTier accepts only known ids', () => {
39
+ for (const id of TIERS) assert.equal(isTier(id), true, id)
40
+ for (const value of ['BALANCED', 'loud', '', null, undefined, 3, {}]) assert.equal(isTier(value), false, String(value))
41
+ })
42
+
43
+ test('clampTier falls back rather than throwing', () => {
44
+ assert.equal(clampTier('often'), 'often')
45
+ assert.equal(clampTier('nope'), DEFAULT_TIER)
46
+ assert.equal(clampTier('nope', 'minimal'), 'minimal')
47
+ assert.equal(clampTier('nope', 'also-nope'), DEFAULT_TIER, 'an invalid fallback returns the default')
48
+ })
49
+
50
+ test('the countdown choices are all within the accepted range', () => {
51
+ for (const seconds of WAIT_CHOICES) {
52
+ assert.equal(clampWaitSeconds(seconds), seconds, String(seconds))
53
+ }
54
+ assert.ok(WAIT_CHOICES.includes(DEFAULT_WAIT_SECONDS))
55
+ })
56
+
57
+ test('clampWaitSeconds rounds, coerces numeric strings and clamps', () => {
58
+ assert.equal(clampWaitSeconds(42.4), 42)
59
+ assert.equal(clampWaitSeconds('90'), 90)
60
+ assert.equal(clampWaitSeconds(' 90 '), 90)
61
+ assert.equal(clampWaitSeconds(1), MIN_WAIT_SECONDS, 'below the floor is raised')
62
+ assert.equal(clampWaitSeconds(1e9), MAX_WAIT_SECONDS, 'above the ceiling is lowered')
63
+ assert.equal(clampWaitSeconds('abc'), DEFAULT_WAIT_SECONDS)
64
+ assert.equal(clampWaitSeconds(undefined), DEFAULT_WAIT_SECONDS)
65
+ assert.equal(clampWaitSeconds(null), DEFAULT_WAIT_SECONDS)
66
+ assert.equal(clampWaitSeconds(Number.NaN), DEFAULT_WAIT_SECONDS)
67
+ assert.equal(clampWaitSeconds(Number.POSITIVE_INFINITY), DEFAULT_WAIT_SECONDS)
68
+ })
69
+
70
+ test('clampWaitSeconds honours a usable fallback', () => {
71
+ assert.equal(clampWaitSeconds('abc', 60), 60)
72
+ assert.equal(clampWaitSeconds(undefined, '300'), 300)
73
+ assert.equal(clampWaitSeconds(undefined, 'junk'), DEFAULT_WAIT_SECONDS)
74
+ })
75
+
76
+ test('normalizeScope keeps only known knobs', () => {
77
+ assert.deepEqual(normalizeScope({ tier: 'often', waitSeconds: 60 }), { tier: 'often', waitSeconds: 60 })
78
+ assert.deepEqual(normalizeScope({ tier: 'often', extra: 1 }), { tier: 'often' })
79
+ assert.deepEqual(normalizeScope({ waitSeconds: '45' }), { waitSeconds: 45 })
80
+ assert.equal(normalizeScope({ tier: 'bogus' }), undefined, 'an unknown level is not stored')
81
+ assert.equal(normalizeScope({}), undefined)
82
+ assert.equal(normalizeScope(null), undefined)
83
+ assert.equal(normalizeScope([]), undefined)
84
+ assert.equal(normalizeScope('often'), undefined)
85
+ })
86
+
87
+ test('an empty scope is one that overrides nothing', () => {
88
+ assert.equal(isEmptyScope(undefined), true)
89
+ assert.equal(isEmptyScope({}), true)
90
+ assert.equal(isEmptyScope({ tier: 'often' }), false)
91
+ assert.equal(isEmptyScope({ waitSeconds: 60 }), false)
92
+ })
93
+
94
+ test('sessionKeyOf accepts ids and rejects everything else', () => {
95
+ assert.equal(sessionKeyOf('abc'), 'abc')
96
+ assert.equal(sessionKeyOf(' '), undefined)
97
+ assert.equal(sessionKeyOf(''), undefined)
98
+ assert.equal(sessionKeyOf(7), '7')
99
+ assert.equal(sessionKeyOf(undefined), undefined)
100
+ assert.equal(sessionKeyOf({}), undefined)
101
+ })
102
+
103
+ test('readState on nothing configured yields defaults', () => {
104
+ for (const input of [undefined, null, 0, 'text', [], { sessions: 'nope' }, { global: [] }]) {
105
+ const state = readState(input)
106
+ assert.equal(state.version, STATE_VERSION)
107
+ assert.deepEqual(state.global, {})
108
+ assert.equal(state.sessions.size, 0)
109
+ }
110
+ })
111
+
112
+ test('readState keeps usable sessions and drops empty ones', () => {
113
+ const state = readState({
114
+ version: STATE_VERSION,
115
+ sessions: {
116
+ a: { tier: 'often' },
117
+ b: { tier: 'nope' },
118
+ c: { waitSeconds: 30 },
119
+ },
120
+ })
121
+ assert.deepEqual([...state.sessions.keys()].sort(), ['a', 'c'])
122
+ assert.deepEqual(state.sessions.get('c'), { waitSeconds: 30 })
123
+ })
124
+
125
+ test('readState ignores an older version but still reads its data', () => {
126
+ const state = readState({ version: 1, global: { tier: 'minimal' }, sessions: { s: { tier: 'often' } } })
127
+ assert.equal(state.version, STATE_VERSION)
128
+ assert.deepEqual(state.global, { tier: 'minimal' })
129
+ assert.deepEqual(state.sessions.get('s'), { tier: 'often' })
130
+ })
131
+
132
+ test('writeState omits empty scopes and round-trips', () => {
133
+ const state = seeded()
134
+ const serialized = writeState(state)
135
+ assert.deepEqual(serialized.global, { tier: 'often', waitSeconds: 300 })
136
+ assert.deepEqual(serialized.sessions, { s1: { tier: 'minimal' } })
137
+
138
+ const restored = readState(serialized)
139
+ assert.deepEqual(restored.global, state.global)
140
+ assert.deepEqual(restored.sessions.get('s1'), state.sessions.get('s1'))
141
+ })
142
+
143
+ test('writeState writes no global key when nothing is configured', () => {
144
+ const serialized = writeState(readState(undefined))
145
+ assert.equal('global' in serialized, false)
146
+ assert.deepEqual(serialized.sessions, {})
147
+ })
148
+
149
+ test('resolveSettings layers session over global per knob', () => {
150
+ const state = seeded()
151
+ const merged = resolveSettings(state, 's1')
152
+ assert.equal(merged.tier, 'minimal', 'the session level wins')
153
+ assert.equal(merged.waitSeconds, 300, 'an unset countdown follows the global value')
154
+ assert.equal(merged.sessionTier, true)
155
+ assert.equal(merged.sessionWait, false)
156
+
157
+ const other = resolveSettings(state, 's2')
158
+ assert.equal(other.tier, 'often')
159
+ assert.equal(other.sessionTier, false)
160
+ })
161
+
162
+ test('resolveSettings falls back to defaults with nothing configured', () => {
163
+ const resolved = resolveSettings(readState(undefined), 'unknown')
164
+ assert.equal(resolved.tier, DEFAULT_TIER)
165
+ assert.equal(resolved.waitSeconds, DEFAULT_WAIT_SECONDS)
166
+ assert.equal(resolved.sessionTier, false)
167
+ assert.equal(resolved.sessionWait, false)
168
+ })
169
+
170
+ test('resolveSettings tolerates a missing session id', () => {
171
+ const state = seeded()
172
+ const resolved = resolveSettings(state, undefined)
173
+ assert.equal(resolved.tier, 'often', 'no session reads the global layer')
174
+ assert.equal(resolved.sessionTier, false)
175
+ })
176
+
177
+ test('hasOverride reports only real session overrides', () => {
178
+ const state = seeded()
179
+ assert.equal(hasOverride(state, 's1'), true)
180
+ assert.equal(hasOverride(state, 's2'), false)
181
+ assert.equal(hasOverride(state, undefined), false)
182
+ })
183
+
184
+ test('applyPatch sets a session override without touching other sessions', () => {
185
+ const state = applyPatch(seeded(), { scope: 'session', sessionId: 's2', waitSeconds: 60 })
186
+ assert.deepEqual(state.sessions.get('s1'), { tier: 'minimal' })
187
+ assert.deepEqual(state.sessions.get('s2'), { waitSeconds: 60 })
188
+ })
189
+
190
+ test('applyPatch leaves the knob it did not name alone', () => {
191
+ const state = applyPatch(seeded(), { scope: 'session', sessionId: 's1', waitSeconds: 30 })
192
+ assert.deepEqual(state.sessions.get('s1'), { tier: 'minimal', waitSeconds: 30 })
193
+ })
194
+
195
+ test('applyPatch with null clears one knob and keeps the other', () => {
196
+ const twice = applyPatch(seeded(), { scope: 'session', sessionId: 's1', waitSeconds: 30 })
197
+ const cleared = applyPatch(twice, { scope: 'session', sessionId: 's1', tier: null })
198
+ assert.deepEqual(cleared.sessions.get('s1'), { waitSeconds: 30 })
199
+ })
200
+
201
+ test('clearing every knob drops the session entry entirely', () => {
202
+ const state = applyPatch(seeded(), { scope: 'session', sessionId: 's1', tier: null })
203
+ assert.equal(state.sessions.has('s1'), false, 'the session falls back to the global layer')
204
+ assert.equal(resolveSettings(state, 's1').tier, 'often')
205
+ })
206
+
207
+ test('applyPatch on the global layer writes the defaults', () => {
208
+ const state = applyPatch(readState(undefined), { scope: 'global', tier: 'mandatory', waitSeconds: 60 })
209
+ assert.deepEqual(state.global, { tier: 'mandatory', waitSeconds: 60 })
210
+ assert.equal(resolveSettings(state, 'anything').tier, 'mandatory')
211
+ })
212
+
213
+ test('clearing the global layer removes both keys', () => {
214
+ const state = applyPatch(seeded(), { scope: 'global', tier: null, waitSeconds: null })
215
+ assert.deepEqual(state.global, {})
216
+ assert.deepEqual(writeState(state).global, undefined)
217
+ })
218
+
219
+ test('applyPatch ignores an unusable patch', () => {
220
+ const before = seeded()
221
+ assert.equal(applyPatch(before, null), before)
222
+ assert.equal(applyPatch(before, undefined), before)
223
+ assert.equal(applyPatch(before, 'nope'), before)
224
+ assert.equal(applyPatch(before, { scope: 'session', tier: 'often' }), before, 'a session patch needs a session')
225
+ const unchanged = applyPatch(before, { scope: 'session', sessionId: 's1', tier: 'bogus' })
226
+ assert.deepEqual(unchanged.sessions.get('s1'), { tier: 'minimal' }, 'an unknown level is not written')
227
+ })
228
+
229
+ test('applyPatch does not mutate the state it was given', () => {
230
+ const before = seeded()
231
+ const snapshot = JSON.stringify(writeState(before))
232
+ applyPatch(before, { scope: 'session', sessionId: 's1', tier: 'mandatory' })
233
+ assert.equal(JSON.stringify(writeState(before)), snapshot)
234
+ })
235
+
236
+ test('applyPatch clamps an out-of-range countdown', () => {
237
+ const state = applyPatch(readState(undefined), { scope: 'global', waitSeconds: 1 })
238
+ assert.equal(state.global.waitSeconds, MIN_WAIT_SECONDS)
239
+ })
240
+
241
+ test('every level has policy text that names it', () => {
242
+ for (const tier of TIERS) {
243
+ const text = tierPolicy(tier)
244
+ assert.ok(text.length > 60, `${tier} has a substantive policy`)
245
+ assert.match(text, /ask/i, `${tier} policy talks about asking`)
246
+ }
247
+ })
248
+
249
+ test('an unknown level falls back to the middle policy', () => {
250
+ assert.equal(tierPolicy('bogus'), tierPolicy(DEFAULT_TIER))
251
+ })
252
+
253
+ test('the prompt section states the countdown and the pending contract', () => {
254
+ const text = promptSection('balanced', 90)
255
+ assert.match(text, /ask_user_question/)
256
+ assert.match(text, /90s/, 'the effective countdown is published')
257
+ assert.match(text, /pending/)
258
+ assert.match(text, /ask_user_question is safe to call|never blocks/)
259
+ })
260
+
261
+ test('the prompt section embeds the level policy', () => {
262
+ const text = promptSection('mandatory', 120)
263
+ assert.ok(text.includes(tierPolicy('mandatory')))
264
+ })
265
+
266
+ test('the prompt section clamps a nonsense countdown instead of printing it', () => {
267
+ assert.match(promptSection('balanced', 'junk'), /120s/)
268
+ assert.match(promptSection('balanced', 1), /10s/)
269
+ })