Skip to content

Commit e75754e

Browse files
committed
Add tests for JavaScript source fetching
1 parent 2facf9f commit e75754e

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

src/test/unit/fetch-source.test.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import { fetchSource } from 'firefox-profiler/utils/fetch-source';
66

7+
const TEST_SOURCE_UUID = 'ff6d24c3-b8f5-45cd-a7d3-b643b3292e41';
8+
79
describe('fetchSource', function () {
810
it('fetches single files', async function () {
911
expect(
@@ -554,4 +556,109 @@ describe('fetchSource', function () {
554556
],
555557
});
556558
});
559+
560+
it('fetches JS source from browser with sourceUuid', async function () {
561+
expect(
562+
await fetchSource(
563+
'/path/to/script.js',
564+
TEST_SOURCE_UUID,
565+
'https://symbolication.services.mozilla.com',
566+
null,
567+
new Map(),
568+
{
569+
fetchUrlResponse: async (_url: string, _postData?: string) => {
570+
throw new Error('Should not fetch from URL');
571+
},
572+
queryBrowserSymbolicationApi: async (
573+
_path: string,
574+
_requestJson: string
575+
) => {
576+
throw new Error('Should not query TEST_SOURCE_UUID API');
577+
},
578+
fetchJSSourceFromBrowser: async (sourceUuid: string) => {
579+
if (sourceUuid === TEST_SOURCE_UUID) {
580+
return `console.log("Hello from browser with sourceUuid ${sourceUuid}");`;
581+
}
582+
throw new Error(`Unexpected source: ${sourceUuid}`);
583+
},
584+
}
585+
)
586+
).toEqual({
587+
type: 'SUCCESS',
588+
source: `console.log("Hello from browser with sourceUuid ${TEST_SOURCE_UUID}");`,
589+
});
590+
});
591+
592+
it('handles fetch JS source from browser with invalid sourceUuid', async function () {
593+
expect(
594+
await fetchSource(
595+
'/path/to/script.js',
596+
TEST_SOURCE_UUID,
597+
'https://symbolication.services.mozilla.com',
598+
null,
599+
new Map(),
600+
{
601+
fetchUrlResponse: async (_url: string, _postData?: string) => {
602+
throw new Error('Should not fetch from URL');
603+
},
604+
queryBrowserSymbolicationApi: async (
605+
_path: string,
606+
_requestJson: string
607+
) => {
608+
throw new Error('Should not query symbolication API');
609+
},
610+
fetchJSSourceFromBrowser: async (sourceUuid: string) => {
611+
throw new Error(
612+
`Source not found for source with ID: ${sourceUuid}`
613+
);
614+
},
615+
}
616+
)
617+
).toEqual({
618+
type: 'ERROR',
619+
errors: [
620+
{
621+
type: 'BROWSER_API_ERROR',
622+
apiErrorMessage: `Source not found for source with ID: ${TEST_SOURCE_UUID}`,
623+
},
624+
{
625+
type: 'NO_KNOWN_CORS_URL',
626+
},
627+
],
628+
});
629+
});
630+
631+
it('falls back to other methods when fetchJSSourceFromBrowser fails', async function () {
632+
expect(
633+
await fetchSource(
634+
'hg:hg.mozilla.org/mozilla-central:widget/cocoa/nsAppShell.mm:997f00815e6bc28806b75448c8829f0259d2cb28',
635+
// Should still try browser first but fall back to URL fetch
636+
TEST_SOURCE_UUID,
637+
'https://symbolication.services.mozilla.com',
638+
null,
639+
new Map(),
640+
{
641+
fetchUrlResponse: async (url: string, _postData?: string) => {
642+
const r = new Response(`Fallback response from ${url}`, {
643+
status: 200,
644+
});
645+
return r;
646+
},
647+
queryBrowserSymbolicationApi: async (
648+
_path: string,
649+
_requestJson: string
650+
) => {
651+
throw new Error('No browser connection');
652+
},
653+
fetchJSSourceFromBrowser: async (_sourceUuid: string) => {
654+
throw new Error('Source not found in browser');
655+
},
656+
}
657+
)
658+
).toEqual({
659+
type: 'SUCCESS',
660+
source:
661+
'Fallback response from https://hg.mozilla.org/mozilla-central/raw-file/997f00815e6bc28806b75448c8829f0259d2cb28/widget/cocoa/nsAppShell.mm',
662+
});
663+
});
557664
});

0 commit comments

Comments
 (0)