|
1 | 1 | /** |
2 | 2 | * @vitest-environment node |
3 | 3 | */ |
4 | | -import { describe, expect, it } from 'vitest' |
| 4 | +import { describe, expect, it, vi } from 'vitest' |
5 | 5 | import type { BlockState } from '@/stores/workflows/workflow/types' |
6 | | -import { migrateSubblockIds } from './subblock-migrations' |
| 6 | + |
| 7 | +vi.unmock('@/blocks/registry') |
| 8 | + |
| 9 | +import { backfillCanonicalModes, migrateSubblockIds } from './subblock-migrations' |
7 | 10 |
|
8 | 11 | function makeBlock(overrides: Partial<BlockState> & { type: string }): BlockState { |
9 | 12 | return { |
@@ -181,3 +184,185 @@ describe('migrateSubblockIds', () => { |
181 | 184 | expect(migrated).toBe(false) |
182 | 185 | }) |
183 | 186 | }) |
| 187 | + |
| 188 | +describe('backfillCanonicalModes', () => { |
| 189 | + it('should add missing canonicalModes entry for knowledge block with basic value', () => { |
| 190 | + const input: Record<string, BlockState> = { |
| 191 | + b1: makeBlock({ |
| 192 | + type: 'knowledge', |
| 193 | + data: {}, |
| 194 | + subBlocks: { |
| 195 | + operation: { id: 'operation', type: 'dropdown', value: 'search' }, |
| 196 | + knowledgeBaseSelector: { |
| 197 | + id: 'knowledgeBaseSelector', |
| 198 | + type: 'knowledge-base-selector', |
| 199 | + value: 'kb-uuid', |
| 200 | + }, |
| 201 | + }, |
| 202 | + }), |
| 203 | + } |
| 204 | + |
| 205 | + const { blocks, migrated } = backfillCanonicalModes(input) |
| 206 | + |
| 207 | + expect(migrated).toBe(true) |
| 208 | + const modes = blocks.b1.data?.canonicalModes as Record<string, string> |
| 209 | + expect(modes.knowledgeBaseId).toBe('basic') |
| 210 | + }) |
| 211 | + |
| 212 | + it('should resolve to advanced when only the advanced value is set', () => { |
| 213 | + const input: Record<string, BlockState> = { |
| 214 | + b1: makeBlock({ |
| 215 | + type: 'knowledge', |
| 216 | + data: {}, |
| 217 | + subBlocks: { |
| 218 | + operation: { id: 'operation', type: 'dropdown', value: 'search' }, |
| 219 | + manualKnowledgeBaseId: { |
| 220 | + id: 'manualKnowledgeBaseId', |
| 221 | + type: 'short-input', |
| 222 | + value: 'kb-uuid-manual', |
| 223 | + }, |
| 224 | + }, |
| 225 | + }), |
| 226 | + } |
| 227 | + |
| 228 | + const { blocks, migrated } = backfillCanonicalModes(input) |
| 229 | + |
| 230 | + expect(migrated).toBe(true) |
| 231 | + const modes = blocks.b1.data?.canonicalModes as Record<string, string> |
| 232 | + expect(modes.knowledgeBaseId).toBe('advanced') |
| 233 | + }) |
| 234 | + |
| 235 | + it('should not overwrite existing canonicalModes entries', () => { |
| 236 | + const input: Record<string, BlockState> = { |
| 237 | + b1: makeBlock({ |
| 238 | + type: 'knowledge', |
| 239 | + data: { canonicalModes: { knowledgeBaseId: 'advanced' } }, |
| 240 | + subBlocks: { |
| 241 | + knowledgeBaseSelector: { |
| 242 | + id: 'knowledgeBaseSelector', |
| 243 | + type: 'knowledge-base-selector', |
| 244 | + value: 'kb-uuid', |
| 245 | + }, |
| 246 | + }, |
| 247 | + }), |
| 248 | + } |
| 249 | + |
| 250 | + const { blocks, migrated } = backfillCanonicalModes(input) |
| 251 | + |
| 252 | + expect(migrated).toBe(false) |
| 253 | + const modes = blocks.b1.data?.canonicalModes as Record<string, string> |
| 254 | + expect(modes.knowledgeBaseId).toBe('advanced') |
| 255 | + }) |
| 256 | + |
| 257 | + it('should skip blocks with no canonical pairs in their config', () => { |
| 258 | + const input: Record<string, BlockState> = { |
| 259 | + b1: makeBlock({ |
| 260 | + type: 'function', |
| 261 | + data: {}, |
| 262 | + subBlocks: { |
| 263 | + code: { id: 'code', type: 'code', value: '' }, |
| 264 | + }, |
| 265 | + }), |
| 266 | + } |
| 267 | + |
| 268 | + const { migrated } = backfillCanonicalModes(input) |
| 269 | + |
| 270 | + expect(migrated).toBe(false) |
| 271 | + }) |
| 272 | + |
| 273 | + it('should not mutate the input blocks', () => { |
| 274 | + const input: Record<string, BlockState> = { |
| 275 | + b1: makeBlock({ |
| 276 | + type: 'knowledge', |
| 277 | + data: {}, |
| 278 | + subBlocks: { |
| 279 | + knowledgeBaseSelector: { |
| 280 | + id: 'knowledgeBaseSelector', |
| 281 | + type: 'knowledge-base-selector', |
| 282 | + value: 'kb-uuid', |
| 283 | + }, |
| 284 | + }, |
| 285 | + }), |
| 286 | + } |
| 287 | + |
| 288 | + const { blocks } = backfillCanonicalModes(input) |
| 289 | + |
| 290 | + expect(input.b1.data?.canonicalModes).toBeUndefined() |
| 291 | + expect((blocks.b1.data?.canonicalModes as Record<string, string>).knowledgeBaseId).toBe('basic') |
| 292 | + expect(blocks).not.toBe(input) |
| 293 | + }) |
| 294 | + |
| 295 | + it('should resolve correctly when existing field became the basic variant', () => { |
| 296 | + const input: Record<string, BlockState> = { |
| 297 | + b1: makeBlock({ |
| 298 | + type: 'knowledge', |
| 299 | + data: {}, |
| 300 | + subBlocks: { |
| 301 | + operation: { id: 'operation', type: 'dropdown', value: 'search' }, |
| 302 | + knowledgeBaseSelector: { |
| 303 | + id: 'knowledgeBaseSelector', |
| 304 | + type: 'knowledge-base-selector', |
| 305 | + value: 'kb-uuid', |
| 306 | + }, |
| 307 | + manualKnowledgeBaseId: { |
| 308 | + id: 'manualKnowledgeBaseId', |
| 309 | + type: 'short-input', |
| 310 | + value: '', |
| 311 | + }, |
| 312 | + }, |
| 313 | + }), |
| 314 | + } |
| 315 | + |
| 316 | + const { blocks, migrated } = backfillCanonicalModes(input) |
| 317 | + |
| 318 | + expect(migrated).toBe(true) |
| 319 | + const modes = blocks.b1.data?.canonicalModes as Record<string, string> |
| 320 | + expect(modes.knowledgeBaseId).toBe('basic') |
| 321 | + }) |
| 322 | + |
| 323 | + it('should resolve correctly when existing field became the advanced variant', () => { |
| 324 | + const input: Record<string, BlockState> = { |
| 325 | + b1: makeBlock({ |
| 326 | + type: 'knowledge', |
| 327 | + data: {}, |
| 328 | + subBlocks: { |
| 329 | + operation: { id: 'operation', type: 'dropdown', value: 'search' }, |
| 330 | + knowledgeBaseSelector: { |
| 331 | + id: 'knowledgeBaseSelector', |
| 332 | + type: 'knowledge-base-selector', |
| 333 | + value: '', |
| 334 | + }, |
| 335 | + manualKnowledgeBaseId: { |
| 336 | + id: 'manualKnowledgeBaseId', |
| 337 | + type: 'short-input', |
| 338 | + value: 'manually-entered-kb-id', |
| 339 | + }, |
| 340 | + }, |
| 341 | + }), |
| 342 | + } |
| 343 | + |
| 344 | + const { blocks, migrated } = backfillCanonicalModes(input) |
| 345 | + |
| 346 | + expect(migrated).toBe(true) |
| 347 | + const modes = blocks.b1.data?.canonicalModes as Record<string, string> |
| 348 | + expect(modes.knowledgeBaseId).toBe('advanced') |
| 349 | + }) |
| 350 | + |
| 351 | + it('should default to basic when neither value is set', () => { |
| 352 | + const input: Record<string, BlockState> = { |
| 353 | + b1: makeBlock({ |
| 354 | + type: 'knowledge', |
| 355 | + data: {}, |
| 356 | + subBlocks: { |
| 357 | + operation: { id: 'operation', type: 'dropdown', value: 'search' }, |
| 358 | + }, |
| 359 | + }), |
| 360 | + } |
| 361 | + |
| 362 | + const { blocks, migrated } = backfillCanonicalModes(input) |
| 363 | + |
| 364 | + expect(migrated).toBe(true) |
| 365 | + const modes = blocks.b1.data?.canonicalModes as Record<string, string> |
| 366 | + expect(modes.knowledgeBaseId).toBe('basic') |
| 367 | + }) |
| 368 | +}) |
0 commit comments