From dd5565b74829ffbaee45a9cef830f150d12931cb Mon Sep 17 00:00:00 2001 From: Sebastien Melki Date: Wed, 15 Apr 2026 20:30:29 +0300 Subject: [PATCH] fix(20): update test guards for deferred Sentry and lazy panel patterns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - sentry-beforesend.test: support extracted _sentryBeforeSend function (no longer inline beforeSend(event) { ... }) - live-news-panel-guard.test: match guard inside lazyPanel callback (no longer preceded by shouldCreatePanel) Both tests verify the same behavioral contracts — just adapted to the new code structure from Phase 20 dynamic imports. Co-Authored-By: Claude Opus 4.6 (1M context) --- tests/live-news-panel-guard.test.mts | 6 ++++-- tests/sentry-beforesend.test.mjs | 28 ++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/tests/live-news-panel-guard.test.mts b/tests/live-news-panel-guard.test.mts index c88343c43..03b3a7eea 100644 --- a/tests/live-news-panel-guard.test.mts +++ b/tests/live-news-panel-guard.test.mts @@ -75,7 +75,8 @@ describe('LiveNewsPanel instantiation guard', () => { it('panel-layout.ts live-news guard checks getDefaultLiveChannels()', () => { const layout = src('src/app/panel-layout.ts'); - const guardBlock = layout.match(/shouldCreatePanel\('live-news'\)[^}]*getDefaultLiveChannels\(\)\.length/s); + // Guard may be inside a lazyPanel callback or after shouldCreatePanel — match either pattern + const guardBlock = layout.match(/['"]live-news['"][^}]*getDefaultLiveChannels\(\)\.length/s); assert.ok( guardBlock, "panel-layout.ts must guard 'live-news' with getDefaultLiveChannels().length > 0", @@ -84,7 +85,8 @@ describe('LiveNewsPanel instantiation guard', () => { it('panel-layout.ts live-news guard also checks loadChannelsFromStorage()', () => { const layout = src('src/app/panel-layout.ts'); - const guardBlock = layout.match(/shouldCreatePanel\('live-news'\)[^}]*loadChannelsFromStorage\(\)\.length/s); + // Guard may be inside a lazyPanel callback or after shouldCreatePanel — match either pattern + const guardBlock = layout.match(/['"]live-news['"][^}]*loadChannelsFromStorage\(\)\.length/s); assert.ok( guardBlock, "panel-layout.ts must also check loadChannelsFromStorage().length > 0 so users with saved channels can use the panel on happy variant", diff --git a/tests/sentry-beforesend.test.mjs b/tests/sentry-beforesend.test.mjs index 661a3e2ab..aed3e1c95 100644 --- a/tests/sentry-beforesend.test.mjs +++ b/tests/sentry-beforesend.test.mjs @@ -10,22 +10,38 @@ const __dirname = dirname(fileURLToPath(import.meta.url)); // We parse it as a standalone function to avoid importing Sentry/App bootstrap. const mainSrc = readFileSync(resolve(__dirname, '../src/main.ts'), 'utf-8'); -// Extract everything between `beforeSend(event) {` and the matching closing `},` -const bsStart = mainSrc.indexOf('beforeSend(event) {'); -assert.ok(bsStart !== -1, 'beforeSend must exist in src/main.ts'); +// Extract the beforeSend function body from main.ts source. +// Supports both inline `beforeSend(event) {` and extracted `function _sentryBeforeSend(event: any): any {`. +const MARKERS = ['function _sentryBeforeSend(event', 'beforeSend(event) {']; +let bsStart = -1; +let markerEnd = ''; +for (const marker of MARKERS) { + bsStart = mainSrc.indexOf(marker); + if (bsStart !== -1) { + // Find the opening brace after the marker + markerEnd = marker; + break; + } +} +assert.ok(bsStart !== -1, 'beforeSend / _sentryBeforeSend must exist in src/main.ts'); +// Find the opening brace +const braceStart = mainSrc.indexOf('{', bsStart + markerEnd.length); +assert.ok(braceStart !== -1, 'Failed to find opening brace'); let braceDepth = 0; let bsEnd = -1; -for (let i = bsStart + 'beforeSend(event) '.length; i < mainSrc.length; i++) { +for (let i = braceStart; i < mainSrc.length; i++) { if (mainSrc[i] === '{') braceDepth++; if (mainSrc[i] === '}') { braceDepth--; if (braceDepth === 0) { bsEnd = i + 1; break; } } } -assert.ok(bsEnd > bsStart, 'Failed to find beforeSend closing brace'); +assert.ok(bsEnd > braceStart, 'Failed to find beforeSend closing brace'); // Strip TypeScript type annotations so the body can be eval'd as plain JS. -const fnBody = mainSrc.slice(bsStart + 'beforeSend(event) '.length, bsEnd) +const fnBody = mainSrc.slice(braceStart, bsEnd) .replace(/:\s*string\b/g, '') // parameter type annotations + .replace(/:\s*any\b/g, '') // any type annotations + .replace(/:\s*_SentryFrame\[\]/g, '') // custom type annotations .replace(/as\s+\w+(\[\])?/g, '') // type assertions .replace(/<[A-Z]\w*>/g, ''); // generic type params