File size: 8,961 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
// 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 { SelectionBrowser } from '../../src/selection-browser.js';

const PALETTE = Array.from({ length: 16 }, (_, i) => [i, 2 * i, 3 * i, 100]);

const LIST = {
    ok: 1,
    truncated: false,
    selection: [
        { name: 'inst_a', type: 'Inst', bbox: [0, 0, 100, 100] },
        { name: 'net_b', type: 'Net', bbox: [10, 10, 50, 50] },
    ],
    groups: Array.from({ length: 16 }, (_, g) =>
        (g === 4 ? [{ name: 'inst_c', type: 'Inst',
                      bbox: [5, 5, 20, 20] }] : [])),
};

function createMockApp(responses = {}) {
    const requests = [];
    return {
        websocketManager: {
            request(msg) {
                requests.push(msg);
                const type = msg.type;
                if (responses[type]) {
                    return Promise.resolve(responses[type](msg));
                }
                return Promise.resolve({ ok: 1 });
            },
        },
        techData: { highlight_colors: PALETTE },
        designScale: 1,
        designMaxDXDY: 100000,
        designOriginX: 0,
        designOriginY: 0,
        showDbu: true,
        getDbuPerMicron() { return 1000; },
        map: { fitBounds() {} },
        focusComponent() {},
        updateInspector() {},
        _requests: requests,
    };
}

function makeContainer() {
    return { element: document.createElement('div'), on() {} };
}

const settle = () => new Promise(r => setTimeout(r, 0));

describe('SelectionBrowser', () => {
    let app, browser, container;

    beforeEach(async () => {
        app = createMockApp({ list_selection: () => LIST });
        container = makeContainer();
        browser = new SelectionBrowser(container, app, () => {});
        await settle();
    });

    it('renders tabs with live counters', () => {
        const tabs = container.element.querySelectorAll('.sb-tab');
        assert.equal(tabs.length, 2);
        assert.equal(tabs[0].textContent, 'Selected (2)');
        assert.equal(tabs[1].textContent, 'Highlighted (1)');
    });

    it('lists selection rows with bounds and actions', () => {
        const rows = container.element.querySelectorAll('.sb-row');
        assert.equal(rows.length, 2);
        assert.ok(rows[0].textContent.includes('inst_a'));
        assert.ok(rows[0].querySelector('.sb-row-btn[title="De-select"]'));
    });

    it('filters by substring across name and type', () => {
        const filter = container.element.querySelector('.sb-filter');
        filter.value = 'net';
        filter.dispatchEvent(new window.Event('input'));
        const rows = container.element.querySelectorAll('.sb-row');
        assert.equal(rows.length, 1);
        assert.ok(rows[0].textContent.includes('net_b'));
    });

    it('row click sends inspect_selection with the stable index', async () => {
        app._requests.length = 0;
        container.element.querySelectorAll('.sb-row')[1].click();
        await settle();
        assert.equal(app._requests[0].type, 'inspect_selection');
        assert.equal(app._requests[0].index, 1);
    });

    it('de-select button sends deselect and refreshes', async () => {
        app._requests.length = 0;
        container.element
            .querySelector('.sb-row-btn[title="De-select"]').click();
        await settle();
        assert.equal(app._requests[0].type, 'deselect');
        assert.equal(app._requests[0].index, 0);
        assert.ok(app._requests.some(r => r.type === 'list_selection'),
                  'refreshes the list after removal');
    });

    it('highlighted tab shows the group chip and inspects by group', async () => {
        const tabs = container.element.querySelectorAll('.sb-tab');
        tabs[1].click();
        const chip = container.element.querySelector('.sb-chip');
        assert.ok(chip, 'group color chip rendered');
        const c = PALETTE[4];
        assert.equal(chip.style.backgroundColor,
                     `rgb(${c[0]}, ${c[1]}, ${c[2]})`);
        assert.ok(container.element.textContent.includes('Group 5'));

        app._requests.length = 0;
        container.element.querySelector('.sb-row').click();
        await settle();
        assert.equal(app._requests[0].type, 'inspect_group');
        assert.equal(app._requests[0].group, 4);
        assert.equal(app._requests[0].index, 0);
    });

    it('sorting keeps actions on the right object', async () => {
        // Sort by name descending: net_b first, inst_a second.
        const nameTh = container.element.querySelector('th.sb-sortable');
        nameTh.click();  // asc
        nameTh.click();  // desc
        const rows = container.element.querySelectorAll('.sb-row');
        assert.ok(rows[0].textContent.includes('net_b'));

        app._requests.length = 0;
        rows[0].click();
        await settle();
        // net_b is index 1 in fetch order — the sort must not change that.
        assert.equal(app._requests[0].index, 1);
    });

    it('shows the truncation notice when the server capped the list', async () => {
        app = createMockApp({
            list_selection: () => ({ ...LIST, truncated: true }),
        });
        container = makeContainer();
        browser = new SelectionBrowser(container, app, () => {});
        await settle();
        assert.ok(container.element.querySelector('.sb-truncated'));
    });

    it('row hover starts a continuous animation and mouseleave stops it', () => {
        const calls = [];
        app.animateSelection = (bbox, opts) => calls.push(['start', bbox, opts]);
        app.stopSelectionAnimation = () => calls.push(['stop']);

        const row = container.element.querySelector('tbody tr');
        row.dispatchEvent(new Event('mouseenter'));
        assert.deepEqual(calls[0],
                         ['start', [0, 0, 100, 100], { repeats: 0 }]);
        row.dispatchEvent(new Event('mouseleave'));
        assert.deepEqual(calls[1], ['stop']);
    });
});

describe('SelectionBrowser lifecycle', () => {
    // Container that records GoldenLayout handlers so tests can fire them.
    function makeCapturingContainer() {
        const handlers = {};
        return {
            element: document.createElement('div'),
            on(evt, cb) { handlers[evt] = cb; },
            fire(evt) { if (handlers[evt]) handlers[evt](); },
        };
    }

    it('skips refresh while hidden', async () => {
        const app = createMockApp({ list_selection: () => LIST });
        const container = makeCapturingContainer();
        const browser = new SelectionBrowser(container, app, () => {});
        await settle();
        const before = app._requests.length;

        container.fire('hide');
        browser.refresh();
        await settle();
        assert.equal(app._requests.length, before,
                     'no list_selection request while hidden');
    });

    it('re-runs a refresh that arrived while one was in flight', async () => {
        // A mutation refreshing mid-request would otherwise be dropped, and
        // the table -- and the row indices inspect/deselect send back --
        // would keep describing the pre-mutation set.
        let release;
        const gate = new Promise(r => { release = r; });
        let calls = 0;
        const app = createMockApp();
        app.websocketManager.request = (msg) => {
            app._requests.push(msg);
            if (msg.type !== 'list_selection') return Promise.resolve({ ok: 1 });
            calls += 1;
            return calls === 1 ? gate.then(() => LIST) : Promise.resolve(LIST);
        };
        const container = makeContainer();
        const browser = new SelectionBrowser(container, app, () => {});
        assert.equal(calls, 1, 'the constructor issued the first fetch');

        // Second refresh lands while the first is still outstanding.
        browser.refresh();
        assert.equal(calls, 1, 'no second request while one is in flight');
        assert.ok(browser._staleWhileInFlight, 'the skipped refresh is recorded');

        release();
        await settle();
        assert.equal(calls, 2, 'the skipped refresh re-runs once the first lands');
        assert.equal(browser._staleWhileInFlight, false);
        assert.equal(browser._inFlight, false);
    });

    it('clears the pending debounced refresh on destroy', () => {
        const app = createMockApp({ list_selection: () => LIST });
        const container = makeCapturingContainer();
        const browser = new SelectionBrowser(container, app, () => {});

        browser.scheduleRefresh();
        assert.notEqual(browser._pendingRefresh, null,
                        'a refresh is pending');
        container.fire('destroy');
        assert.equal(browser._pendingRefresh, null,
                     'destroy cleared the pending timer');
    });
});