|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { loadScriptIntoContext } from './helpers/load-script.js'; |
| 4 | + |
| 5 | +function makeChromeStub() { |
| 6 | + return { |
| 7 | + runtime: { |
| 8 | + sendMessage: () => {}, |
| 9 | + lastError: null, |
| 10 | + onInstalled: { addListener: () => {} }, |
| 11 | + onMessage: { addListener: () => {} }, |
| 12 | + openOptionsPage: () => {}, |
| 13 | + getManifest: () => ({ version: '0.0.0' }), |
| 14 | + }, |
| 15 | + storage: { |
| 16 | + sync: { |
| 17 | + get: (_keys, cb) => cb({}), |
| 18 | + set: async () => {}, |
| 19 | + }, |
| 20 | + local: { |
| 21 | + set: async () => {}, |
| 22 | + get: async () => ({}), |
| 23 | + }, |
| 24 | + onChanged: { addListener: () => {} }, |
| 25 | + }, |
| 26 | + webRequest: { |
| 27 | + onBeforeRequest: { addListener: () => {} }, |
| 28 | + onSendHeaders: { addListener: () => {} }, |
| 29 | + }, |
| 30 | + action: { |
| 31 | + setBadgeText: () => {}, |
| 32 | + setBadgeBackgroundColor: () => {}, |
| 33 | + onClicked: { addListener: () => {} }, |
| 34 | + }, |
| 35 | + tabs: { |
| 36 | + onRemoved: { addListener: () => {} }, |
| 37 | + onUpdated: { addListener: () => {} }, |
| 38 | + onActivated: { addListener: () => {} }, |
| 39 | + query: (_q, cb) => cb([]), |
| 40 | + get: (_id, cb) => cb(null), |
| 41 | + }, |
| 42 | + webNavigation: { |
| 43 | + onCommitted: { addListener: () => {} }, |
| 44 | + }, |
| 45 | + contextMenus: { |
| 46 | + create: () => {}, |
| 47 | + onClicked: { addListener: () => {} }, |
| 48 | + }, |
| 49 | + notifications: { |
| 50 | + create: () => {}, |
| 51 | + }, |
| 52 | + sidePanel: { |
| 53 | + open: async () => {}, |
| 54 | + }, |
| 55 | + cookies: { |
| 56 | + getAll: async () => [], |
| 57 | + }, |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +function withFixedNow(ctx, nowMs) { |
| 62 | + ctx.Date = class extends Date { |
| 63 | + static now() { |
| 64 | + return nowMs; |
| 65 | + } |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +describe('background.js pure helpers', () => { |
| 70 | + it('isCandidateVideoUrl accepts m3u8/mp4 and rejects obvious non-video', () => { |
| 71 | + const ctx = loadScriptIntoContext('background.js', { |
| 72 | + chrome: makeChromeStub(), |
| 73 | + fetch: async () => ({ ok: true, json: async () => ({}) }), |
| 74 | + }); |
| 75 | + |
| 76 | + expect(ctx.isCandidateVideoUrl('https://a/b/c.m3u8')).toBe(true); |
| 77 | + expect(ctx.isCandidateVideoUrl('https://a/b/c.mp4')).toBe(true); |
| 78 | + |
| 79 | + // segments |
| 80 | + expect(ctx.isCandidateVideoUrl('https://a/b/seg0001.ts')).toBe(false); |
| 81 | + expect(ctx.isCandidateVideoUrl('https://a/b/seg0001.m4s')).toBe(false); |
| 82 | + |
| 83 | + // false positives |
| 84 | + expect(ctx.isCandidateVideoUrl('https://a/b/preview_720p.mp4.jpg')).toBe(false); |
| 85 | + expect(ctx.isCandidateVideoUrl('https://a/b/playlist.m3u8.png')).toBe(false); |
| 86 | + expect(ctx.isCandidateVideoUrl('https://a/b/app.js?video=1.mp4')).toBe(false); |
| 87 | + }); |
| 88 | + |
| 89 | + it('scoreUrlInfo prefers recent + range hits + media type', () => { |
| 90 | + const ctx = loadScriptIntoContext('background.js', { |
| 91 | + chrome: makeChromeStub(), |
| 92 | + }); |
| 93 | + |
| 94 | + const now = 1_000_000; |
| 95 | + withFixedNow(ctx, now); |
| 96 | + |
| 97 | + const base = { |
| 98 | + url: 'https://cdn.example.com/v/video.mp4', |
| 99 | + timestamp: now - 5_000, |
| 100 | + requestType: 'media', |
| 101 | + hitCount: 1, |
| 102 | + rangeHitCount: 0, |
| 103 | + }; |
| 104 | + |
| 105 | + const s1 = ctx.scoreUrlInfo(base); |
| 106 | + const s2 = ctx.scoreUrlInfo({ ...base, rangeHitCount: 1 }); |
| 107 | + const s3 = ctx.scoreUrlInfo({ ...base, rangeHitCount: 1, hitCount: 10 }); |
| 108 | + |
| 109 | + expect(s2).toBeGreaterThan(s1); |
| 110 | + expect(s3).toBeGreaterThan(s2); |
| 111 | + }); |
| 112 | + |
| 113 | + it('getSortedUrlsForTab marks a clear winner as now playing', () => { |
| 114 | + const ctx = loadScriptIntoContext('background.js', { |
| 115 | + chrome: makeChromeStub(), |
| 116 | + }); |
| 117 | + |
| 118 | + const now = 2_000_000; |
| 119 | + withFixedNow(ctx, now); |
| 120 | + |
| 121 | + const tabId = 123; |
| 122 | + ctx.__eval(`currentTabUrls[${tabId}] = ${JSON.stringify([ |
| 123 | + { |
| 124 | + url: 'https://cdn.example.com/v/low.m3u8', |
| 125 | + timestamp: now - 60_000, |
| 126 | + requestType: 'xmlhttprequest', |
| 127 | + hitCount: 1, |
| 128 | + rangeHitCount: 0, |
| 129 | + }, |
| 130 | + { |
| 131 | + url: 'https://cdn.example.com/v/high.mp4', |
| 132 | + timestamp: now - 2_000, |
| 133 | + requestType: 'media', |
| 134 | + hitCount: 3, |
| 135 | + rangeHitCount: 2, |
| 136 | + }, |
| 137 | + ])};`); |
| 138 | + |
| 139 | + const sorted = ctx.getSortedUrlsForTab(tabId); |
| 140 | + expect(sorted[0].url).toContain('high.mp4'); |
| 141 | + expect(sorted[0].isNowPlaying).toBe(true); |
| 142 | + }); |
| 143 | + |
| 144 | + it('safeOrigin returns null on invalid URL', () => { |
| 145 | + const ctx = loadScriptIntoContext('background.js', { |
| 146 | + chrome: makeChromeStub(), |
| 147 | + }); |
| 148 | + |
| 149 | + expect(ctx.safeOrigin('https://example.com/a')).toBe('https://example.com'); |
| 150 | + expect(ctx.safeOrigin('not a url')).toBe(null); |
| 151 | + }); |
| 152 | +}); |
0 commit comments