|
| 1 | +import { TestBed, type ComponentFixture } from '@angular/core/testing'; |
| 2 | +import type { DevframeRpcClient } from 'devframe/client'; |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { CdRecording, type CdPage } from '../pages/cd-recording'; |
| 5 | + |
| 6 | +let fixture: ComponentFixture<CdRecording>; |
| 7 | +let saved: { name: string; blob: Blob }[]; |
| 8 | + |
| 9 | +function page(): CdPage { |
| 10 | + return { |
| 11 | + pageId: 'p1', |
| 12 | + supported: true, |
| 13 | + recording: false, |
| 14 | + startedAt: 1000, |
| 15 | + dropped: 0, |
| 16 | + hosts: { e1: 2 }, |
| 17 | + components: [{ name: 'Cart', checks: 2, ms: 3, maxMs: 2, cycles: 2 }], |
| 18 | + hooks: [{ name: 'Cart', hook: 'ngDoCheck', calls: 4, ms: 1.25, maxMs: 0.5 }], |
| 19 | + outputs: [{ name: 'Cart', listener: 'onAdd', calls: 1, ms: 0.5, maxMs: 0.5 }], |
| 20 | + afterRender: { runs: 2, ms: 0.75, maxMs: 0.5 }, |
| 21 | + spanNames: ['Cart'], |
| 22 | + cycles: [ |
| 23 | + { |
| 24 | + id: 1, |
| 25 | + at: 1010, |
| 26 | + t: 10, |
| 27 | + ms: 2, |
| 28 | + passes: 1, |
| 29 | + checks: 2, |
| 30 | + components: [{ name: 'Cart', checks: 2, ms: 1 }], |
| 31 | + hookMs: 0.5, |
| 32 | + afterRenderMs: 0.25, |
| 33 | + spans: [[0, 0, 1, 0, 'c']], |
| 34 | + }, |
| 35 | + ], |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +const host = () => fixture.nativeElement as HTMLElement; |
| 40 | +const button = (text: string) => |
| 41 | + [...host().querySelectorAll('button')].find((b) => b.textContent?.trim() === text); |
| 42 | +const live = () => host().querySelector('[role="status"]')!.textContent!; |
| 43 | + |
| 44 | +async function settle() { |
| 45 | + await fixture.whenStable(); |
| 46 | + await new Promise((resolve) => setTimeout(resolve)); |
| 47 | + await fixture.whenStable(); |
| 48 | +} |
| 49 | + |
| 50 | +async function importFile(content: string, name = 'recording.json') { |
| 51 | + const input = host().querySelector<HTMLInputElement>('input[type="file"]')!; |
| 52 | + const file = new File([content], name, { type: 'application/json' }); |
| 53 | + Object.defineProperty(input, 'files', { value: [file], configurable: true }); |
| 54 | + input.dispatchEvent(new Event('change')); |
| 55 | + await settle(); |
| 56 | +} |
| 57 | + |
| 58 | +async function saveWith(text: string): Promise<unknown> { |
| 59 | + saved = []; |
| 60 | + button(text)!.click(); |
| 61 | + await settle(); |
| 62 | + expect(saved).toHaveLength(1); |
| 63 | + return JSON.parse(await saved[0]!.blob.text()); |
| 64 | +} |
| 65 | + |
| 66 | +describe('CdRecording export and import', () => { |
| 67 | + beforeEach(async () => { |
| 68 | + saved = []; |
| 69 | + let last: Blob | null = null; |
| 70 | + vi.spyOn(URL, 'createObjectURL').mockImplementation((blob) => { |
| 71 | + last = blob as Blob; |
| 72 | + return 'blob:x'; |
| 73 | + }); |
| 74 | + vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => {}); |
| 75 | + vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function ( |
| 76 | + this: HTMLAnchorElement, |
| 77 | + ) { |
| 78 | + saved.push({ name: this.download, blob: last! }); |
| 79 | + }); |
| 80 | + const client = { scope: () => ({ rpc: { call: vi.fn(() => Promise.resolve()) } }) }; |
| 81 | + fixture = TestBed.createComponent(CdRecording); |
| 82 | + fixture.componentRef.setInput('pageId', 'p1'); |
| 83 | + fixture.componentRef.setInput('rpc', client as unknown as DevframeRpcClient); |
| 84 | + fixture.componentRef.setInput('page', page()); |
| 85 | + await settle(); |
| 86 | + }); |
| 87 | + |
| 88 | + afterEach(() => { |
| 89 | + vi.restoreAllMocks(); |
| 90 | + fixture?.destroy(); |
| 91 | + TestBed.resetTestingModule(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('shows hook, output and afterRender time', () => { |
| 95 | + const text = host().textContent!.replace(/\s+/g, ' '); |
| 96 | + expect(text).toContain('Lifecycle hooks'); |
| 97 | + expect(text).toContain('Cart.ngDoCheck'); |
| 98 | + expect(text).toContain('Cart (onAdd)'); |
| 99 | + expect(text).toContain('afterRender callbacks ran 2 times, 0.75 ms in total, slowest 0.50 ms.'); |
| 100 | + expect(text).toContain('hooks 0.50 ms'); |
| 101 | + expect(text).toContain('afterRender 0.25 ms'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('exports the recording as JSON and as a Perfetto trace', async () => { |
| 105 | + const file = (await saveWith('Export JSON')) as { |
| 106 | + format: string; |
| 107 | + pageId: string; |
| 108 | + recording: { hosts: object; hooks: unknown[] }; |
| 109 | + }; |
| 110 | + expect(saved[0]?.name).toBe('change-detection-p1.json'); |
| 111 | + expect(file.format).toBe('pangular-inspector/change-detection'); |
| 112 | + expect(file.recording.hosts).toEqual({}); |
| 113 | + expect(file.recording.hooks).toHaveLength(1); |
| 114 | + |
| 115 | + const trace = (await saveWith('Export trace (Perfetto)')) as { |
| 116 | + traceEvents: { ph: string; name: string }[]; |
| 117 | + }; |
| 118 | + expect(saved[0]?.name).toBe('change-detection-p1.trace.json'); |
| 119 | + const complete = trace.traceEvents.filter((e) => e.ph === 'X').map((e) => e.name); |
| 120 | + expect(complete).toEqual(['Change detection #1', 'Cart']); |
| 121 | + }); |
| 122 | + |
| 123 | + it('shows an imported recording, marked as imported, and goes back to the live one', async () => { |
| 124 | + const hostile = { |
| 125 | + ...page(), |
| 126 | + pageId: 'other', |
| 127 | + recording: true, |
| 128 | + components: [ |
| 129 | + { name: '<img src=x onerror="alert(1)">', checks: 1, ms: 9, maxMs: 9, cycles: 1 }, |
| 130 | + ], |
| 131 | + script: '<script>alert(1)</script>', |
| 132 | + cycles: [...page().cycles, { ...page().cycles[0]!, id: 2, trigger: 'x'.repeat(1000) }], |
| 133 | + }; |
| 134 | + await importFile(JSON.stringify(hostile), 'hand-edited.json'); |
| 135 | + |
| 136 | + expect(host().querySelector('.imported')!.textContent).toContain('Imported recording'); |
| 137 | + expect(host().querySelector('.imported')!.textContent).toContain('hand-edited.json'); |
| 138 | + expect(host().querySelector('.status')!.textContent!.trim()).toBe('2 cycles imported.'); |
| 139 | + expect(live()).toContain('Imported hand-edited.json: 2 cycles.'); |
| 140 | + expect(button('Record')).toBeUndefined(); |
| 141 | + expect(button('Stop recording')).toBeUndefined(); |
| 142 | + // Names are text, never markup, and long strings are cut by the shared sanitizer. |
| 143 | + expect(host().querySelector('img, script')).toBeNull(); |
| 144 | + expect(host().textContent).toContain('<img src=x onerror="alert(1)">'); |
| 145 | + expect(host().querySelector('.trigger .mono')!.textContent).toHaveLength(300); |
| 146 | + |
| 147 | + button('Back to live recording')!.click(); |
| 148 | + await settle(); |
| 149 | + expect(host().querySelector('.imported')).toBeNull(); |
| 150 | + expect(button('Record')).toBeDefined(); |
| 151 | + expect(live()).toBe('Showing the live recording.'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('says why a file cannot be imported and keeps the live recording', async () => { |
| 155 | + await importFile('{"navigations": []}', 'routes.json'); |
| 156 | + expect(host().querySelector('[role="alert"]')!.textContent).toBe( |
| 157 | + 'routes.json: The file has no change detection cycles.', |
| 158 | + ); |
| 159 | + expect(host().querySelector('.imported')).toBeNull(); |
| 160 | + |
| 161 | + await importFile('not json', 'broken.json'); |
| 162 | + expect(host().querySelector('[role="alert"]')!.textContent).toBe( |
| 163 | + 'broken.json: The file is not valid JSON.', |
| 164 | + ); |
| 165 | + |
| 166 | + await importFile(JSON.stringify(page())); |
| 167 | + expect(host().querySelector('[role="alert"]')).toBeNull(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('turns the export buttons off when there is nothing to export', async () => { |
| 171 | + fixture.componentRef.setInput('page', { ...page(), cycles: [] }); |
| 172 | + await settle(); |
| 173 | + expect(button('Export JSON')!.disabled).toBe(true); |
| 174 | + expect(button('Export trace (Perfetto)')!.disabled).toBe(true); |
| 175 | + expect(button('Import')!.disabled).toBe(false); |
| 176 | + }); |
| 177 | +}); |
0 commit comments