Files
KoalaSync/extension/media-frame-target.test.mjs
T

149 lines
6.8 KiB
JavaScript

import { describe, expect, it, vi } from 'vitest';
import {
GOOGLE_DRIVE_PLAYER_ACCESS_REQUIRED,
GOOGLE_DRIVE_PLAYER_AMBIGUOUS,
isGoogleDriveUrl,
resolveMediaScriptTarget,
selectGoogleDrivePlayerFrame
} from './media-frame-target.js';
const drivePlayerUrl = 'https://youtube.googleapis.com/embed/?enablejsapi=1&origin=https%3A%2F%2Fdrive.google.com';
describe('Google Drive media-frame targeting', () => {
it('recognizes Drive tabs without treating other Google pages as Drive', () => {
expect(isGoogleDriveUrl('https://drive.google.com/drive/u/1/search?q=mp4')).toBe(true);
expect(isGoogleDriveUrl('https://docs.google.com/document/d/1/edit')).toBe(false);
});
it('selects the visible frame containing the real video', () => {
const selected = selectGoogleDrivePlayerFrame([
{ frameId: 0, result: { href: 'https://drive.google.com/drive/u/1/search?q=mp4', videoCount: 0, videoScore: 0, frameArea: 900000 } },
{ frameId: 4, result: { href: drivePlayerUrl, videoCount: 1, videoScore: 1900000, frameArea: 900000, parentFrameVisible: true } },
{ frameId: 5, result: { href: drivePlayerUrl, videoCount: 0, videoScore: 0, frameArea: 0, parentFrameVisible: false } }
]);
expect(selected.frameId).toBe(4);
});
it('never lets a hidden loaded player outrank the visible Drive player', () => {
const selected = selectGoogleDrivePlayerFrame([
{ frameId: 4, result: { href: drivePlayerUrl, videoCount: 1, videoScore: 927600, frameArea: 900000, parentFrameVisible: true, parentFrameArea: 900000 } },
{ frameId: 5, result: { href: drivePlayerUrl, videoCount: 1, videoScore: 1281600, frameArea: 900000, parentFrameVisible: false, parentFrameArea: 900000 } }
]);
expect(selected.frameId).toBe(4);
});
it('refuses to guess when parent visibility is unavailable', () => {
const selected = selectGoogleDrivePlayerFrame([
{ frameId: 4, result: { href: drivePlayerUrl, videoCount: 1, videoScore: 927600, frameArea: 900000 } },
{ frameId: 5, result: { href: drivePlayerUrl, videoCount: 1, videoScore: 1281600, frameArea: 900000 } }
]);
expect(selected).toBeNull();
});
it('ignores unrelated video frames inside a Drive tab', () => {
const selected = selectGoogleDrivePlayerFrame([
{ frameId: 3, result: { href: 'https://example.com/embed', videoCount: 1, videoScore: 3000000, frameArea: 1200000 } },
{ frameId: 4, result: { href: drivePlayerUrl, videoCount: 1, videoScore: 927600, frameArea: 900000 } }
]);
expect(selected.frameId).toBe(4);
});
it('falls back to the visible Drive player frame while its video is loading', () => {
const selected = selectGoogleDrivePlayerFrame([
{ frameId: 4, result: { href: drivePlayerUrl, videoCount: 0, videoScore: 0, frameArea: 900000, parentFrameVisible: true } },
{ frameId: 5, result: { href: drivePlayerUrl, videoCount: 0, videoScore: 0, frameArea: 0, parentFrameVisible: false } }
]);
expect(selected.frameId).toBe(4);
});
it('reports ambiguity instead of selecting a stale frame after retries', async () => {
const ambiguousResults = [
{ frameId: 4, result: { href: drivePlayerUrl, videoCount: 1, videoScore: 927600, frameArea: 900000 } },
{ frameId: 5, result: { href: drivePlayerUrl, videoCount: 1, videoScore: 1281600, frameArea: 900000 } }
];
const executeScript = vi.fn().mockResolvedValue(ambiguousResults);
await expect(resolveMediaScriptTarget(
{ scripting: { executeScript } },
42,
'https://drive.google.com/drive/u/1/search?q=mp4',
{ attempts: 1, retryDelayMs: 0 }
)).rejects.toMatchObject({ code: GOOGLE_DRIVE_PLAYER_AMBIGUOUS });
});
it('probes all frames and returns only the frame with the Drive video', async () => {
const executeScript = vi.fn()
.mockResolvedValueOnce([
{ frameId: 0, result: { href: 'https://drive.google.com/drive/u/1/search?q=mp4', videoCount: 0, videoScore: 0, frameArea: 900000 } },
{ frameId: 4, result: { href: drivePlayerUrl, videoCount: 0, videoScore: 0, frameArea: 900000 } }
])
.mockResolvedValueOnce([
{ frameId: 0, result: { href: 'https://drive.google.com/drive/u/1/search?q=mp4', videoCount: 0, videoScore: 0, frameArea: 900000 } },
{ frameId: 4, result: { href: drivePlayerUrl, videoCount: 1, videoScore: 1900000, frameArea: 900000 } }
]);
await expect(resolveMediaScriptTarget(
{ scripting: { executeScript } },
42,
'https://drive.google.com/drive/u/1/search?q=mp4',
{ retryDelayMs: 0 }
)).resolves.toEqual({ tabId: 42, frameIds: [4] });
expect(executeScript).toHaveBeenCalledWith({
target: { tabId: 42, allFrames: true },
func: expect.any(Function)
});
});
it('reports missing access when Drive exposes a player iframe but the frame cannot be inspected', async () => {
const executeScript = vi.fn().mockResolvedValue([
{
frameId: 0,
result: {
href: 'https://drive.google.com/drive/u/1/search?q=mp4',
videoCount: 0,
videoScore: 0,
frameArea: 900000,
drivePlayerIframeCount: 1
}
}
]);
await expect(resolveMediaScriptTarget(
{ scripting: { executeScript } },
42,
'https://drive.google.com/drive/u/1/search?q=mp4',
{ attempts: 1, retryDelayMs: 0 }
)).rejects.toMatchObject({ code: GOOGLE_DRIVE_PLAYER_ACCESS_REQUIRED });
});
it('reports missing access when the all-frame probe itself is rejected', async () => {
const executeScript = vi.fn()
.mockRejectedValueOnce(new Error('Cannot access contents of the frame'))
.mockResolvedValueOnce([
{
frameId: 0,
result: {
href: 'https://drive.google.com/drive/u/1/search?q=mp4',
videoCount: 0,
videoScore: 0,
frameArea: 900000,
drivePlayerIframeCount: 1
}
}
]);
await expect(resolveMediaScriptTarget(
{ scripting: { executeScript } },
42,
'https://drive.google.com/drive/u/1/search?q=mp4',
{ attempts: 1, retryDelayMs: 0 }
)).rejects.toMatchObject({ code: GOOGLE_DRIVE_PLAYER_ACCESS_REQUIRED });
});
});