|
| 1 | +/** |
| 2 | + * Copyright 2026 Scratch Foundation |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +import * as Blockly from 'blockly/core' |
| 6 | +import { afterAll, afterEach, assert, beforeAll, beforeEach, describe, expect, it } from 'vitest' |
| 7 | +import { CheckableContinuousFlyout } from '../../src/checkable_continuous_flyout' |
| 8 | +import { registerRecyclableBlockFlyoutInflater } from '../../src/recyclable_block_flyout_inflater' |
| 9 | +import { registerScratchBlockPaster } from '../../src/scratch_block_paster' |
| 10 | + |
| 11 | +// Browser tests for the shared-shadow-ID bug (forum topic 878291). |
| 12 | +// Both the duplicate/paste path and the flyout copy path must produce |
| 13 | +// blocks with unique shadow IDs. Without stripIds, disposed shadows |
| 14 | +// from the original reuse their IDs in the copy, causing the VM to |
| 15 | +// think both blocks share the same shadow. |
| 16 | + |
| 17 | +const BLOCK_TYPES = ['test_value_block', 'test_text_shadow', 'test_reporter'] |
| 18 | + |
| 19 | +let container: HTMLElement | undefined |
| 20 | +let workspace: Blockly.WorkspaceSvg | undefined |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + container = document.createElement('div') |
| 24 | + container.style.width = '800px' |
| 25 | + container.style.height = '600px' |
| 26 | + document.body.appendChild(container) |
| 27 | + |
| 28 | + Blockly.defineBlocksWithJsonArray([ |
| 29 | + { |
| 30 | + type: 'test_value_block', |
| 31 | + message0: 'set to %1', |
| 32 | + args0: [{ type: 'input_value', name: 'VALUE' }], |
| 33 | + previousStatement: null, |
| 34 | + nextStatement: null, |
| 35 | + }, |
| 36 | + { |
| 37 | + type: 'test_text_shadow', |
| 38 | + message0: '%1', |
| 39 | + args0: [{ type: 'field_input', name: 'TEXT' }], |
| 40 | + output: 'String', |
| 41 | + }, |
| 42 | + { |
| 43 | + type: 'test_reporter', |
| 44 | + message0: 'answer', |
| 45 | + output: 'String', |
| 46 | + }, |
| 47 | + ]) |
| 48 | +}) |
| 49 | + |
| 50 | +// Save the default Blockly implementations so we can restore them after |
| 51 | +// all tests, preventing cross-suite leakage of Scratch-specific overrides. |
| 52 | +const DefaultInflater = Blockly.registry.getClass(Blockly.registry.Type.FLYOUT_INFLATER, 'block') |
| 53 | + |
| 54 | +beforeAll(() => { |
| 55 | + registerScratchBlockPaster() |
| 56 | + registerRecyclableBlockFlyoutInflater() |
| 57 | +}) |
| 58 | + |
| 59 | +afterAll(() => { |
| 60 | + // Restore the default block paster |
| 61 | + Blockly.clipboard.registry.unregister(Blockly.clipboard.BlockPaster.TYPE) |
| 62 | + Blockly.clipboard.registry.register(Blockly.clipboard.BlockPaster.TYPE, new Blockly.clipboard.BlockPaster()) |
| 63 | + // Restore the default flyout inflater |
| 64 | + if (DefaultInflater) { |
| 65 | + Blockly.registry.unregister(Blockly.registry.Type.FLYOUT_INFLATER, 'block') |
| 66 | + Blockly.registry.register(Blockly.registry.Type.FLYOUT_INFLATER, 'block', DefaultInflater) |
| 67 | + } |
| 68 | +}) |
| 69 | + |
| 70 | +afterEach(() => { |
| 71 | + workspace?.dispose() |
| 72 | + container?.remove() |
| 73 | + for (const t of BLOCK_TYPES) { |
| 74 | + delete Blockly.Blocks[t] |
| 75 | + } |
| 76 | +}) |
| 77 | + |
| 78 | +describe('duplicate block shadow IDs (forum topic 878291)', () => { |
| 79 | + it('duplicated block gets unique shadow IDs, not shared with original', () => { |
| 80 | + assert(container, 'Expected container from beforeEach') |
| 81 | + workspace = Blockly.inject(container, {}) |
| 82 | + |
| 83 | + // Create the original block with a shadow on VALUE |
| 84 | + let original: Blockly.BlockSvg |
| 85 | + let reporter: Blockly.BlockSvg |
| 86 | + Blockly.Events.disable() |
| 87 | + try { |
| 88 | + original = Blockly.serialization.blocks.append( |
| 89 | + { |
| 90 | + type: 'test_value_block', |
| 91 | + inputs: { |
| 92 | + VALUE: { |
| 93 | + shadow: { |
| 94 | + type: 'test_text_shadow', |
| 95 | + fields: { TEXT: '0' }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + workspace, |
| 101 | + ) as Blockly.BlockSvg |
| 102 | + |
| 103 | + // Connect a reporter to obscure the shadow |
| 104 | + reporter = workspace.newBlock('test_reporter') |
| 105 | + } finally { |
| 106 | + Blockly.Events.enable() |
| 107 | + } |
| 108 | + |
| 109 | + const conn = original.getInput('VALUE')?.connection |
| 110 | + assert(conn, 'Expected VALUE connection') |
| 111 | + const reporterOutput = reporter.outputConnection |
| 112 | + assert(reporterOutput, 'Expected reporter output connection') |
| 113 | + conn.connect(reporterOutput) |
| 114 | + |
| 115 | + // Capture the original's shadow state before duplication |
| 116 | + const originalShadowState = conn.getShadowState() |
| 117 | + assert(originalShadowState, 'Expected shadow state on original after connecting reporter') |
| 118 | + const originalShadowId = originalShadowState.id |
| 119 | + assert(originalShadowId, 'Expected shadow state to have an ID') |
| 120 | + |
| 121 | + // Duplicate via the actual clipboard path (toCopyData + paste) |
| 122 | + const copyData = original.toCopyData() |
| 123 | + assert(copyData, 'Expected toCopyData to return data') |
| 124 | + const copyResult = Blockly.clipboard.paste(copyData, workspace) |
| 125 | + assert(copyResult, 'Expected paste to return a block') |
| 126 | + const copy = copyResult as unknown as Blockly.BlockSvg |
| 127 | + |
| 128 | + // The copy's shadow should have a DIFFERENT ID than the original's |
| 129 | + const copyConn = copy.getInput('VALUE')?.connection |
| 130 | + assert(copyConn, 'Expected VALUE connection on copy') |
| 131 | + const copyShadowState = copyConn.getShadowState() |
| 132 | + assert(copyShadowState, 'Expected shadow state on copy') |
| 133 | + |
| 134 | + expect(copyShadowState.id).not.toBe(originalShadowId) |
| 135 | + |
| 136 | + // Verify: deleting the copy does not affect the original's shadow |
| 137 | + copy.dispose() |
| 138 | + reporterOutput.disconnect() |
| 139 | + const respawned = conn.targetBlock() |
| 140 | + assert(respawned, 'Original shadow should respawn after copy is deleted') |
| 141 | + expect(respawned.isShadow()).toBe(true) |
| 142 | + expect(respawned.type).toBe('test_text_shadow') |
| 143 | + }) |
| 144 | + |
| 145 | + it('two flyout copies get unique shadow IDs when first copy shadow is obscured', () => { |
| 146 | + assert(container, 'Expected container from beforeEach') |
| 147 | + // Inject with CheckableContinuousFlyout (which has the stripIds fix) |
| 148 | + // and a toolbox that includes a block with a shadow input. |
| 149 | + workspace = Blockly.inject(container, { |
| 150 | + toolbox: { |
| 151 | + kind: 'flyoutToolbox', |
| 152 | + contents: [ |
| 153 | + { |
| 154 | + kind: 'block', |
| 155 | + type: 'test_value_block', |
| 156 | + inputs: { |
| 157 | + VALUE: { |
| 158 | + shadow: { |
| 159 | + type: 'test_text_shadow', |
| 160 | + fields: { TEXT: '0' }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + ], |
| 166 | + }, |
| 167 | + plugins: { |
| 168 | + flyoutsVerticalToolbox: CheckableContinuousFlyout, |
| 169 | + }, |
| 170 | + }) |
| 171 | + |
| 172 | + const flyout = workspace.getFlyout() |
| 173 | + assert(flyout, 'Expected workspace to have a flyout') |
| 174 | + |
| 175 | + // Find the template block in the flyout |
| 176 | + const flyoutBlocks = flyout.getWorkspace().getAllBlocks(false) |
| 177 | + const template = flyoutBlocks.find((b) => b.type === 'test_value_block') |
| 178 | + assert(template, 'Expected template block in flyout') |
| 179 | + |
| 180 | + // First copy from flyout |
| 181 | + const copy1 = flyout.createBlock(template) |
| 182 | + const conn1 = copy1.getInput('VALUE')?.connection |
| 183 | + assert(conn1, 'Expected VALUE connection on first copy') |
| 184 | + const shadow1 = conn1.targetBlock() |
| 185 | + assert(shadow1, 'Expected shadow on first copy') |
| 186 | + expect(shadow1.isShadow()).toBe(true) |
| 187 | + const shadow1Id = shadow1.id |
| 188 | + |
| 189 | + // Obscure the first copy's shadow by connecting a reporter |
| 190 | + let reporter: Blockly.BlockSvg |
| 191 | + Blockly.Events.disable() |
| 192 | + try { |
| 193 | + reporter = workspace.newBlock('test_reporter') |
| 194 | + } finally { |
| 195 | + Blockly.Events.enable() |
| 196 | + } |
| 197 | + const reporterOutput = reporter.outputConnection |
| 198 | + assert(reporterOutput, 'Expected reporter output connection') |
| 199 | + conn1.connect(reporterOutput) |
| 200 | + |
| 201 | + // Second copy from flyout — its shadow must get a different ID |
| 202 | + const copy2 = flyout.createBlock(template) |
| 203 | + const conn2 = copy2.getInput('VALUE')?.connection |
| 204 | + assert(conn2, 'Expected VALUE connection on second copy') |
| 205 | + const shadow2 = conn2.targetBlock() |
| 206 | + assert(shadow2, 'Expected shadow on second copy') |
| 207 | + expect(shadow2.isShadow()).toBe(true) |
| 208 | + |
| 209 | + expect(shadow2.id).not.toBe(shadow1Id) |
| 210 | + }) |
| 211 | +}) |
0 commit comments