import json
from pathlib import Path
import subprocess

import pytest

SOURCE=Path(__file__).resolve().parents[1]/'pallet/assets/pallet_review_filter_sync.js'
NODE=Path('/tmp/opencode/pallet_review_browser_tools/node')


@pytest.mark.parametrize('scope,state,changed,want_scope,want_state',[
    ('todo','correct','state','all','correct'),
    ('class13-recheck','correct','state','class13-all','correct'),
    ('completed','draft','state','all','draft'),
    ('completed','all','state','all','all'),
    ('completed','pending','state','all','pending'),
    ('completed','defer','state','all','defer'),
    ('completed','all','scope','completed','correct'),
    ('completed','draft','scope','completed','correct'),
    ('todo','correct','scope','todo','all'),
    ('class13-recheck','correct','scope','class13-recheck','all'),
    ('priority','correct','state','priority','correct'),
    ('audit','correct','state','audit','correct'),
    ('diagnostic','correct','state','diagnostic','correct'),
    ('class13-all','correct','state','class13-all','correct'),
    ('all','correct','state','all','correct'),
    ('todo','draft','state','todo','draft'),
    ('class13-recheck','draft','state','class13-recheck','draft'),
])
def test_filter_coordination(scope,state,changed,want_scope,want_state):
    source=SOURCE.read_text().split('const filterSyncCards=')[0]
    result=subprocess.run([str(NODE),'-e',source+'\nconsole.log(JSON.stringify(coordinatedReviewFilters(...'+json.dumps([scope,state,changed])+')));'],check=True,capture_output=True,text=True)
    assert json.loads(result.stdout)==dict(scope=want_scope,state=want_state)


def test_filter_patch_never_changes_saved_values_or_opened_navigation():
    text=SOURCE.read_text()
    assert 'navigationKeys=' not in text and 'navigationKeys =' not in text
    assert 'localStorage.setItem' not in text and "request('annotations/" not in text
    assert 'reviews[' not in text and 'catalog.targets=' not in text
