File size: 3,105 Bytes
7e173d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e940395
 
7e173d8
 
 
 
 
 
 
 
 
b5abc97
 
 
7e173d8
 
 
b5abc97
1464612
7e173d8
 
1464612
 
7e173d8
 
 
1464612
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""drAIML WebGPU installer: adds the flags, is idempotent, skips a running browser, and undo removes only its own."""
import json
import os
import subprocess
from pathlib import Path

SH = Path(__file__).resolve().parents[1] / "webgpu" / "installer" / "webgpu-enable.sh"


def run(*a):
    return subprocess.run(["bash", str(SH), *a], capture_output=True, text=True)


def labs(d):
    return json.loads((d / "Local State").read_text())["browser"]["enabled_labs_experiments"]


def test_chromium_flags_round_trip(tmp_path):
    (tmp_path / "Local State").write_text(json.dumps({"browser": {"enabled_labs_experiments": ["smooth-scrolling@2"]}, "x": 1}))
    assert "would add" in run("--dir", str(tmp_path), "--dry-run").stdout and labs(tmp_path) == ["smooth-scrolling@2"]
    assert run("--dir", str(tmp_path)).returncode == 0
    added = labs(tmp_path)
    assert added[0] == "smooth-scrolling@2" and "enable-unsafe-webgpu" in added
    assert "unchanged" in run("--dir", str(tmp_path)).stdout and labs(tmp_path) == added
    assert list(tmp_path.glob("Local State.bak-webgpu-*"))
    run("--dir", str(tmp_path), "--blocklist")      # a second run adding one more flag: undo must remove both
    assert "ignore-gpu-blocklist" in labs(tmp_path)
    run("--dir", str(tmp_path), "--undo")
    assert labs(tmp_path) == ["smooth-scrolling@2"] and json.loads((tmp_path / "Local State").read_text())["x"] == 1


def test_running_browser_is_skipped(tmp_path):
    (tmp_path / "Local State").write_text("{}")
    os.symlink(f"{os.uname().nodename}-{os.getpid()}", tmp_path / "SingletonLock")   # this test process: alive
    r = run("--dir", str(tmp_path))
    assert r.returncode == 3 and "running" in r.stdout and json.loads((tmp_path / "Local State").read_text()) == {}
    ff = tmp_path / "ff"; ff.mkdir(); (ff / "prefs.js").write_text("")
    os.symlink(f"127.0.1.1:+{os.getpid()}", ff / "lock")
    assert run("--dir", str(ff)).returncode == 3 and not (ff / "user.js").exists()


def test_firefox_profile(tmp_path):
    os.symlink("127.0.1.1:+999999999", tmp_path / "lock")   # Firefox's lock format; a pid that is not running
    (tmp_path / "prefs.js").write_text('user_pref("other", 1);\n')
    run("--dir", str(tmp_path))
    assert 'user_pref("dom.webgpu.enabled", true);' in (tmp_path / "user.js").read_text()
    with open(tmp_path / "prefs.js", "a") as f:               # Firefox copies the value into prefs.js on exit
        f.write('user_pref("dom.webgpu.enabled", true);\n')
    run("--dir", str(tmp_path), "--undo")
    assert "dom.webgpu" not in (tmp_path / "user.js").read_text() and "dom.webgpu" not in (tmp_path / "prefs.js").read_text()
    assert 'user_pref("other", 1);' in (tmp_path / "prefs.js").read_text()


def test_firefox_undo_keeps_a_pref_that_was_yours(tmp_path):
    (tmp_path / "prefs.js").write_text('user_pref("dom.webgpu.enabled", true);\n')
    run("--dir", str(tmp_path))
    run("--dir", str(tmp_path), "--undo")
    assert "dom.webgpu" not in (tmp_path / "user.js").read_text()
    assert 'user_pref("dom.webgpu.enabled", true);' in (tmp_path / "prefs.js").read_text()