import json
from pathlib import Path
import subprocess

import pytest

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


@pytest.mark.parametrize('args,want',[
    (('class13-all','correct','16','class'),('all','correct','16')),
    (('class13-recheck','all','16','class'),('all','all','16')),
    (('class13-all','all','','class'),('all','all','')),
    (('class13-recheck','all','13','class'),('class13-recheck','all','13')),
    (('class13-recheck','correct','13','state'),('class13-all','correct','13')),
    (('class13-recheck','correct','','state'),('all','correct','')),
    (('class13-all','correct','16','state'),('all','correct','16')),
    (('todo','correct','16','state'),('all','correct','16')),
    (('priority','correct','16','state'),('all','correct','16')),
    (('audit','correct','16','state'),('all','correct','16')),
    (('diagnostic','correct','16','state'),('all','correct','16')),
    (('completed','correct','16','class'),('all','correct','16')),
    (('all','correct','16','scope'),('all','all','16')),
    (('all','draft','16','scope'),('all','all','16')),
    (('completed','all','16','scope'),('completed','correct','16')),
    (('completed','draft','16','state'),('all','draft','16')),
    (('todo','correct','16','scope'),('todo','all','16')),
    (('class13-recheck','correct','16','scope'),('class13-recheck','all','13')),
    (('class13-all','all','','scope'),('class13-all','all','13')),
    (('audit','draft','16','scope'),('audit','draft','16')),
])
def test_universal_transitions(args,want):
    code=SOURCE.read_text().split('const universalBaseCards=')[0]
    result=subprocess.run([str(NODE),'-e',code+'\nconsole.log(JSON.stringify(universalReviewFilters(...'+json.dumps(args)+')));'],check=True,capture_output=True,text=True)
    assert json.loads(result.stdout)==dict(zip(('scope','state','classId'),want))


def test_entire_class_state_scope_matrix_not_class13_only():
    code=SOURCE.read_text().split('const universalBaseCards=')[0]
    code+='''
for(const cls of ['',...Array.from({length:150},(_,i)=>String(i+1))]){
 for(const scope of ['todo','priority','audit','diagnostic','completed','class13-recheck','class13-all','all']){
  const next=universalReviewFilters(scope,'correct',cls,'state');
  const expected=cls==='13'&&scope.startsWith('class13-')?'class13-all':'all';
  if(next.scope!==expected||next.state!=='correct'||next.classId!==cls)throw Error('Completed transition: '+cls+scope);
  const changed=universalReviewFilters(scope,'all',cls,'class');
  if(cls!=='13'&&changed.scope.startsWith('class13-'))throw Error('Stale class13 scope: '+cls);
 }
 for(const state of ['all','correct','draft','pending','default_normal','defer']){
  const next=universalReviewFilters('all',state,cls,'scope');
  if(next.state!=='all'||next.classId!==cls)throw Error('All history retained state');
 }
}
console.log('matrix passed');
'''
    result=subprocess.run([str(NODE),'-e',code],check=True,capture_output=True,text=True);assert result.stdout.strip()=='matrix passed'


def test_patch_does_not_mutate_labels_drafts_or_opened_sequence():
    source=SOURCE.read_text()
    for forbidden in ('navigationKeys=','navigationKeys =','localStorage.setItem',"request('annotations/",'catalog.targets=','reviews[key]='):
        assert forbidden not in source
