@airalogy/aimd-recorder 1.14.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 (178) hide show
  1. package/README.md +240 -0
  2. package/README.zh-CN.md +224 -0
  3. package/dist/AimdCodeField-DC3DPvgc.js +172 -0
  4. package/dist/AimdDnaSequenceField-__jdnFS9.js +59466 -0
  5. package/dist/AimdEditor.vue_vue_type_style_index_0_lang-Bq79rKNw.js +28370 -0
  6. package/dist/AimdMarkdownField-98ImHGbc.js +66 -0
  7. package/dist/AimdMarkdownNoteField-CuUJZkgT.js +185 -0
  8. package/dist/_.contribution-IP0cDNhs.js +38 -0
  9. package/dist/_commonjsHelpers-CZXJ37nu.js +40 -0
  10. package/dist/abap-Bs8kvjsq.js +1398 -0
  11. package/dist/aimd-recorder.css +1 -0
  12. package/dist/apex-Dspwrl5m.js +326 -0
  13. package/dist/assets/css.worker-Dmf38jDD.js +79 -0
  14. package/dist/assets/editor.worker-CpuX8TSY.js +12 -0
  15. package/dist/assets/html.worker-DotaEs7v.js +456 -0
  16. package/dist/assets/json.worker-B4VHhH6h.js +44 -0
  17. package/dist/assets/ts.worker-sl17zBDo.js +51316 -0
  18. package/dist/azcli-k1BiOau1.js +68 -0
  19. package/dist/bat-BIP72KWc.js +100 -0
  20. package/dist/bicep-VhQeYhS3.js +102 -0
  21. package/dist/cameligo-DnuAXtHg.js +174 -0
  22. package/dist/clojure-BAuDPsal.js +761 -0
  23. package/dist/coffee-BV06qSnj.js +232 -0
  24. package/dist/components.js +3595 -0
  25. package/dist/composables.js +72 -0
  26. package/dist/cpp-Cz8Jeyrf.js +389 -0
  27. package/dist/csharp-F6hhlXFb.js +326 -0
  28. package/dist/csp-KzlEHCyI.js +53 -0
  29. package/dist/css-e4FKntZ3.js +187 -0
  30. package/dist/cssMode-uz9U6SUU.js +1571 -0
  31. package/dist/cypher-CpvDGKRr.js +263 -0
  32. package/dist/dart-DdHeFWR0.js +281 -0
  33. package/dist/dockerfile-aoNXDcio.js +130 -0
  34. package/dist/ecl-C4Q0DYuA.js +456 -0
  35. package/dist/editor.api-CgD9w0gG.js +77091 -0
  36. package/dist/editor.main-Du02abDl.js +33783 -0
  37. package/dist/elixir-C9X42b0T.js +569 -0
  38. package/dist/flow9-Bff42pfB.js +142 -0
  39. package/dist/freemarker2-DFLX4iQ-.js +989 -0
  40. package/dist/fsharp-C6vPAsHG.js +217 -0
  41. package/dist/go-DEr8zx9f.js +218 -0
  42. package/dist/graphql-BiNBBRFh.js +151 -0
  43. package/dist/handlebars-aiZbIuWY.js +419 -0
  44. package/dist/hcl-2T9lKuFg.js +183 -0
  45. package/dist/html-phzTMObN.js +308 -0
  46. package/dist/htmlMode-BF7wMi3N.js +1581 -0
  47. package/dist/index.js +89 -0
  48. package/dist/ini-CZGtssYM.js +71 -0
  49. package/dist/ini.contribution-DyWtiacL.js +8 -0
  50. package/dist/java-B260NeH3.js +232 -0
  51. package/dist/javascript-BKKOItMi.js +70 -0
  52. package/dist/jsonMode-CDYK4B5l.js +1996 -0
  53. package/dist/julia-kuNLM9c5.js +511 -0
  54. package/dist/kotlin-Bz6kAefA.js +253 -0
  55. package/dist/less-AZUtS89I.js +162 -0
  56. package/dist/lexon-BcmgnyRb.js +157 -0
  57. package/dist/liquid-B2eRz5SA.js +240 -0
  58. package/dist/lua-Dz1RbATg.js +162 -0
  59. package/dist/m3-CIMooLFn.js +210 -0
  60. package/dist/markdown-C1BsCK9P.js +229 -0
  61. package/dist/markdown.contribution-DYYk6zUU.js +7 -0
  62. package/dist/mdx-BfV1HDRG.js +165 -0
  63. package/dist/mips-BovsnfLQ.js +198 -0
  64. package/dist/monaco.contribution-C-bQhUTI.js +114 -0
  65. package/dist/monaco.contribution-CCVvl3Jx.js +72 -0
  66. package/dist/monaco.contribution-Col-f0XR.js +164 -0
  67. package/dist/monaco.contribution-b5xkgQ-s.js +112 -0
  68. package/dist/msdax-CiRB2ZB-.js +375 -0
  69. package/dist/mysql-BwdpPE19.js +878 -0
  70. package/dist/objective-c-D4nfHfso.js +183 -0
  71. package/dist/pascal--j5-PYtz.js +251 -0
  72. package/dist/pascaligo-C7TcJxNL.js +164 -0
  73. package/dist/perl-BzEWqUb5.js +626 -0
  74. package/dist/pgsql-B_KZ0mGX.js +851 -0
  75. package/dist/php-A19QHzQV.js +500 -0
  76. package/dist/pla-COeKY1hA.js +137 -0
  77. package/dist/postiats-BfMp7zJL.js +907 -0
  78. package/dist/powerquery-B0TS_5Gb.js +890 -0
  79. package/dist/powershell-5jSbKdYB.js +239 -0
  80. package/dist/protobuf-YgnOutn3.js +420 -0
  81. package/dist/pug-DLej6kn5.js +402 -0
  82. package/dist/python-CQrZx9mR.js +301 -0
  83. package/dist/python.contribution-CcKs2Ktm.js +8 -0
  84. package/dist/qsharp-AhjqtSHB.js +301 -0
  85. package/dist/r-D-F4P80w.js +243 -0
  86. package/dist/razor-DWmpHMcx.js +550 -0
  87. package/dist/redis-hwwIlMnV.js +302 -0
  88. package/dist/redshift-Bcm_kjEG.js +809 -0
  89. package/dist/restructuredtext-CrdzfPP-.js +174 -0
  90. package/dist/ruby-QC2UNm1s.js +511 -0
  91. package/dist/rust-5aqiQ3L5.js +343 -0
  92. package/dist/sb-B1g1ZfLJ.js +115 -0
  93. package/dist/scala-C2U4R5Gb.js +370 -0
  94. package/dist/scheme-DeFRbslr.js +108 -0
  95. package/dist/scss-RSTCiCmI.js +262 -0
  96. package/dist/shell-BpF-iNu8.js +221 -0
  97. package/dist/shell.contribution-B80NZ3iO.js +7 -0
  98. package/dist/solidity-C00r2mbQ.js +1367 -0
  99. package/dist/sophia-DVW5ZKDB.js +199 -0
  100. package/dist/sparql-B0nmpxeX.js +201 -0
  101. package/dist/sql-CJwKPERn.js +853 -0
  102. package/dist/sql.contribution-DQy36aE5.js +7 -0
  103. package/dist/st-DPlKG5DT.js +416 -0
  104. package/dist/swift-YZBAbVHV.js +309 -0
  105. package/dist/systemverilog-CDQSaH63.js +576 -0
  106. package/dist/tcl-Dlu8sp6Q.js +232 -0
  107. package/dist/tsMode-Bh-v12OL.js +887 -0
  108. package/dist/twig-C5TO7-kw.js +392 -0
  109. package/dist/typescript-c7y6IZVP.js +343 -0
  110. package/dist/typespec-BsmjDEu7.js +117 -0
  111. package/dist/useFieldRendering-BIqeE3qo.js +6083 -0
  112. package/dist/vb-ChHvIDrk.js +372 -0
  113. package/dist/wgsl-XEp1ju8c.js +439 -0
  114. package/dist/xml-MAEprTnF.js +95 -0
  115. package/dist/xml.contribution-XMOaon8i.js +27 -0
  116. package/dist/yaml-C1p8rXG7.js +206 -0
  117. package/dist/yaml.contribution-DRRVhJmJ.js +8 -0
  118. package/package.json +88 -0
  119. package/src/__tests__/client-assigner.test.ts +341 -0
  120. package/src/__tests__/code-types.test.ts +30 -0
  121. package/src/__tests__/markdown-field.test.ts +34 -0
  122. package/src/__tests__/markdown-note-field.test.ts +135 -0
  123. package/src/__tests__/quiz-recorder.test.ts +195 -0
  124. package/src/__tests__/recorder-editor-content-sync.test.ts +172 -0
  125. package/src/__tests__/recorder-editor.test.ts +143 -0
  126. package/src/__tests__/recorder-render.test.ts +57 -0
  127. package/src/__tests__/recorder-wysiwyg-field-host.test.ts +33 -0
  128. package/src/__tests__/step-field.test.ts +270 -0
  129. package/src/__tests__/type-plugins.test.ts +59 -0
  130. package/src/__tests__/var-field-render.test.ts +26 -0
  131. package/src/client-assigner.ts +269 -0
  132. package/src/code-types.ts +94 -0
  133. package/src/components/AimdCodeField.vue +228 -0
  134. package/src/components/AimdDnaSequenceField.vue +983 -0
  135. package/src/components/AimdMarkdownField.vue +168 -0
  136. package/src/components/AimdMarkdownNoteField.vue +457 -0
  137. package/src/components/AimdQuizRecorder.vue +792 -0
  138. package/src/components/AimdRecorder.vue +1959 -0
  139. package/src/components/AimdRecorderEditor.vue +2281 -0
  140. package/src/components/AimdRecorderWysiwygFieldHost.vue +93 -0
  141. package/src/components/AimdRecorderWysiwygSurface.vue +487 -0
  142. package/src/components/AimdSeqVizViewer.vue +122 -0
  143. package/src/components/AimdStepCheckField.vue +453 -0
  144. package/src/components/AimdVarField.vue +213 -0
  145. package/src/components/AimdVarTableField.vue +516 -0
  146. package/src/components/DnaAnnotationEditor.vue +477 -0
  147. package/src/components/DnaSequenceEditor.vue +44 -0
  148. package/src/components/DnaSequenceToolbar.vue +98 -0
  149. package/src/components/DnaSequenceViewer.vue +162 -0
  150. package/src/components/index.ts +21 -0
  151. package/src/components/recorderMilkdownPlugin.ts +632 -0
  152. package/src/composables/__tests__/useDnaSequence.test.ts +467 -0
  153. package/src/composables/__tests__/useFieldAdapters.test.ts +115 -0
  154. package/src/composables/__tests__/useFocusManagement.test.ts +66 -0
  155. package/src/composables/__tests__/useRecordState.test.ts +586 -0
  156. package/src/composables/__tests__/useStepTimers.test.ts +64 -0
  157. package/src/composables/__tests__/useVarHelpers.test.ts +424 -0
  158. package/src/composables/__tests__/workbenchFieldEditing.test.ts +97 -0
  159. package/src/composables/index.ts +94 -0
  160. package/src/composables/useClientAssignerRunner.ts +74 -0
  161. package/src/composables/useDnaSequence.ts +400 -0
  162. package/src/composables/useDnaSequenceField.ts +316 -0
  163. package/src/composables/useFieldAdapters.ts +61 -0
  164. package/src/composables/useFieldRendering.ts +269 -0
  165. package/src/composables/useFocusManagement.ts +144 -0
  166. package/src/composables/useRecordState.ts +429 -0
  167. package/src/composables/useStepTimers.ts +243 -0
  168. package/src/composables/useVarHelpers.ts +545 -0
  169. package/src/composables/useVarTableDragDrop.ts +236 -0
  170. package/src/composables/workbenchFieldEditing.ts +705 -0
  171. package/src/css.d.ts +9 -0
  172. package/src/index.ts +65 -0
  173. package/src/locales.ts +559 -0
  174. package/src/monaco-code.ts +75 -0
  175. package/src/styles/aimd.css +994 -0
  176. package/src/type-plugins.ts +132 -0
  177. package/src/type-utils.ts +3 -0
  178. package/src/types.ts +191 -0
@@ -0,0 +1,586 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ import {
4
+ cloneRecordData,
5
+ normalizeCheckLike,
6
+ getRecordDataSignature,
7
+ normalizeStepLike,
8
+ normalizeIncomingRecord,
9
+ replaceSection,
10
+ applyNormalizedRecord,
11
+ applyIncomingRecord,
12
+ normalizeStepFields,
13
+ normalizeCheckFields,
14
+ normalizeQuizFields,
15
+ normalizeVarTableFields,
16
+ getQuizDefaultValue,
17
+ ensureDefaultsFromFields,
18
+ createEmptyVarTableRow,
19
+ normalizeVarTableRows,
20
+ parsePastedVarTableText,
21
+ applyPastedVarTableGrid,
22
+ } from '../useRecordState'
23
+ import { createEmptyProtocolRecordData } from '../../types'
24
+ import { createEmptyCheckRecordItem, createEmptyStepRecordItem } from '../useStepTimers'
25
+
26
+ // ---------------------------------------------------------------------------
27
+ // cloneRecordData
28
+ // ---------------------------------------------------------------------------
29
+
30
+ describe('cloneRecordData', () => {
31
+ it('produces a deep copy', () => {
32
+ const original = createEmptyProtocolRecordData()
33
+ original.var.temperature = 36.5
34
+ original.step.step1 = { ...createEmptyStepRecordItem(), checked: true, annotation: 'done' }
35
+ const cloned = cloneRecordData(original)
36
+
37
+ expect(cloned).toEqual(original)
38
+ expect(cloned).not.toBe(original)
39
+ expect(cloned.var).not.toBe(original.var)
40
+ expect(cloned.step).not.toBe(original.step)
41
+ })
42
+
43
+ it('deep-clones nested objects', () => {
44
+ const original = createEmptyProtocolRecordData()
45
+ original.var.data = { nested: [1, 2, 3] }
46
+ const cloned = cloneRecordData(original)
47
+
48
+ ;(cloned.var.data as any).nested.push(4)
49
+ expect((original.var.data as any).nested).toEqual([1, 2, 3])
50
+ })
51
+ })
52
+
53
+ describe('getRecordDataSignature', () => {
54
+ it('returns the same signature for semantically equal records with different key order', () => {
55
+ const left = {
56
+ var: {
57
+ summary: { blocks: [{ text: 'A', id: 2 }, { id: 1, text: 'B' }] },
58
+ temperature: 25,
59
+ },
60
+ step: {
61
+ s1: { annotation: 'done', checked: true },
62
+ },
63
+ } as unknown as Partial<ReturnType<typeof createEmptyProtocolRecordData>>
64
+
65
+ const right = {
66
+ step: {
67
+ s1: { checked: true, annotation: 'done' },
68
+ },
69
+ var: {
70
+ temperature: 25,
71
+ summary: { blocks: [{ id: 2, text: 'A' }, { text: 'B', id: 1 }] },
72
+ },
73
+ } as unknown as Partial<ReturnType<typeof createEmptyProtocolRecordData>>
74
+
75
+ expect(getRecordDataSignature(left)).toBe(getRecordDataSignature(right))
76
+ })
77
+
78
+ it('returns a different signature when record content changes', () => {
79
+ const left = { var: { summary: 'alpha' } }
80
+ const right = { var: { summary: 'beta' } }
81
+
82
+ expect(getRecordDataSignature(left)).not.toBe(getRecordDataSignature(right))
83
+ })
84
+ })
85
+
86
+ // ---------------------------------------------------------------------------
87
+ // normalizeStepLike
88
+ // ---------------------------------------------------------------------------
89
+
90
+ describe('normalizeStepLike', () => {
91
+ it('returns default for null', () => {
92
+ expect(normalizeStepLike(null)).toEqual(createEmptyStepRecordItem())
93
+ })
94
+
95
+ it('returns default for undefined', () => {
96
+ expect(normalizeStepLike(undefined)).toEqual(createEmptyStepRecordItem())
97
+ })
98
+
99
+ it('returns default for arrays', () => {
100
+ expect(normalizeStepLike([1, 2])).toEqual(createEmptyStepRecordItem())
101
+ })
102
+
103
+ it('returns default for primitives', () => {
104
+ expect(normalizeStepLike('hello')).toEqual(createEmptyStepRecordItem())
105
+ expect(normalizeStepLike(42)).toEqual(createEmptyStepRecordItem())
106
+ })
107
+
108
+ it('normalizes valid object', () => {
109
+ expect(normalizeStepLike({ checked: true, annotation: 'note' }))
110
+ .toEqual({ ...createEmptyStepRecordItem(), checked: true, annotation: 'note' })
111
+ })
112
+
113
+ it('coerces checked to boolean', () => {
114
+ expect(normalizeStepLike({ checked: 1, annotation: '' }))
115
+ .toEqual({ ...createEmptyStepRecordItem(), checked: true, annotation: '' })
116
+ expect(normalizeStepLike({ checked: 0, annotation: '' }))
117
+ .toEqual(createEmptyStepRecordItem())
118
+ })
119
+
120
+ it('replaces non-string annotation with empty string', () => {
121
+ expect(normalizeStepLike({ checked: false, annotation: 123 }))
122
+ .toEqual(createEmptyStepRecordItem())
123
+ })
124
+
125
+ it('normalizes timer fields when present', () => {
126
+ expect(normalizeStepLike({
127
+ checked: true,
128
+ annotation: 'timed',
129
+ elapsed_ms: '2500',
130
+ timer_started_at_ms: 5000,
131
+ started_at_ms: 4000,
132
+ ended_at_ms: null,
133
+ })).toEqual({
134
+ checked: true,
135
+ annotation: 'timed',
136
+ elapsed_ms: 2500,
137
+ timer_started_at_ms: 5000,
138
+ started_at_ms: 4000,
139
+ ended_at_ms: null,
140
+ })
141
+ })
142
+ })
143
+
144
+ describe('normalizeCheckLike', () => {
145
+ it('normalizes check items without timer fields', () => {
146
+ expect(normalizeCheckLike({ checked: 1, annotation: 'ok', elapsed_ms: 500 }))
147
+ .toEqual({ checked: true, annotation: 'ok' })
148
+ })
149
+
150
+ it('falls back to the empty check record shape', () => {
151
+ expect(normalizeCheckLike(null)).toEqual(createEmptyCheckRecordItem())
152
+ })
153
+ })
154
+
155
+ // ---------------------------------------------------------------------------
156
+ // normalizeIncomingRecord
157
+ // ---------------------------------------------------------------------------
158
+
159
+ describe('normalizeIncomingRecord', () => {
160
+ it('returns empty record for undefined', () => {
161
+ const result = normalizeIncomingRecord(undefined)
162
+ expect(result).toEqual(createEmptyProtocolRecordData())
163
+ })
164
+
165
+ it('returns empty record for null', () => {
166
+ const result = normalizeIncomingRecord(null as any)
167
+ expect(result).toEqual(createEmptyProtocolRecordData())
168
+ })
169
+
170
+ it('preserves valid var data', () => {
171
+ const result = normalizeIncomingRecord({ var: { x: 42 } })
172
+ expect(result.var.x).toBe(42)
173
+ })
174
+
175
+ it('normalizes step items', () => {
176
+ const result = normalizeIncomingRecord({
177
+ step: { s1: { checked: true, annotation: 'ok' } as any },
178
+ })
179
+ expect(result.step.s1).toEqual({ ...createEmptyStepRecordItem(), checked: true, annotation: 'ok' })
180
+ })
181
+
182
+ it('normalizes check items', () => {
183
+ const result = normalizeIncomingRecord({
184
+ check: { c1: { checked: false, annotation: '' } as any },
185
+ })
186
+ expect(result.check.c1).toEqual(createEmptyCheckRecordItem())
187
+ })
188
+
189
+ it('preserves quiz data', () => {
190
+ const result = normalizeIncomingRecord({ quiz: { q1: 'answer' } })
191
+ expect(result.quiz.q1).toBe('answer')
192
+ })
193
+
194
+ it('ignores non-object var', () => {
195
+ const result = normalizeIncomingRecord({ var: 'bad' as any })
196
+ expect(result.var).toEqual({})
197
+ })
198
+ })
199
+
200
+ // ---------------------------------------------------------------------------
201
+ // replaceSection
202
+ // ---------------------------------------------------------------------------
203
+
204
+ describe('replaceSection', () => {
205
+ it('replaces all keys in target with source keys', () => {
206
+ const target: Record<string, unknown> = { a: 1, b: 2 }
207
+ replaceSection(target, { c: 3 })
208
+ expect(target).toEqual({ c: 3 })
209
+ })
210
+
211
+ it('removes keys not in source', () => {
212
+ const target: Record<string, unknown> = { a: 1 }
213
+ replaceSection(target, {})
214
+ expect(Object.keys(target)).toHaveLength(0)
215
+ })
216
+ })
217
+
218
+ // ---------------------------------------------------------------------------
219
+ // applyNormalizedRecord / applyIncomingRecord
220
+ // ---------------------------------------------------------------------------
221
+
222
+ describe('applyNormalizedRecord', () => {
223
+ it('replaces all sections', () => {
224
+ const local = createEmptyProtocolRecordData()
225
+ local.var.old = 'value'
226
+
227
+ const normalized = createEmptyProtocolRecordData()
228
+ normalized.var.new_var = 'new'
229
+ normalized.step.s1 = { ...createEmptyStepRecordItem(), checked: true }
230
+
231
+ applyNormalizedRecord(local, normalized)
232
+ expect(local.var).toEqual({ new_var: 'new' })
233
+ expect(local.step).toEqual({ s1: { ...createEmptyStepRecordItem(), checked: true } })
234
+ })
235
+ })
236
+
237
+ describe('applyIncomingRecord', () => {
238
+ it('normalizes and applies', () => {
239
+ const local = createEmptyProtocolRecordData()
240
+ applyIncomingRecord(local, { var: { temp: 36 } })
241
+ expect(local.var.temp).toBe(36)
242
+ })
243
+ })
244
+
245
+ // ---------------------------------------------------------------------------
246
+ // normalizeStepFields
247
+ // ---------------------------------------------------------------------------
248
+
249
+ describe('normalizeStepFields', () => {
250
+ it('returns empty array for non-array input', () => {
251
+ expect(normalizeStepFields(null)).toEqual([])
252
+ expect(normalizeStepFields('string')).toEqual([])
253
+ expect(normalizeStepFields(42)).toEqual([])
254
+ })
255
+
256
+ it('converts strings to objects', () => {
257
+ expect(normalizeStepFields(['step1', 'step2'])).toEqual([
258
+ { name: 'step1' },
259
+ { name: 'step2' },
260
+ ])
261
+ })
262
+
263
+ it('trims string names', () => {
264
+ expect(normalizeStepFields([' spaced '])).toEqual([{ name: 'spaced' }])
265
+ })
266
+
267
+ it('accepts objects with name property', () => {
268
+ expect(normalizeStepFields([{ name: 'obj_step' }])).toEqual([
269
+ { name: 'obj_step' },
270
+ ])
271
+ })
272
+
273
+ it('filters out empty strings', () => {
274
+ expect(normalizeStepFields(['', ' ', 'valid'])).toEqual([{ name: 'valid' }])
275
+ })
276
+
277
+ it('filters out invalid items', () => {
278
+ expect(normalizeStepFields([null, undefined, 42, 'valid'])).toEqual([
279
+ { name: 'valid' },
280
+ ])
281
+ })
282
+ })
283
+
284
+ // ---------------------------------------------------------------------------
285
+ // normalizeCheckFields
286
+ // ---------------------------------------------------------------------------
287
+
288
+ describe('normalizeCheckFields', () => {
289
+ it('returns empty array for non-array', () => {
290
+ expect(normalizeCheckFields(undefined)).toEqual([])
291
+ })
292
+
293
+ it('converts strings to name+label', () => {
294
+ expect(normalizeCheckFields(['check1'])).toEqual([
295
+ { name: 'check1', label: 'check1' },
296
+ ])
297
+ })
298
+
299
+ it('accepts objects with name and optional label', () => {
300
+ expect(normalizeCheckFields([{ name: 'c1', label: 'Check 1' }])).toEqual([
301
+ { name: 'c1', label: 'Check 1' },
302
+ ])
303
+ })
304
+
305
+ it('omits label when not a string', () => {
306
+ expect(normalizeCheckFields([{ name: 'c1', label: 123 }])).toEqual([
307
+ { name: 'c1', label: undefined },
308
+ ])
309
+ })
310
+ })
311
+
312
+ // ---------------------------------------------------------------------------
313
+ // normalizeQuizFields
314
+ // ---------------------------------------------------------------------------
315
+
316
+ describe('normalizeQuizFields', () => {
317
+ it('returns empty for non-array', () => {
318
+ expect(normalizeQuizFields(null)).toEqual([])
319
+ })
320
+
321
+ it('filters valid quiz fields', () => {
322
+ const valid = { id: 'q1', type: 'choice', stem: 'What?' }
323
+ const invalid = { id: 'q2' }
324
+ expect(normalizeQuizFields([valid, invalid, null])).toEqual([valid])
325
+ })
326
+ })
327
+
328
+ // ---------------------------------------------------------------------------
329
+ // normalizeVarTableFields
330
+ // ---------------------------------------------------------------------------
331
+
332
+ describe('normalizeVarTableFields', () => {
333
+ it('returns empty for non-array', () => {
334
+ expect(normalizeVarTableFields({})).toEqual([])
335
+ })
336
+
337
+ it('filters valid var_table fields', () => {
338
+ const valid = { id: 'vt1', subvars: [{ id: 'col1' }] }
339
+ const invalid = { id: 'vt2' }
340
+ expect(normalizeVarTableFields([valid, invalid])).toEqual([valid])
341
+ })
342
+ })
343
+
344
+ // ---------------------------------------------------------------------------
345
+ // getQuizDefaultValue
346
+ // ---------------------------------------------------------------------------
347
+
348
+ describe('getQuizDefaultValue', () => {
349
+ it('returns empty string for choice quiz without default', () => {
350
+ const quiz = { id: 'q1', type: 'choice', stem: 'Q?', options: [{ key: 'a', label: 'A' }] }
351
+ expect(getQuizDefaultValue(quiz as any)).toBe('')
352
+ })
353
+
354
+ it('returns valid choice default', () => {
355
+ const quiz = {
356
+ id: 'q1', type: 'choice', stem: 'Q?',
357
+ options: [{ key: 'a', label: 'A' }, { key: 'b', label: 'B' }],
358
+ default: 'a',
359
+ }
360
+ expect(getQuizDefaultValue(quiz as any)).toBe('a')
361
+ })
362
+
363
+ it('rejects invalid choice default', () => {
364
+ const quiz = {
365
+ id: 'q1', type: 'choice', stem: 'Q?',
366
+ options: [{ key: 'a', label: 'A' }],
367
+ default: 'invalid',
368
+ }
369
+ expect(getQuizDefaultValue(quiz as any)).toBe('')
370
+ })
371
+
372
+ it('handles multiple choice mode', () => {
373
+ const quiz = {
374
+ id: 'q1', type: 'choice', stem: 'Q?', mode: 'multiple',
375
+ options: [{ key: 'a', label: 'A' }, { key: 'b', label: 'B' }],
376
+ default: ['a', 'b'],
377
+ }
378
+ expect(getQuizDefaultValue(quiz as any)).toEqual(['a', 'b'])
379
+ })
380
+
381
+ it('filters invalid items in multiple choice default', () => {
382
+ const quiz = {
383
+ id: 'q1', type: 'choice', stem: 'Q?', mode: 'multiple',
384
+ options: [{ key: 'a', label: 'A' }],
385
+ default: ['a', 'invalid', 123],
386
+ }
387
+ expect(getQuizDefaultValue(quiz as any)).toEqual(['a'])
388
+ })
389
+
390
+ it('returns empty array for multiple choice without default', () => {
391
+ const quiz = {
392
+ id: 'q1', type: 'choice', stem: 'Q?', mode: 'multiple',
393
+ options: [{ key: 'a', label: 'A' }],
394
+ }
395
+ expect(getQuizDefaultValue(quiz as any)).toEqual([])
396
+ })
397
+
398
+ it('returns blank map for blank quiz', () => {
399
+ const quiz = {
400
+ id: 'q1', type: 'blank', stem: 'Fill ____',
401
+ blanks: [{ key: 'b1' }, { key: 'b2' }],
402
+ }
403
+ expect(getQuizDefaultValue(quiz as any)).toEqual({ b1: '', b2: '' })
404
+ })
405
+
406
+ it('uses object default for blank quiz', () => {
407
+ const quiz = {
408
+ id: 'q1', type: 'blank', stem: 'Fill ____',
409
+ blanks: [{ key: 'b1' }],
410
+ default: { b1: 'answer' },
411
+ }
412
+ expect(getQuizDefaultValue(quiz as any)).toEqual({ b1: 'answer' })
413
+ })
414
+
415
+ it('uses string default for single-blank quiz', () => {
416
+ const quiz = {
417
+ id: 'q1', type: 'blank', stem: 'Fill ____',
418
+ blanks: [{ key: 'b1' }],
419
+ default: 'answer',
420
+ }
421
+ expect(getQuizDefaultValue(quiz as any)).toEqual({ b1: 'answer' })
422
+ })
423
+
424
+ it('returns string default for open quiz', () => {
425
+ const quiz = { id: 'q1', type: 'open', stem: 'Describe', default: 'my answer' }
426
+ expect(getQuizDefaultValue(quiz as any)).toBe('my answer')
427
+ })
428
+
429
+ it('returns empty string for open quiz without default', () => {
430
+ const quiz = { id: 'q1', type: 'open', stem: 'Describe' }
431
+ expect(getQuizDefaultValue(quiz as any)).toBe('')
432
+ })
433
+ })
434
+
435
+ // ---------------------------------------------------------------------------
436
+ // createEmptyVarTableRow / normalizeVarTableRows
437
+ // ---------------------------------------------------------------------------
438
+
439
+ describe('createEmptyVarTableRow', () => {
440
+ it('creates row with empty string values', () => {
441
+ expect(createEmptyVarTableRow(['a', 'b'])).toEqual({ a: '', b: '' })
442
+ })
443
+ })
444
+
445
+ describe('normalizeVarTableRows', () => {
446
+ it('returns one empty row for non-array input', () => {
447
+ const rows = normalizeVarTableRows(null, ['x', 'y'])
448
+ expect(rows).toEqual([{ x: '', y: '' }])
449
+ })
450
+
451
+ it('returns one empty row for empty array', () => {
452
+ const rows = normalizeVarTableRows([], ['x'])
453
+ expect(rows).toEqual([{ x: '' }])
454
+ })
455
+
456
+ it('normalizes existing rows to columns', () => {
457
+ const rows = normalizeVarTableRows([{ x: 'val', extra: 'ignored' }], ['x', 'y'])
458
+ expect(rows).toEqual([{ x: 'val', y: '' }])
459
+ })
460
+
461
+ it('converts non-string values to strings', () => {
462
+ const rows = normalizeVarTableRows([{ x: 42 }], ['x'])
463
+ expect(rows).toEqual([{ x: '42' }])
464
+ })
465
+
466
+ it('handles null/undefined values', () => {
467
+ const rows = normalizeVarTableRows([{ x: null }], ['x'])
468
+ expect(rows).toEqual([{ x: '' }])
469
+ })
470
+
471
+ it('replaces invalid row items with empty rows', () => {
472
+ const rows = normalizeVarTableRows([null, 'bad', { x: 'ok' }], ['x'])
473
+ expect(rows).toEqual([{ x: '' }, { x: '' }, { x: 'ok' }])
474
+ })
475
+ })
476
+
477
+ describe('parsePastedVarTableText', () => {
478
+ it('parses spreadsheet-style tabular text', () => {
479
+ expect(parsePastedVarTableText('A\tB\r\n1\t2\r\n')).toEqual([
480
+ ['A', 'B'],
481
+ ['1', '2'],
482
+ ])
483
+ })
484
+
485
+ it('keeps single-column multiline text as one column per row', () => {
486
+ expect(parsePastedVarTableText('alpha\nbeta')).toEqual([
487
+ ['alpha'],
488
+ ['beta'],
489
+ ])
490
+ })
491
+ })
492
+
493
+ describe('applyPastedVarTableGrid', () => {
494
+ it('applies pasted cells from the current cell and appends rows as needed', () => {
495
+ const rows = [{ sample: 'S1', value: '' }]
496
+ const result = applyPastedVarTableGrid(
497
+ rows,
498
+ ['sample', 'value'],
499
+ 0,
500
+ 1,
501
+ [['10'], ['20']],
502
+ )
503
+
504
+ expect(rows).toEqual([
505
+ { sample: 'S1', value: '10' },
506
+ { sample: '', value: '20' },
507
+ ])
508
+ expect(result).toEqual({
509
+ rowsAdded: 1,
510
+ changedCells: [
511
+ { rowIndex: 0, column: 'value', value: '10' },
512
+ { rowIndex: 1, column: 'value', value: '20' },
513
+ ],
514
+ })
515
+ })
516
+
517
+ it('skips disabled columns and ignores cells outside the table width', () => {
518
+ const rows = [{ a: '', b: '', c: '' }]
519
+ const result = applyPastedVarTableGrid(
520
+ rows,
521
+ ['a', 'b', 'c'],
522
+ 0,
523
+ 0,
524
+ [['x', 'y', 'z', 'overflow']],
525
+ { disabledColumns: ['b'] },
526
+ )
527
+
528
+ expect(rows).toEqual([{ a: 'x', b: '', c: 'z' }])
529
+ expect(result).toEqual({
530
+ rowsAdded: 0,
531
+ changedCells: [
532
+ { rowIndex: 0, column: 'a', value: 'x' },
533
+ { rowIndex: 0, column: 'c', value: 'z' },
534
+ ],
535
+ })
536
+ })
537
+ })
538
+
539
+ // ---------------------------------------------------------------------------
540
+ // ensureDefaultsFromFields
541
+ // ---------------------------------------------------------------------------
542
+
543
+ describe('ensureDefaultsFromFields', () => {
544
+ it('adds missing step defaults', () => {
545
+ const record = createEmptyProtocolRecordData()
546
+ const fields = { step: [{ name: 's1' }], check: [], quiz: [], var: [], var_table: [], subvar: [], assigner: [], ref: [], fig: [], cite: [] }
547
+ const changed = ensureDefaultsFromFields(record, fields as any)
548
+ expect(changed).toBe(true)
549
+ expect(record.step.s1).toEqual(createEmptyStepRecordItem())
550
+ })
551
+
552
+ it('does not overwrite existing step', () => {
553
+ const record = createEmptyProtocolRecordData()
554
+ record.step.s1 = { ...createEmptyStepRecordItem(), checked: true, annotation: 'existing', elapsed_ms: 1500 }
555
+ const fields = { step: [{ name: 's1' }], check: [], quiz: [], var: [], var_table: [], subvar: [], assigner: [], ref: [], fig: [], cite: [] }
556
+ const changed = ensureDefaultsFromFields(record, fields as any)
557
+ expect(changed).toBe(false)
558
+ expect(record.step.s1.annotation).toBe('existing')
559
+ expect(record.step.s1.elapsed_ms).toBe(1500)
560
+ })
561
+
562
+ it('adds missing check defaults', () => {
563
+ const record = createEmptyProtocolRecordData()
564
+ const fields = { step: [], check: [{ name: 'c1' }], quiz: [], var: [], var_table: [], subvar: [], assigner: [], ref: [], fig: [], cite: [] }
565
+ const changed = ensureDefaultsFromFields(record, fields as any)
566
+ expect(changed).toBe(true)
567
+ expect(record.check.c1).toEqual(createEmptyCheckRecordItem())
568
+ })
569
+
570
+ it('adds missing quiz defaults', () => {
571
+ const record = createEmptyProtocolRecordData()
572
+ const fields = {
573
+ step: [], check: [], var: [], var_table: [], subvar: [], assigner: [], ref: [], fig: [], cite: [],
574
+ quiz: [{ id: 'q1', type: 'open', stem: 'Q?' }],
575
+ }
576
+ const changed = ensureDefaultsFromFields(record, fields as any)
577
+ expect(changed).toBe(true)
578
+ expect(record.quiz.q1).toBe('')
579
+ })
580
+
581
+ it('returns false when nothing changes', () => {
582
+ const record = createEmptyProtocolRecordData()
583
+ const fields = { step: [], check: [], quiz: [], var: [], var_table: [], subvar: [], assigner: [], ref: [], fig: [], cite: [] }
584
+ expect(ensureDefaultsFromFields(record, fields as any)).toBe(false)
585
+ })
586
+ })
@@ -0,0 +1,64 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ import {
4
+ createEmptyStepRecordItem,
5
+ formatStepDuration,
6
+ getProtocolEstimatedDurationMs,
7
+ getStepElapsedMs,
8
+ getStepRemainingMs,
9
+ isStepTimerWarning,
10
+ pauseStepTimer,
11
+ resetStepTimer,
12
+ resolveStepTimerMode,
13
+ startStepTimer,
14
+ } from '../useStepTimers'
15
+
16
+ describe('useStepTimers', () => {
17
+ it('formats durations in a compact human-readable form', () => {
18
+ expect(formatStepDuration(3_723_000, 'en-US')).toBe('1h 2m 3s')
19
+ expect(formatStepDuration(125_000, 'zh-CN')).toBe('2分5秒')
20
+ expect(formatStepDuration(187_200_000, 'en-US')).toBe('2d 4h')
21
+ })
22
+
23
+ it('tracks elapsed time across start, pause, and reset', () => {
24
+ const step = createEmptyStepRecordItem()
25
+
26
+ expect(startStepTimer(step, 1_000)).toBe(true)
27
+ expect(getStepElapsedMs(step, 2_500)).toBe(1_500)
28
+
29
+ expect(pauseStepTimer(step, 4_000)).toBe(true)
30
+ expect(step.elapsed_ms).toBe(3_000)
31
+
32
+ expect(startStepTimer(step, 5_000)).toBe(true)
33
+ expect(getStepElapsedMs(step, 6_500)).toBe(4_500)
34
+
35
+ expect(resetStepTimer(step)).toBe(true)
36
+ expect(step).toEqual(createEmptyStepRecordItem())
37
+ })
38
+
39
+ it('resolves countdown modes only when an estimate exists', () => {
40
+ expect(resolveStepTimerMode({ timer_mode: 'countdown', estimated_duration_ms: 30_000 })).toBe('countdown')
41
+ expect(resolveStepTimerMode({ timer_mode: 'both', estimated_duration_ms: 30_000 })).toBe('both')
42
+ expect(resolveStepTimerMode({ timer_mode: 'countdown' })).toBe('elapsed')
43
+ })
44
+
45
+ it('computes remaining time and warning thresholds for countdown timers', () => {
46
+ const step = createEmptyStepRecordItem()
47
+ startStepTimer(step, 0)
48
+
49
+ expect(getStepRemainingMs(step, 30_000, 15_000)).toBe(15_000)
50
+ expect(isStepTimerWarning(15_000, 30_000)).toBe(false)
51
+ expect(getStepRemainingMs(step, 30_000, 25_000)).toBe(5_000)
52
+ expect(isStepTimerWarning(5_000, 30_000)).toBe(true)
53
+ expect(getStepRemainingMs(step, 30_000, 35_000)).toBe(-5_000)
54
+ })
55
+
56
+ it('avoids double-counting child estimates when a parent step already has an estimate', () => {
57
+ expect(getProtocolEstimatedDurationMs([
58
+ { id: 'parent', estimated_duration_ms: 60_000 },
59
+ { id: 'child_a', parent_id: 'parent', estimated_duration_ms: 10_000 },
60
+ { id: 'child_b', parent_id: 'parent', estimated_duration_ms: 15_000 },
61
+ { id: 'sibling', estimated_duration_ms: 5_000 },
62
+ ])).toBe(65_000)
63
+ })
64
+ })