|
| 1 | +/** |
| 2 | + * Copyright 2026 Scratch Foundation |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +import * as Blockly from 'blockly/core' |
| 6 | +import { afterEach, assert, beforeEach, describe, expect, it } from 'vitest' |
| 7 | +// Registers the C-block wrapping fix. |
| 8 | +import '../../src/scratch_c_block_wrap' |
| 9 | + |
| 10 | +let workspace: Blockly.Workspace |
| 11 | + |
| 12 | +beforeEach(() => { |
| 13 | + workspace = new Blockly.Workspace() |
| 14 | +}) |
| 15 | + |
| 16 | +afterEach(() => { |
| 17 | + workspace.dispose() |
| 18 | +}) |
| 19 | + |
| 20 | +describe('C-block wrapping', () => { |
| 21 | + const BLOCK_TYPES = ['test_c_block_wrap', 'test_stmt_wrap_inner', 'test_stmt_wrap_outer'] |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + Blockly.defineBlocksWithJsonArray([ |
| 25 | + { |
| 26 | + type: 'test_c_block_wrap', |
| 27 | + message0: 'repeat %1', |
| 28 | + args0: [{ type: 'input_statement', name: 'SUBSTACK' }], |
| 29 | + previousStatement: null, |
| 30 | + nextStatement: null, |
| 31 | + }, |
| 32 | + { |
| 33 | + type: 'test_stmt_wrap_inner', |
| 34 | + message0: 'inner block', |
| 35 | + previousStatement: null, |
| 36 | + nextStatement: null, |
| 37 | + }, |
| 38 | + { |
| 39 | + type: 'test_stmt_wrap_outer', |
| 40 | + message0: 'outer block', |
| 41 | + previousStatement: null, |
| 42 | + nextStatement: null, |
| 43 | + }, |
| 44 | + ]) |
| 45 | + }) |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + for (const type of BLOCK_TYPES) { |
| 49 | + delete Blockly.Blocks[type] |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + it('displaced block goes into statement input when C-block is dropped onto a middle-of-stack block (actual drop)', () => { |
| 54 | + // Stack: outerBlock → innerBlock |
| 55 | + const cBlock = workspace.newBlock('test_c_block_wrap') |
| 56 | + const outerBlock = workspace.newBlock('test_stmt_wrap_outer') |
| 57 | + const innerBlock = workspace.newBlock('test_stmt_wrap_inner') |
| 58 | + |
| 59 | + // Connect outerBlock → innerBlock (innerBlock is connected inside an existing stack) |
| 60 | + assert(outerBlock.nextConnection, 'outerBlock should have nextConnection') |
| 61 | + assert(innerBlock.previousConnection, 'innerBlock should have previousConnection') |
| 62 | + outerBlock.nextConnection.connect(innerBlock.previousConnection) |
| 63 | + |
| 64 | + // Simulate the scenario: the C-block's previous connects to outerBlock's next, |
| 65 | + // displacing innerBlock. Where should innerBlock (the orphan) go? |
| 66 | + // |
| 67 | + // Expected (wrap behavior): into cBlock's statement input (SUBSTACK) |
| 68 | + // Actual before fix (bug): after cBlock's next connection |
| 69 | + const result = Blockly.Connection.getConnectionForOrphanedConnection(cBlock, innerBlock.previousConnection) |
| 70 | + |
| 71 | + const statementInputConn = cBlock.getInput('SUBSTACK')?.connection |
| 72 | + assert(statementInputConn, 'cBlock should have a SUBSTACK statement input with a connection') |
| 73 | + |
| 74 | + expect(result).toBe(statementInputConn) |
| 75 | + }) |
| 76 | + |
| 77 | + it('hideInsertionMarker restores displaced block from statement input so stack heals correctly', () => { |
| 78 | + // When a C-block insertion marker is mid-stack with the displaced block (B2) |
| 79 | + // sitting in its statement input (as placed by our getConnectionForOrphanedConnection |
| 80 | + // patch), hideInsertionMarker must move B2 back to marker.nextConnection so that |
| 81 | + // marker.unplug(true) can reconnect B1 → B2 when the preview is dismissed. |
| 82 | + const b1 = workspace.newBlock('test_stmt_wrap_outer') |
| 83 | + const marker = workspace.newBlock('test_c_block_wrap') |
| 84 | + const b2 = workspace.newBlock('test_stmt_wrap_inner') |
| 85 | + |
| 86 | + marker.setInsertionMarker(true) |
| 87 | + |
| 88 | + // B1 → marker (marker is mid-stack) |
| 89 | + assert(b1.nextConnection, 'b1 should have nextConnection') |
| 90 | + assert(marker.previousConnection, 'marker should have previousConnection') |
| 91 | + b1.nextConnection.connect(marker.previousConnection) |
| 92 | + |
| 93 | + // B2 is in the marker's statement input (placed there by our orphan-connection patch) |
| 94 | + const markerStatementConn = marker.getInput('SUBSTACK')?.connection |
| 95 | + assert(markerStatementConn, 'marker should have a SUBSTACK statement input') |
| 96 | + assert(b2.previousConnection, 'b2 should have previousConnection') |
| 97 | + markerStatementConn.connect(b2.previousConnection) |
| 98 | + |
| 99 | + // The original hideInsertionMarker does not use `this`, so any object works as context. |
| 100 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call -- tested function does not use `this` |
| 101 | + ;(Blockly.InsertionMarkerPreviewer.prototype as any).hideInsertionMarker.call({}, marker.previousConnection) |
| 102 | + |
| 103 | + // After cleanup, the stack should be healed: B1 → B2 (marker was disposed) |
| 104 | + expect(b1.nextConnection.targetBlock()).toBe(b2) |
| 105 | + }) |
| 106 | + |
| 107 | + it('displaced block goes into statement input even when C-block is an insertion marker (preview matches drop)', () => { |
| 108 | + // During drag preview, Blockly creates an insertion marker to visualise |
| 109 | + // where the dragged block will land. getConnectionForOrphanedConnection |
| 110 | + // is called with the MARKER as startBlock. We want the displaced block to |
| 111 | + // go into the marker's statement input so the preview matches the actual |
| 112 | + // drop result (B2 inside C, not after C). A separate patch in |
| 113 | + // hideInsertionMarker restores B2 to nextConnection before cleanup so that |
| 114 | + // unplug(true) can heal the stack correctly when the preview is dismissed. |
| 115 | + const markerBlock = workspace.newBlock('test_c_block_wrap') |
| 116 | + markerBlock.setInsertionMarker(true) |
| 117 | + |
| 118 | + const outerBlock = workspace.newBlock('test_stmt_wrap_outer') |
| 119 | + const innerBlock = workspace.newBlock('test_stmt_wrap_inner') |
| 120 | + assert(outerBlock.nextConnection, 'outerBlock should have nextConnection') |
| 121 | + assert(innerBlock.previousConnection, 'innerBlock should have previousConnection') |
| 122 | + outerBlock.nextConnection.connect(innerBlock.previousConnection) |
| 123 | + |
| 124 | + const result = Blockly.Connection.getConnectionForOrphanedConnection(markerBlock, innerBlock.previousConnection) |
| 125 | + |
| 126 | + const statementInputConn = markerBlock.getInput('SUBSTACK')?.connection |
| 127 | + assert(statementInputConn, 'markerBlock should have a SUBSTACK statement input with a connection') |
| 128 | + |
| 129 | + // Should redirect the orphan into the statement input (same as a real block). |
| 130 | + expect(result).toBe(statementInputConn) |
| 131 | + }) |
| 132 | +}) |
0 commit comments