File size: 12,792 Bytes
f61bac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026, The OpenROAD Authors

import './setup-dom.js';
import { describe, it, beforeEach } from 'node:test';
import assert from 'node:assert/strict';
import {
    showGlobalConnectDialog,
    showInsertBufferDialog,
} from '../../src/edit-dialogs.js';

function waitMicrotasks() {
    return new Promise(resolve => setTimeout(resolve, 0));
}

// Build an app whose websocketManager.request is driven by `responder`.
function makeApp(responder) {
    const requests = [];
    return {
        requests,
        scheduleRedrawAllLayers() { this._redrew = true; },
        websocketManager: {
            request(msg) {
                requests.push(msg);
                return Promise.resolve(responder(msg) ?? {});
            },
        },
    };
}

describe('Global Connect dialog', () => {
    beforeEach(() => { document.body.innerHTML = ''; });

    it('lists rules, populates nets, and blocks Add on invalid regex', async () => {
        const app = makeApp(msg => {
            if (msg.type === 'global_connect_info') {
                return {
                    rules: [{ inst: '.*', pin: '^VDD$', net: 'VDD', region: '' }],
                    nets: ['VDD', 'VSS'],
                    regions: [],
                };
            }
            return {};
        });
        showGlobalConnectDialog(app);
        await waitMicrotasks();

        // Existing rule rendered (head row + 1 data row).
        assert.equal(document.querySelectorAll('.gc-row').length, 2);
        // Net dropdown populated with special nets.
        const netSel = document.querySelectorAll('.gc-form select')[0];
        assert.deepEqual([...netSel.options].map(o => o.value), ['VDD', 'VSS']);

        const pin = document.querySelectorAll('.gc-form input')[1];
        const addBtn = [...document.querySelectorAll('.gc-form button')]
            .find(b => b.textContent === 'Add rule');
        // Empty pin → disabled.
        assert.equal(addBtn.disabled, true);
        pin.value = '[';  // invalid regex
        pin.dispatchEvent(new Event('input', { bubbles: true }));
        assert.equal(addBtn.disabled, true);
        pin.value = '^VDD$';
        pin.dispatchEvent(new Event('input', { bubbles: true }));
        assert.equal(addBtn.disabled, false);
    });

    it('Add builds the add_global_connection command', async () => {
        const app = makeApp(msg => {
            if (msg.type === 'global_connect_info') {
                return { rules: [], nets: ['VDD'], regions: [] };
            }
            if (msg.type === 'tcl_eval') return { result: 'Connected 3 pins', is_error: false };
            return {};
        });
        showGlobalConnectDialog(app);
        await waitMicrotasks();

        const pin = document.querySelectorAll('.gc-form input')[1];
        pin.value = '^VDD$';
        pin.dispatchEvent(new Event('input', { bubbles: true }));
        [...document.querySelectorAll('.gc-form button')]
            .find(b => b.textContent === 'Add rule').click();
        await waitMicrotasks();

        const ev = app.requests.find(r => r.type === 'tcl_eval');
        assert.equal(ev.cmd,
            'add_global_connection -net {VDD} -inst_pattern {.*} -pin_pattern {^VDD$} -power');
    });

    it('Apply (force) shows the connected count via global_connect_apply', async () => {
        const app = makeApp(msg => {
            if (msg.type === 'global_connect_info') return { rules: [], nets: ['VDD'], regions: [] };
            if (msg.type === 'global_connect_apply') return { ok: true, had_rules: true, connected: 7 };
            return {};
        });
        showGlobalConnectDialog(app);
        await waitMicrotasks();
        [...document.querySelectorAll('.modal-buttons button')]
            .find(b => b.textContent === 'Apply (force)').click();
        await waitMicrotasks();
        const applyReq = app.requests.find(r => r.type === 'global_connect_apply');
        assert.equal(applyReq.force, true);
        assert.match(document.querySelector('.modal-error').textContent, /Connected 7 pin/);
    });

    it('Apply (force) with no rules shows a friendly message (no ODB-0378)', async () => {
        const app = makeApp(msg => {
            if (msg.type === 'global_connect_info') return { rules: [], nets: [], regions: [] };
            if (msg.type === 'global_connect_apply') return { ok: true, had_rules: false, connected: 0 };
            return {};
        });
        showGlobalConnectDialog(app);
        await waitMicrotasks();
        [...document.querySelectorAll('.modal-buttons button')]
            .find(b => b.textContent === 'Apply (force)').click();
        await waitMicrotasks();
        assert.match(document.querySelector('.modal-error').textContent, /No global-connect rules/);
    });

    it('per-row delete calls global_connect_delete with the rule fields', async () => {
        const app = makeApp(msg => {
            if (msg.type === 'global_connect_info') {
                return { rules: [{ inst: '.*', pin: 'P', net: 'VDD', region: '' }], nets: ['VDD'], regions: [] };
            }
            return {};
        });
        showGlobalConnectDialog(app);
        await waitMicrotasks();
        document.querySelector('.gc-row:not(.gc-head) button').click();
        await waitMicrotasks();
        const del = app.requests.find(r => r.type === 'global_connect_delete');
        assert.equal('index' in del, false);
        assert.deepEqual(
            { inst: del.inst, pin: del.pin, net: del.net, region: del.region },
            { inst: '.*', pin: 'P', net: 'VDD', region: '' });
    });
});

describe('Insert Buffer dialog', () => {
    beforeEach(() => { document.body.innerHTML = ''; });

    it('disables OK and shows a message when the net cannot be buffered', async () => {
        const app = makeApp(() => ({ can_buffer: false, error: 'multiple drivers' }));
        showInsertBufferDialog(app, 'n1');
        await waitMicrotasks();
        assert.match(document.querySelector('.modal-body').textContent, /multiple drivers/);
        const ok = [...document.querySelectorAll('.modal-buttons button')]
            .find(b => b.textContent === 'Insert');
        assert.equal(ok.disabled, true);
    });

    it('sends insert_buffer with the driver by default', async () => {
        const app = makeApp(msg => {
            if (msg.type === 'buffer_info') {
                return {
                    can_buffer: true,
                    drivers: [{ id: 'I:drv/Z', label: 'drv/Z (Driver)' }],
                    loads: [{ id: 'I:ld/A', label: 'ld/A (Load)' }],
                    masters: ['BUF_X1', 'BUF_X2'],
                };
            }
            if (msg.type === 'insert_buffer') return { ok: true, inst: 'buf0' };
            return {};
        });
        showInsertBufferDialog(app, 'n1');
        await waitMicrotasks();

        const ok = [...document.querySelectorAll('.modal-buttons button')]
            .find(b => b.textContent === 'Insert');
        assert.equal(ok.disabled, false);
        ok.click();
        await waitMicrotasks();

        const ins = app.requests.find(r => r.type === 'insert_buffer');
        assert.equal(ins.mode, 'driver');
        assert.deepEqual(ins.pins, ['I:drv/Z']);
        assert.equal(ins.master, 'BUF_X1');
        assert.equal(app.selectedInstanceName, 'buf0');
        assert.equal(app._redrew, true);
    });

    it('switches to loads mode when a load pin is picked', async () => {
        const app = makeApp(msg => {
            if (msg.type === 'buffer_info') {
                return {
                    can_buffer: true,
                    drivers: [{ id: 'I:drv/Z', label: 'drv/Z (Driver)' }],
                    loads: [{ id: 'I:ld/A', label: 'ld/A (Load)' }],
                    masters: ['BUF_X1'],
                };
            }
            if (msg.type === 'insert_buffer') return { ok: true, inst: 'buf0' };
            return {};
        });
        showInsertBufferDialog(app, 'n1');
        await waitMicrotasks();

        // Check the load pin (second checkbox) → driver auto-deselects.
        const boxes = document.querySelectorAll('.buf-pin input');
        boxes[1].checked = true;
        boxes[1].dispatchEvent(new Event('change', { bubbles: true }));
        await waitMicrotasks();

        [...document.querySelectorAll('.modal-buttons button')]
            .find(b => b.textContent === 'Insert').click();
        await waitMicrotasks();

        const ins = app.requests.find(r => r.type === 'insert_buffer');
        assert.equal(ins.mode, 'loads');
        assert.deepEqual(ins.pins, ['I:ld/A']);
    });

    it('does not send x/y and has no position/move UI', async () => {
        const app = makeApp(msg => {
            if (msg.type === 'buffer_info') {
                return {
                    can_buffer: true,
                    drivers: [{ id: 'I:drv/Z', label: 'drv/Z (Driver)' }],
                    loads: [],
                    masters: ['BUF_X1'],
                };
            }
            if (msg.type === 'insert_buffer') return { ok: true, inst: 'buf0' };
            return {};
        });
        showInsertBufferDialog(app, 'n1');
        await waitMicrotasks();
        [...document.querySelectorAll('.modal-buttons button')]
            .find(b => b.textContent === 'Insert').click();
        await waitMicrotasks();

        const ins = app.requests.find(r => r.type === 'insert_buffer');
        assert.equal('x' in ins, false);
        assert.equal('y' in ins, false);
        // No location/move UI remains in the dialog.
        assert.equal(document.querySelectorAll('.buf-dialog input[type="number"]').length, 0);
        assert.equal([...document.querySelectorAll('.buf-dialog button')]
            .some(b => b.textContent === 'Pick location'), false);
    });
});

describe('Editing dialog teardown', () => {
    beforeEach(() => { document.body.innerHTML = ''; });

    // The modal installs a document-level keydown listener for Escape.  Every
    // way out of the dialog has to take it back off again, or each open/close
    // cycle leaves another handler behind holding the dialog's closure alive.
    for (const exit of ['Close button', 'overlay click', 'Escape']) {
        it(`removes its keydown listener on exit via ${exit}`, async () => {
            const app = makeApp(msg => {
                if (msg.type === 'global_connect_info') {
                    return { rules: [], nets: [], regions: [] };
                }
                return {};
            });

            const added = [];
            const removed = [];
            const origAdd = document.addEventListener;
            const origRemove = document.removeEventListener;
            document.addEventListener = function (type, fn, opts) {
                if (type === 'keydown') added.push(fn);
                return origAdd.call(this, type, fn, opts);
            };
            document.removeEventListener = function (type, fn, opts) {
                if (type === 'keydown') removed.push(fn);
                return origRemove.call(this, type, fn, opts);
            };
            try {
                showGlobalConnectDialog(app);
                await waitMicrotasks();
                assert.equal(added.length, 1, 'one keydown listener installed');

                if (exit === 'Close button') {
                    [...document.querySelectorAll('.gc-dialog button')]
                        .find(b => b.textContent === 'Close').click();
                } else if (exit === 'overlay click') {
                    const overlay = document.querySelector('.modal-overlay');
                    overlay.dispatchEvent(new window.MouseEvent('click',
                                                                { bubbles: true }));
                } else {
                    document.dispatchEvent(new window.KeyboardEvent(
                        'keydown', { key: 'Escape', bubbles: true }));
                }
                await waitMicrotasks();

                assert.equal(document.querySelector('.modal-overlay'), null,
                             'dialog closed');
                // Only this dialog's listener is asserted on: earlier tests in
                // this file leave their dialogs open on the shared jsdom
                // document, so an Escape reaches their handlers too and they
                // unregister themselves as well.
                assert.equal(removed.filter(fn => fn === added[0]).length, 1,
                             'the installed listener was removed exactly once');
            } finally {
                document.addEventListener = origAdd;
                document.removeEventListener = origRemove;
            }
        });
    }
});