#!/usr/bin/env python3
"""Test: ignore ALL feature/blink/GPU/color related flags together."""
from playwright.sync_api import sync_playwright

# These are the ones that are suspicious
suspicious = [
    '--disable-features=AvoidUnnecessaryBeforeUnloadCheckSync,BoundaryEventDispatchTracksNodeRemoval,DestroyProfileOnBrowserClose,DialMediaRouteProvider,GlobalMediaControls,HttpsUpgrades,LensOverlay,MediaRouter,PaintHolding,ThirdPartyStoragePartitioning,Translate,AutoDeElevate,RenderDocument,OptimizationHints',
    '--enable-features=CDPScreenshotNewSurface',
    '--blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4',
    '--enable-unsafe-swiftshader',
    '--force-color-profile=srgb',
    '--disable-field-trial-config',
]

def test(ignored_flags, label):
    try:
        p = sync_playwright().start()
        b = p.chromium.launch(
            executable_path='/usr/bin/google-chrome-stable',
            headless=True,
            args=['--no-sandbox', '--disable-gpu'],
            ignore_default_args=ignored_flags,
            timeout=10000,
        )
        b.close()
        p.stop()
        print(f'{label}: OK')
        return True
    except Exception:
        print(f'{label}: CRASH')
        return False

# Test: ignore ALL suspicious flags together
print("Test 1: Ignore all suspicious flags together")
test(suspicious, 'all-suspicious')

# Test: which specific feature in disable-features?
features = "AvoidUnnecessaryBeforeUnloadCheckSync,BoundaryEventDispatchTracksNodeRemoval,DestroyProfileOnBrowserClose,DialMediaRouteProvider,GlobalMediaControls,HttpsUpgrades,LensOverlay,MediaRouter,PaintHolding,ThirdPartyStoragePartitioning,Translate,AutoDeElevate,RenderDocument,OptimizationHints".split(',')
for f in features:
    ignore = [f'--disable-features={f}']
    ok = test(ignore, f'  disable-feature={f}')
    if not ok:
        print(f'    ^^^ THIS FEATURE CAUSES CRASH')
