Skip to content

Commit d610a14

Browse files
authored
fix: audit findings for the panel shell, hub, extension and native overlays (#364)
Fixes confirmed audit findings from #353, #354, #356 and #357. The issues stay open for the items that need a product decision or a larger change.
1 parent 1cca086 commit d610a14

72 files changed

Lines changed: 2385 additions & 439 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import { TestBed, type ComponentFixture } from '@angular/core/testing';
2+
import type { DevframeRpcClient } from 'devframe/client';
3+
import { afterEach, describe, expect, it, vi } from 'vitest';
4+
5+
const connection = vi.hoisted(() => ({
6+
attempts: 0,
7+
/** What each attempt does: `true` connects, `false` fails. */
8+
answers: [] as boolean[],
9+
/** Sends a `connection:status` event to the last connected client's listeners. */
10+
emitStatus: (_status: string) => {},
11+
}));
12+
13+
function fakeClient(): DevframeRpcClient {
14+
const rpc = {
15+
call: () => new Promise(() => {}),
16+
callEvent: () => Promise.resolve(),
17+
sharedState: () => Promise.resolve({ value: () => null, on: () => () => {} }),
18+
};
19+
const listeners: ((status: string) => void)[] = [];
20+
connection.emitStatus = (status) => listeners.forEach((listener) => listener(status));
21+
return {
22+
connectionMeta: { configs: {} },
23+
scope: () => ({ rpc }),
24+
events: {
25+
on: (name: string, listener: (status: string) => void) => {
26+
if (name === 'connection:status') listeners.push(listener);
27+
return () => {};
28+
},
29+
},
30+
close: () => {},
31+
} as unknown as DevframeRpcClient;
32+
}
33+
34+
vi.mock('devframe/client', () => ({
35+
connectDevframe: () => {
36+
const ok = connection.answers[connection.attempts++] ?? false;
37+
return ok ? Promise.resolve(fakeClient()) : Promise.reject(new Error('no server'));
38+
},
39+
}));
40+
41+
const { App } = await import('../app');
42+
43+
async function settle(fixture: ComponentFixture<unknown>) {
44+
for (let i = 0; i < 3; i++) {
45+
await new Promise((resolve) => setTimeout(resolve));
46+
await fixture.whenStable();
47+
}
48+
}
49+
50+
const host = (fixture: ComponentFixture<unknown>) => fixture.nativeElement as HTMLElement;
51+
const status = (fixture: ComponentFixture<unknown>) =>
52+
host(fixture).querySelector('.status')?.textContent?.trim();
53+
54+
afterEach(() => {
55+
document.body.innerHTML = '';
56+
connection.attempts = 0;
57+
connection.answers = [];
58+
TestBed.resetTestingModule();
59+
});
60+
61+
describe('App connection retry', () => {
62+
it('offers Retry now when the server does not answer, and connects from it', async () => {
63+
connection.answers = [false, true];
64+
const fixture = TestBed.createComponent(App);
65+
document.body.appendChild(host(fixture));
66+
await settle(fixture);
67+
68+
const alert = host(fixture).querySelector('.connection-error')!;
69+
expect(alert.getAttribute('role')).toBe('alert');
70+
expect(status(fixture)).toBe('Disconnected');
71+
const retry = alert.querySelector('button')!;
72+
expect(retry.textContent?.trim()).toBe('Retry now');
73+
74+
retry.focus();
75+
retry.click();
76+
await settle(fixture);
77+
expect(connection.attempts).toBe(2);
78+
expect(host(fixture).querySelector('.connection-error')).toBeNull();
79+
expect(status(fixture)).toBe('Live');
80+
// The button went with the error, so focus moved on rather than falling to the page.
81+
expect(document.activeElement).toBe(host(fixture).querySelector('nav button.active'));
82+
fixture.destroy();
83+
});
84+
85+
it('tries again on its own after a failed first connection', async () => {
86+
connection.answers = [false, true];
87+
const fixture = TestBed.createComponent(App);
88+
await settle(fixture);
89+
expect(connection.attempts).toBe(1);
90+
91+
await vi.waitFor(() => expect(connection.attempts).toBe(2), { timeout: 3000 });
92+
await settle(fixture);
93+
expect(status(fixture)).toBe('Live');
94+
fixture.destroy();
95+
});
96+
97+
it('connects again when an open connection drops', async () => {
98+
connection.answers = [true, true];
99+
const fixture = TestBed.createComponent(App);
100+
await settle(fixture);
101+
expect(status(fixture)).toBe('Live');
102+
103+
// The dev server restarted: the old client is closed for good.
104+
connection.emitStatus('disconnected');
105+
await settle(fixture);
106+
expect(status(fixture)).toBe('Disconnected');
107+
expect(host(fixture).querySelector('.connection-error button')).not.toBeNull();
108+
109+
await vi.waitFor(() => expect(connection.attempts).toBe(2), { timeout: 3000 });
110+
await settle(fixture);
111+
expect(status(fixture)).toBe('Live');
112+
fixture.destroy();
113+
});
114+
115+
it('stops retrying once the panel is gone', async () => {
116+
connection.answers = [false, true];
117+
const fixture = TestBed.createComponent(App);
118+
await settle(fixture);
119+
fixture.destroy();
120+
121+
await new Promise((resolve) => setTimeout(resolve, 1_200));
122+
expect(connection.attempts).toBe(1);
123+
});
124+
});
125+
126+
describe('App tab bar', () => {
127+
it('keeps one tab in the Tab order and moves along the bar with arrow keys', async () => {
128+
connection.answers = [true];
129+
const fixture = TestBed.createComponent(App);
130+
document.body.appendChild(host(fixture));
131+
await settle(fixture);
132+
133+
const buttons = [...host(fixture).querySelectorAll<HTMLButtonElement>('nav button')];
134+
expect(buttons.length).toBeGreaterThan(2);
135+
expect(buttons.filter((b) => b.tabIndex === 0)).toEqual([buttons[0]]);
136+
137+
const press = (key: string) =>
138+
document.activeElement!.dispatchEvent(
139+
new KeyboardEvent('keydown', { key, bubbles: true, cancelable: true }),
140+
);
141+
buttons[0].focus();
142+
press('ArrowRight');
143+
expect(document.activeElement).toBe(buttons[1]);
144+
press('ArrowLeft');
145+
press('ArrowLeft');
146+
expect(document.activeElement).toBe(buttons.at(-1));
147+
press('Home');
148+
expect(document.activeElement).toBe(buttons[0]);
149+
150+
buttons[2].click();
151+
await settle(fixture);
152+
const now = [...host(fixture).querySelectorAll<HTMLButtonElement>('nav button')];
153+
expect(now.filter((b) => b.tabIndex === 0)).toEqual([now[2]]);
154+
fixture.destroy();
155+
});
156+
});

‎app/src/__tests__/native-controls.test.ts‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TestBed, type ComponentFixture } from '@angular/core/testing';
22
import type { DevframeRpcClient } from 'devframe/client';
33
import { afterEach, describe, expect, it } from 'vitest';
4-
import { isAngularNativePage } from '../native-page';
4+
import { isAngularNativePage, isNativePage } from '../native-page';
55
import { ComponentTree } from '../pages/component-tree';
66

77
function fakeClient(pages: Record<string, unknown>): DevframeRpcClient {
@@ -84,22 +84,33 @@ describe('browser-only controls', () => {
8484
expect(isAngularNativePage(null)).toBe(false);
8585
});
8686

87+
it('treats Angular Native and NativeScript pages as native, and only those', () => {
88+
expect(isNativePage({ platform: 'angular-native' })).toBe(true);
89+
expect(isNativePage({ platform: 'nativescript' })).toBe(true);
90+
expect(isAngularNativePage({ platform: 'nativescript' })).toBe(false);
91+
expect(isNativePage({ platform: 'browser' })).toBe(false);
92+
expect(isNativePage(null)).toBe(false);
93+
});
94+
8795
it('shows the component picker and change detection recording for a browser page', async () => {
8896
const fixture = await render();
8997
expect(buttons(fixture)).toContain('Pick component on page');
9098
expect(buttons(fixture)).toContain('Record');
9199
expect((fixture.nativeElement as HTMLElement).querySelector('#cd-heading')).not.toBeNull();
92100
});
93101

94-
it('hides them for an Angular Native page and keeps the tree', async () => {
95-
const fixture = await render('angular-native');
96-
const host = fixture.nativeElement as HTMLElement;
97-
expect(buttons(fixture)).not.toContain('Pick component on page');
98-
expect(buttons(fixture)).not.toContain('Record');
99-
expect(host.querySelector('#cd-heading')).toBeNull();
100-
expect(host.textContent).toContain('App');
101-
expect(host.querySelector('input[type="search"]')).not.toBeNull();
102-
});
102+
it.each(['angular-native', 'nativescript'])(
103+
'hides them for a %s page and keeps the tree',
104+
async (platform) => {
105+
const fixture = await render(platform);
106+
const host = fixture.nativeElement as HTMLElement;
107+
expect(buttons(fixture)).not.toContain('Pick component on page');
108+
expect(buttons(fixture)).not.toContain('Record');
109+
expect(host.querySelector('#cd-heading')).toBeNull();
110+
expect(host.textContent).toContain('App');
111+
expect(host.querySelector('input[type="search"]')).not.toBeNull();
112+
},
113+
);
103114

104115
it('cancels a pick on its own page when the dropdown shows another page', async () => {
105116
const { client, calls } = liveClient({ p1: page('p1'), p2: page('p2') });
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { Component, signal } from '@angular/core';
2+
import { TestBed, type ComponentFixture } from '@angular/core/testing';
3+
import { afterEach, describe, expect, it } from 'vitest';
4+
import { RovingList } from '../ui/roving-list';
5+
6+
@Component({
7+
imports: [RovingList],
8+
template: `
9+
<ul appRovingList="button.row">
10+
@for (item of items(); track item) {
11+
<li>
12+
<button type="button" class="row" [attr.aria-pressed]="item === selected()">
13+
{{ item }}
14+
</button>
15+
</li>
16+
}
17+
</ul>
18+
`,
19+
})
20+
class Host {
21+
readonly items = signal(['a', 'b', 'c', 'd']);
22+
readonly selected = signal<string | null>(null);
23+
}
24+
25+
async function settle(fixture: ComponentFixture<unknown>) {
26+
for (let i = 0; i < 3; i++) {
27+
await new Promise((resolve) => setTimeout(resolve));
28+
await fixture.whenStable();
29+
}
30+
}
31+
32+
function mount() {
33+
const fixture = TestBed.createComponent(Host);
34+
document.body.appendChild(fixture.nativeElement);
35+
return fixture;
36+
}
37+
38+
const rows = (fixture: ComponentFixture<Host>) => [
39+
...(fixture.nativeElement as HTMLElement).querySelectorAll<HTMLButtonElement>('button.row'),
40+
];
41+
const tabStops = (fixture: ComponentFixture<Host>) =>
42+
rows(fixture)
43+
.filter((row) => row.tabIndex === 0)
44+
.map((row) => row.textContent?.trim());
45+
const press = (key: string) =>
46+
document.activeElement!.dispatchEvent(
47+
new KeyboardEvent('keydown', { key, bubbles: true, cancelable: true }),
48+
);
49+
50+
afterEach(() => {
51+
document.body.innerHTML = '';
52+
});
53+
54+
describe('RovingList', () => {
55+
it('keeps one row in the Tab order, the selected one when there is one', async () => {
56+
const fixture = mount();
57+
fixture.componentInstance.selected.set('c');
58+
await settle(fixture);
59+
expect(tabStops(fixture)).toEqual(['c']);
60+
});
61+
62+
it('moves focus with the arrow keys, Home and End, and stops at the ends', async () => {
63+
const fixture = mount();
64+
await settle(fixture);
65+
expect(tabStops(fixture)).toEqual(['a']);
66+
67+
rows(fixture)[0].focus();
68+
press('ArrowUp');
69+
expect(document.activeElement).toBe(rows(fixture)[0]);
70+
press('ArrowDown');
71+
expect(document.activeElement).toBe(rows(fixture)[1]);
72+
expect(tabStops(fixture)).toEqual(['b']);
73+
press('End');
74+
expect(document.activeElement).toBe(rows(fixture)[3]);
75+
press('ArrowDown');
76+
expect(document.activeElement).toBe(rows(fixture)[3]);
77+
press('Home');
78+
expect(document.activeElement).toBe(rows(fixture)[0]);
79+
});
80+
81+
it('takes rows that arrive later out of the Tab order, and finds a new stop when its row goes', async () => {
82+
const fixture = mount();
83+
await settle(fixture);
84+
rows(fixture)[1].focus();
85+
86+
fixture.componentInstance.items.set(['a', 'b', 'c', 'd', 'e']);
87+
await settle(fixture);
88+
expect(tabStops(fixture)).toEqual(['b']);
89+
90+
fixture.componentInstance.items.set(['c', 'd']);
91+
await settle(fixture);
92+
expect(tabStops(fixture)).toEqual(['c']);
93+
});
94+
it('moves focus to the new stop when the focused row is removed', async () => {
95+
const fixture = mount();
96+
await settle(fixture);
97+
rows(fixture)[1].focus();
98+
99+
fixture.componentInstance.items.set(['c', 'd']);
100+
await settle(fixture);
101+
expect(document.activeElement).toBe(rows(fixture)[0]);
102+
press('ArrowDown');
103+
expect(document.activeElement?.textContent?.trim()).toBe('d');
104+
});
105+
106+
it('leaves focus alone when it had already left the list', async () => {
107+
const fixture = mount();
108+
const outside = document.createElement('button');
109+
document.body.appendChild(outside);
110+
await settle(fixture);
111+
112+
rows(fixture)[1].focus();
113+
outside.focus();
114+
fixture.componentInstance.items.set(['c', 'd']);
115+
await settle(fixture);
116+
expect(document.activeElement).toBe(outside);
117+
118+
rows(fixture)[0].focus();
119+
rows(fixture)[0].blur();
120+
await settle(fixture);
121+
fixture.componentInstance.items.set(['d']);
122+
await settle(fixture);
123+
expect(document.activeElement).toBe(document.body);
124+
});
125+
});

‎app/src/__tests__/store-inspector.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,37 @@ describe('StoreInspector source declarations', () => {
403403
expect(box.value).toBe('');
404404
});
405405
});
406+
407+
describe('StoreInspector before the shared state arrives', () => {
408+
it('says it is loading, then shows the empty state once the server answers', async () => {
409+
let value: unknown = undefined;
410+
let push: (next: unknown) => void = () => {};
411+
const rpc = {
412+
call: () => Promise.resolve([]),
413+
callEvent: () => Promise.resolve(),
414+
sharedState: () =>
415+
Promise.resolve({
416+
value: () => value,
417+
on: (_: string, listener: (next: unknown) => void) => {
418+
push = listener;
419+
return () => {};
420+
},
421+
}),
422+
};
423+
const client = { connectionMeta: {}, scope: () => ({ rpc }) } as unknown as DevframeRpcClient;
424+
const fixture = TestBed.createComponent(StoreInspector);
425+
fixture.componentRef.setInput('rpc', client);
426+
await settle(fixture);
427+
const text = () => (fixture.nativeElement as HTMLElement).textContent ?? '';
428+
429+
const status = (fixture.nativeElement as HTMLElement).querySelector('.empty[role="status"]');
430+
expect(status?.textContent).toContain('Loading…');
431+
expect(text()).not.toContain('No page is reporting NgRx state yet.');
432+
433+
value = { pages: [] };
434+
push(value);
435+
await settle(fixture);
436+
expect(text()).not.toContain('Loading…');
437+
expect(text()).toContain('No page is reporting NgRx state yet.');
438+
});
439+
});

0 commit comments

Comments
 (0)