|
| 1 | +// Copyright 2026 The MathWorks, Inc. |
| 2 | +// |
| 3 | +// Set as Active Configuration in the model editor (MatEditorProvider's MODEL_EDITOR): the message |
| 4 | +// the table's menu sends (setActiveConfigSet) reaches the model's engine, makes the set active as |
| 5 | +// one edit on VS Code's stack, undone and redone with it, and a save writes it into the file, every |
| 6 | +// other part as read. A reference to a set is made active too, with VS Code's warning of what that |
| 7 | +// cannot check without MATLAB (read off `vscode.window.showWarningMessage`, wrapped for this suite: |
| 8 | +// the extension's `vscode` module is one object for every file of it). Driven through the provider |
| 9 | +// with a fake panel, as the MAT-file editor's suite is (matEdit.test.ts). |
| 10 | +import * as assert from 'assert'; |
| 11 | +import * as vscode from 'vscode'; |
| 12 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs'; |
| 13 | +import { join } from 'path'; |
| 14 | +import { tmpdir } from 'os'; |
| 15 | +import { unzipSync } from 'fflate'; |
| 16 | +import { parseSlx } from 'data-explorer-core'; |
| 17 | +import { MatEditorProvider, MODEL_EDITOR } from '../../src/host/MatEditorProvider'; |
| 18 | +import { ctx, fakePanel, nameOf, quiet, token, waitFor } from './tools/hostHarness'; |
| 19 | + |
| 20 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 21 | + |
| 22 | +const fixture = (name: string): Uint8Array => |
| 23 | + new Uint8Array(readFileSync(join(ctx().extensionUri.fsPath, 'test-integration', 'fixtures', 'active-config', name))); |
| 24 | +const buffer = (b: Uint8Array) => b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength) as ArrayBuffer; |
| 25 | +const activeIn = (b: Uint8Array) => parseSlx(buffer(b), 'saved.slx').configSets.filter((c: any) => c.active).map((c: any) => c.name); |
| 26 | +const same = (a: Uint8Array, b: Uint8Array) => a.byteLength === b.byteLength && a.every((x, i) => x === b[i]); |
| 27 | + |
| 28 | +/** The rows the table holds now: the last setRows with every updateEntryRows after it spliced in. */ |
| 29 | +function tableOf(posts: any[]): any[] { |
| 30 | + let rows: any[] = []; |
| 31 | + for (const p of posts) { |
| 32 | + if (p.type === 'setRows') rows = p.rows; |
| 33 | + else if (p.type === 'updateEntryRows') { |
| 34 | + const start = rows.findIndex((r) => r.ID === p.entryRowId); |
| 35 | + if (start < 0) continue; |
| 36 | + const subtree = new Set([p.entryRowId]); |
| 37 | + let end = start + 1; |
| 38 | + while (end < rows.length && subtree.has(rows[end].parent)) subtree.add(rows[end++].ID); |
| 39 | + rows = [...rows.slice(0, start), ...p.rows, ...rows.slice(end)]; |
| 40 | + } |
| 41 | + } |
| 42 | + return rows; |
| 43 | +} |
| 44 | + |
| 45 | +const setRow = (posts: any[], name: string): any => { |
| 46 | + const row = tableOf(posts).find((r) => r.parent === 'section:config' && nameOf(r) === name); |
| 47 | + assert.ok(row, `the Configurations section has a row for "${name}"`); |
| 48 | + return row; |
| 49 | +}; |
| 50 | + |
| 51 | +suite('Model editor: Set as Active Configuration', () => { |
| 52 | + let dir: string; |
| 53 | + let provider: MatEditorProvider; |
| 54 | + const opened: { doc: any; view: ReturnType<typeof fakePanel> }[] = []; |
| 55 | + const warned: string[] = []; |
| 56 | + const window = vscode.window as any; |
| 57 | + const warn = window.showWarningMessage; |
| 58 | + |
| 59 | + setup(() => { |
| 60 | + dir = mkdtempSync(join(tmpdir(), 'dex-activeconfig-')); |
| 61 | + provider = new MatEditorProvider(ctx(), MODEL_EDITOR); |
| 62 | + warned.length = 0; |
| 63 | + window.showWarningMessage = (message: string) => { |
| 64 | + warned.push(message); |
| 65 | + return Promise.resolve(undefined); |
| 66 | + }; |
| 67 | + }); |
| 68 | + |
| 69 | + teardown(async () => { |
| 70 | + window.showWarningMessage = warn; |
| 71 | + for (const { doc, view } of opened.splice(0)) { |
| 72 | + view.close(); |
| 73 | + doc.dispose(); |
| 74 | + } |
| 75 | + await quiet(200, 'the disposed watchers to stop watching before their directory goes'); |
| 76 | + rmSync(dir, { recursive: true, force: true }); |
| 77 | + }); |
| 78 | + |
| 79 | + async function open(name: string) { |
| 80 | + const path = join(dir, name); |
| 81 | + const original = fixture(name); |
| 82 | + writeFileSync(path, original); |
| 83 | + const doc: any = await provider.openCustomDocument(vscode.Uri.file(path), {} as vscode.CustomDocumentOpenContext, token()); |
| 84 | + const view = fakePanel(); |
| 85 | + opened.push({ doc, view }); |
| 86 | + const events: vscode.CustomDocumentEditEvent<vscode.CustomDocument>[] = []; |
| 87 | + provider.onDidChangeCustomDocument((e) => { |
| 88 | + if (e.document === doc && 'undo' in e) events.push(e); |
| 89 | + }); |
| 90 | + await provider.resolveCustomEditor(doc, view.panel, token()); |
| 91 | + view.send({ type: 'ready' }); |
| 92 | + await waitFor(`${name}'s first rows`, () => view.posts.some((p) => p.type === 'setRows' || p.type === 'error')); |
| 93 | + return { path, doc, view, events, original }; |
| 94 | + } |
| 95 | + |
| 96 | + const painted = (o: { view: any }, name: string) => |
| 97 | + waitFor(`${name} painted active`, () => setRow(o.view.posts, name).Name.iconId === 'check_settings'); |
| 98 | + |
| 99 | + test('a set made active is one edit, undone and redone with VS Code’s stack, and saved', async () => { |
| 100 | + const o = await open('acs_sets.slx'); |
| 101 | + assert.strictEqual(setRow(o.view.posts, 'Configuration')._canSetActive, false); |
| 102 | + assert.strictEqual(setRow(o.view.posts, 'Fast')._canSetActive, true); |
| 103 | + o.view.send({ type: 'setActiveConfigSet', rowId: setRow(o.view.posts, 'Fast').ID }); |
| 104 | + await painted(o, 'Fast'); |
| 105 | + assert.strictEqual(o.events.length, 1, 'one edit on the stack'); |
| 106 | + assert.strictEqual(o.doc.edits.dirty, true); |
| 107 | + await o.events[0].undo(); |
| 108 | + await painted(o, 'Configuration'); |
| 109 | + assert.strictEqual(o.doc.edits.dirty, false, 'undone: clean'); |
| 110 | + await o.events[0].redo(); |
| 111 | + await painted(o, 'Fast'); |
| 112 | + await provider.saveCustomDocument(o.doc, token()); |
| 113 | + const saved = new Uint8Array(readFileSync(o.path)); |
| 114 | + assert.deepStrictEqual(activeIn(saved), ['Fast']); |
| 115 | + const read = unzipSync(o.original); |
| 116 | + const written = unzipSync(saved); |
| 117 | + const changed = Object.keys(read).filter((n) => !same(read[n], written[n])); |
| 118 | + assert.deepStrictEqual(changed, ['simulink/configSetInfo.json'], 'the mark alone changed'); |
| 119 | + assert.strictEqual(o.doc.edits.dirty, false); |
| 120 | + }); |
| 121 | + |
| 122 | + test('a reference to a set is made active, with a warning that MATLAB alone can check it, and saved', async () => { |
| 123 | + const o = await open('acs_ref.slx'); |
| 124 | + const ref = setRow(o.view.posts, 'RefFromDict'); |
| 125 | + assert.strictEqual(ref._canSetActive, true); |
| 126 | + o.view.send({ type: 'setActiveConfigSet', rowId: ref.ID }); |
| 127 | + await waitFor('the reference painted active', () => setRow(o.view.posts, 'RefFromDict').Name.iconId === 'check_configurationReference'); |
| 128 | + assert.strictEqual(o.events.length, 1); |
| 129 | + assert.deepStrictEqual(warned, [ |
| 130 | + '"RefFromDict" refers to the configuration set "dictCfg" in the data dictionary "acs_dict.sldd". Without MATLAB this editor ' + |
| 131 | + 'cannot check that the set is there: open the model in MATLAB to confirm.', |
| 132 | + ]); |
| 133 | + await provider.saveCustomDocument(o.doc, token()); |
| 134 | + assert.deepStrictEqual(activeIn(new Uint8Array(readFileSync(o.path))), ['RefFromDict']); |
| 135 | + }); |
| 136 | +}); |
0 commit comments