Files
OpenBench/UnitTests/test_pipeline_defaults.py
claude 48027f1142 Pipelines: the settings of a step, filled in the way Create Test fills them in
A pure module that turns an engine, a preset and a dev branch into every
setting a Test step needs: the repository and default network, the default
preset, then the chosen one, with both_ going to dev and base alike. With it
a pipeline only has to be told the dev branch, as a single test does.

What no preset can fill in is reported instead of guessed. Includes the unit
tests, which run without Django.
2026-10-13 21:28:35 +02:00

305 lines
14 KiB
Python

#!/usr/bin/python3
import os
import sys
import unittest
# Needed to include from ../OpenBench/pipeline_defaults.py, which has no Django dependencies
PARENT = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.path.pardir)
sys.path.insert(0, os.path.abspath(PARENT))
from OpenBench.pipeline_defaults import *
from OpenBench.pipeline_graph import ALLOWED_FIELDS, REQUIRED_FIELDS, has_errors, validate_graph
# The shape of a real engine config ( Engines/Eleanor.json ), presets only
ENGINE = {
'source' : 'https://github.com/o/Engine',
'test_presets' : {
'default' : {
'base_branch' : 'main', 'book_name' : 'UHO.epd',
'test_bounds' : '[0.00, 5.00]', 'test_confidence' : '[0.05, 0.05]',
'win_adj' : 'movecount=3 score=400', 'draw_adj' : 'movenumber=40 movecount=8 score=10',
},
'STC' : { 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '8.0+0.08', 'workload_size' : 32 },
'LTC' : { 'both_options' : 'Threads=1 Hash=64', 'both_time_control' : '40.0+0.4', 'workload_size' : 8 },
},
}
NETWORKS = [
{ 'engine' : 'Engine', 'name' : 'old.nn', 'sha256' : 'aaaaaaaa', 'default' : False },
{ 'engine' : 'Engine', 'name' : 'newest.nn', 'sha256' : 'bbbbbbbb', 'default' : True },
{ 'engine' : 'Other', 'name' : 'other.nn', 'sha256' : 'cccccccc', 'default' : True },
]
def build(preset='STC', branch='dev', engine_config=ENGINE, **kwargs):
return test_payload('Engine', engine_config, preset, branch, **kwargs)
def with_presets(**presets):
return { 'source' : ENGINE['source'], 'test_presets' : dict(ENGINE['test_presets'], **presets) }
class PresetTests(unittest.TestCase):
def test_the_names_leave_out_the_defaults(self):
self.assertEqual(preset_names(ENGINE), ['STC', 'LTC'])
def test_an_engine_without_presets_has_none(self):
self.assertEqual(preset_names({}), [])
self.assertEqual(preset_names({ 'test_presets' : None }), [])
def test_both_goes_to_the_dev_and_the_base_alike(self):
payload, _ = build('LTC')
self.assertEqual((payload['dev_options'], payload['base_options']), ('Threads=1 Hash=64', 'Threads=1 Hash=64'))
self.assertEqual((payload['dev_time_control'], payload['base_time_control']), ('40.0+0.4', '40.0+0.4'))
def test_the_defaults_come_first_and_a_preset_wins_over_them(self):
engine = with_presets(Slow={ 'test_bounds' : '[-1.00, 3.00]', 'base_branch' : 'master',
'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1' })
payload, missing = build('Slow', engine_config=engine)
self.assertEqual(payload['test_bounds'], '[-1.00, 3.00]') # The preset
self.assertEqual(payload['base_branch'], 'master') # The preset
self.assertEqual(payload['test_confidence'], '[0.05, 0.05]') # The default, untouched
self.assertEqual(missing, [])
def test_the_time_control_of_a_preset_for_showing(self):
self.assertEqual((time_control(ENGINE, 'STC'), time_control(ENGINE, 'LTC')), ('8.0+0.08', '40.0+0.4'))
self.assertEqual(time_control(ENGINE, 'nope'), '')
class PayloadTests(unittest.TestCase):
def test_one_branch_is_all_that_it_takes(self):
payload, missing = build(branch='my-branch')
self.assertEqual(missing, [])
self.assertEqual(payload['dev_branch'], 'my-branch')
self.assertEqual(payload['base_branch'], 'main')
self.assertEqual(payload['test_mode'], 'SPRT')
self.assertEqual(payload['workload_size'], '32')
def test_it_starts_out_like_the_create_test_page(self):
payload, _ = build()
for key, value in FORM_DEFAULTS.items():
self.assertEqual(payload[key], value, key)
self.assertEqual(payload['syzygy_wdl'], 'DISABLED') # The page says disabled, whatever the editor said
self.assertEqual((payload['priority'], payload['throughput']), ('0', '1000'))
def test_every_setting_that_a_step_needs_is_there_and_is_text(self):
for preset in ('STC', 'LTC'):
payload, missing = build(preset)
self.assertEqual(missing, [])
for key in REQUIRED_FIELDS['TEST'] + ['test_bounds', 'test_confidence']:
self.assertIn(key, payload, key)
self.assertTrue(all(isinstance(value, str) for value in payload.values()))
self.assertTrue(set(payload) <= ALLOWED_FIELDS)
def test_engine_and_repo_are_the_engines_own_unless_the_profile_says_otherwise(self):
payload, _ = build()
self.assertEqual((payload['dev_engine'], payload['base_engine']), ('Engine', 'Engine'))
self.assertEqual((payload['dev_repo'], payload['base_repo']), ('https://github.com/o/Engine',) * 2)
payload, _ = build(repos={ 'Engine' : 'https://github.com/me/Engine' })
self.assertEqual((payload['dev_repo'], payload['base_repo']), ('https://github.com/me/Engine',) * 2)
payload, _ = build(repos={ 'Another' : 'https://github.com/me/Another' }) # Not for this engine
self.assertEqual(payload['dev_repo'], 'https://github.com/o/Engine')
payload, _ = build(repos=None) # A profile with nothing saved
self.assertEqual(payload['dev_repo'], 'https://github.com/o/Engine')
def test_the_base_branch_can_be_chosen(self):
payload, missing = build(base_branch='release')
self.assertEqual((payload['base_branch'], missing), ('release', []))
def test_a_missing_base_branch_is_reported_not_guessed(self):
engine = { 'source' : 'x', 'test_presets' : { 'STC' : ENGINE['test_presets']['STC'],
'default' : { 'book_name' : 'b.epd', 'test_bounds' : '[0.00, 5.00]', 'test_confidence' : '[0.05, 0.05]' } } }
self.assertEqual(build(engine_config=engine)[1], ['base_branch'])
self.assertEqual(build(engine_config=engine, base_branch='main')[1], [])
def test_what_the_presets_leave_out_is_reported(self):
engine = { 'source' : 'x', 'test_presets' : { 'default' : { 'base_branch' : 'main', 'book_name' : 'b.epd',
'test_bounds' : '[0.00, 5.00]', 'test_confidence' : '[0.05, 0.05]' }, 'Bare' : {} } }
payload, missing = build('Bare', engine_config=engine)
self.assertEqual(sorted(missing), ['base_options', 'base_time_control', 'dev_options', 'dev_time_control'])
self.assertEqual(payload['workload_size'], FALLBACKS['workload_size']) # These have a standard value
self.assertEqual(payload['win_adj'], FALLBACKS['win_adj'])
def test_an_engine_that_names_no_book_gets_the_first_one_there_is(self):
engine = with_presets(default={ 'base_branch' : 'main', 'test_bounds' : '[0.00, 5.00]', 'test_confidence' : '[0.05, 0.05]' })
self.assertEqual(build(engine_config=engine, books=['one.epd', 'two.epd'])[0]['book_name'], 'one.epd')
self.assertEqual(build(engine_config=engine)[1], ['book_name'])
def test_an_unknown_preset_only_has_the_defaults_to_go_on(self):
_, missing = build('nope')
self.assertIn('dev_time_control', missing)
def test_whatever_else_a_preset_holds_is_left_out(self):
engine = with_presets(Odd={ 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1', 'rubbish' : 'x' })
payload, _ = build('Odd', engine_config=engine)
self.assertNotIn('rubbish', payload)
def test_a_bench_that_a_preset_gives_is_kept_and_otherwise_it_is_left_to_autofill(self):
self.assertNotIn('dev_bench', build()[0])
engine = with_presets(Fixed={ 'both_bench' : 12345, 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1' })
payload, _ = build('Fixed', engine_config=engine)
self.assertEqual((payload['dev_bench'], payload['base_bench']), ('12345', '12345'))
class ModeTests(unittest.TestCase):
def test_a_preset_of_fixed_games_is_a_games_test(self):
engine = with_presets(Fixed={ 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1', 'test_max_games' : 40000 })
payload, missing = build('Fixed', engine_config=engine)
self.assertEqual((payload['test_mode'], payload['test_max_games'], missing), ('GAMES', '40000', []))
self.assertNotIn('test_bounds', payload); self.assertNotIn('test_confidence', payload)
def test_sprt_leaves_the_number_of_games_out(self):
engine = with_presets(default=dict(ENGINE['test_presets']['default'], test_max_games=100))
payload, _ = build(engine_config=engine)
self.assertEqual(payload['test_mode'], 'GAMES') # The last word of the page, as it is for a person
self.assertEqual(build()[0]['test_mode'], 'SPRT')
self.assertNotIn('test_max_games', build()[0])
def test_bounds_after_games_make_it_sprt_again(self):
engine = with_presets(Both={ 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1',
'test_max_games' : 100, 'test_bounds' : '[0.00, 2.00]' })
payload, _ = build('Both', engine_config=engine)
self.assertEqual(payload['test_mode'], 'SPRT')
self.assertNotIn('test_max_games', payload)
class NetworkTests(unittest.TestCase):
def test_the_default_network_of_the_engine(self):
self.assertEqual(build(networks=NETWORKS)[0]['dev_network'], 'bbbbbbbb')
self.assertEqual(build(networks=NETWORKS)[0]['base_network'], 'bbbbbbbb')
def test_no_default_means_no_network(self):
self.assertEqual(build(networks=NETWORKS[:1])[0]['dev_network'], '')
self.assertEqual(build()[0]['dev_network'], '')
def test_a_network_of_another_engine_is_never_used(self):
self.assertEqual(default_network(NETWORKS, 'Nothing'), '')
self.assertEqual(default_network(NETWORKS, 'Other'), 'cccccccc')
def test_the_last_default_wins_like_it_does_on_the_page(self):
twice = NETWORKS + [{ 'engine' : 'Engine', 'name' : 'later.nn', 'sha256' : 'dddddddd', 'default' : True }]
self.assertEqual(default_network(twice, 'Engine'), 'dddddddd')
def test_a_preset_may_name_a_network_or_give_its_hash(self):
for wanted in ('old.nn', 'aaaaaaaa'):
engine = with_presets(Net={ 'both_network' : wanted, 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1' })
self.assertEqual(build('Net', engine_config=engine, networks=NETWORKS)[0]['dev_network'], 'aaaaaaaa', wanted)
def test_a_preset_may_ask_for_none(self):
engine = with_presets(Net={ 'both_network' : 'None', 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1' })
self.assertEqual(build('Net', engine_config=engine, networks=NETWORKS)[0]['dev_network'], '')
def test_a_network_that_does_not_exist_changes_nothing(self):
engine = with_presets(Net={ 'both_network' : 'ghost.nn', 'both_options' : 'Threads=1 Hash=8', 'both_time_control' : '1+1' })
self.assertEqual(build('Net', engine_config=engine, networks=NETWORKS)[0]['dev_network'], 'bbbbbbbb')
def test_having_no_network_is_not_a_missing_setting(self):
self.assertEqual(build(networks=[])[1], [])
class BranchTests(unittest.TestCase):
def test_what_a_branch_may_look_like(self):
for text in ('main', 'feature/new-search_2', 'v1.2.3', 'a' * 255, '0' * 40, ' padded '):
self.assertEqual(clean_branch(text), text.strip(), text)
def test_what_it_may_not(self):
for text in (None, '', ' ', 'two words', 'tab\there', 'quote"d', "it's", 'back\\slash', 'a' * 256, 'new\nline'):
self.assertIsNone(clean_branch(text), repr(text))
class GraphTests(unittest.TestCase):
def steps(self, *presets):
return [(preset, build(preset)[0]) for preset in presets]
def test_a_graph_of_one_step_is_sound(self):
graph = quick_graph(self.steps('STC'))
self.assertFalse(has_errors(validate_graph(graph)))
self.assertEqual((graph['start'], list(graph['steps']), graph['edges']), ('step1', ['step1'], []))
def test_the_next_one_starts_when_the_one_before_passed(self):
graph = quick_graph(self.steps('STC', 'LTC'))
issues = validate_graph(graph)
self.assertFalse(has_errors(issues))
self.assertEqual([issue['level'] for issue in issues], ['info', 'info']) # Only "it ends here if it fails or stops"
self.assertEqual(graph['edges'], [{ 'from' : 'step1', 'on' : 'passed', 'to' : 'step2' }])
self.assertEqual([step['name'] for step in graph['steps'].values()], ['STC', 'LTC'])
def test_any_number_of_steps_make_a_chain(self):
graph = quick_graph(self.steps('STC', 'LTC', 'STC'))
self.assertFalse(has_errors(validate_graph(graph)))
self.assertEqual([(e['from'], e['to']) for e in graph['edges']], [('step1', 'step2'), ('step2', 'step3')])
def test_the_same_name_twice_is_told_apart(self):
graph = quick_graph(self.steps('STC', 'STC'))
self.assertEqual([step['name'] for step in graph['steps'].values()], ['STC', 'STC (2)'])
def test_a_long_name_is_cut_to_what_a_step_may_have(self):
graph = quick_graph([('x' * 100, build()[0])])
self.assertEqual(len(graph['steps']['step1']['name']), 64)
self.assertFalse(has_errors(validate_graph(graph)))
def test_each_step_has_settings_of_its_own(self):
graph = quick_graph(self.steps('STC', 'LTC'))
first, second = graph['steps']['step1']['payload'], graph['steps']['step2']['payload']
self.assertEqual((first['dev_time_control'], second['dev_time_control']), ('8.0+0.08', '40.0+0.4'))
self.assertEqual((first['dev_branch'], second['dev_branch']), ('dev', 'dev'))
def test_the_name_says_what_is_tested_and_what_runs(self):
self.assertEqual(quick_name('feature/x', ['STC', 'LTC']), 'feature/x: STC then LTC')
self.assertEqual(quick_name('feature/x', ['STC']), 'feature/x: STC')
def test_the_name_always_fits(self):
self.assertEqual(len(quick_name('b' * 255, ['STC', 'LTC'])), 128)
if __name__ == '__main__':
unittest.main()