Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lab/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,12 @@ up to 5 s + 2 s for the journal, and reads `state.json` again. Arguments are clo

- `describe_program` parses `describe_program <JSON>` and returns it as `program`:
`{"id", "options", "prechecks", "schema": 1}` (the option entries as the app prints them) with
`prechecks_supported`. `prechecks` is the string `unsupported` until the program declares a
list (#485); that is a valid reply, not an error. A list is passed through.
`prechecks_supported`. `prechecks` is the string `unsupported` when the program declares no
list; that is a valid reply, not an error, and never means the checks passed. Otherwise it
is the program's checks in run order, each `{"name", "needs", "skippable"}` (#511): `name` a
non-empty string, unique in the list; `needs` the one line of what the program needs;
`skippable` whether the user can turn the check off. An empty list means the program needs
nothing. The list is passed through.
- `game_open` parses `game_open yes` / `game_open no REASON` and returns `open` and `reason`
(`null`, `home`, `user_select`, `black` or `no_video`). `open: false` is an answer, not a tool
error; it does not tell one game from another (`ensure_game` does).
Expand All @@ -210,7 +214,7 @@ up to 5 s + 2 s for the journal, and reads `state.json` again. Arguments are clo
|---|---|
| `describe_program: the selected panel is not a program.` | `panel_not_program` |
| `schema` other than 1 | `unsupported_schema` |
| a failed line, no single reply line and `ok` line, bad JSON, a missing `id`/`options`/`prechecks`, an unknown `game_open` reason | `app_unavailable` (the journal in `evidence`) |
| a failed line, no single reply line and `ok` line, bad JSON, a missing `id`/`options`/`prechecks`, a precheck entry without a name, `needs` or boolean `skippable`, a name twice, an unknown `game_open` reason | `app_unavailable` (the journal in `evidence`) |
| a reply from another instance | `app_unavailable`, `evidence.reason` `instance_replaced` |
| no journal within 7 s | `timeout`; the request is withdrawn from the inbox |

Expand Down
21 changes: 18 additions & 3 deletions lab/server/pa_lab/app_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

- `describe_program` prints `describe_program <JSON>`, the selected program's id and the static
contract of its options: `{"id", "options", "prechecks", "schema": 1}`. `prechecks` is the
string `unsupported` until the program declares a precheck list (#485); a list after that.
string `unsupported` when the program declares no precheck list; otherwise the list in run
order, each `{"name", "needs", "skippable"}` with a unique non-empty name (#511).
- `game_open` prints `game_open yes`, or `game_open no REASON` with REASON `home`,
`user_select`, `black` or `no_video`. It never fails the line and does not tell one game from
another.
Expand Down Expand Up @@ -83,6 +84,21 @@ def _one_reply(lines: List[str], pattern: 're.Pattern[str]', command: str):
return replies[0]


def _good_precheck(entry: Any) -> bool:
return (isinstance(entry, dict) and isinstance(entry.get('name'), str) and bool(entry['name'])
and isinstance(entry.get('needs'), str) and isinstance(entry.get('skippable'), bool))


def _good_prechecks(prechecks: Any) -> bool:
"""`unsupported`, or a list of well-formed entries with unique names."""
if prechecks == PRECHECKS_UNSUPPORTED:
return True
if not isinstance(prechecks, list) or not all(_good_precheck(entry) for entry in prechecks):
return False
names = [entry['name'] for entry in prechecks]
return len(names) == len(set(names))


def _check_contract(doc: Any, lines: List[str]) -> Dict[str, Any]:
if not isinstance(doc, dict):
raise Refused('app_unavailable', 'describe_program did not print a JSON object.', {'journal': lines})
Expand All @@ -94,8 +110,7 @@ def _check_contract(doc: Any, lines: List[str]) -> Dict[str, Any]:
problems.append('id')
if not isinstance(doc.get('options'), list) or not all(isinstance(o, dict) for o in doc['options']):
problems.append('options')
prechecks = doc.get('prechecks')
if prechecks != PRECHECKS_UNSUPPORTED and not isinstance(prechecks, list):
if not _good_prechecks(doc.get('prechecks')):
problems.append('prechecks')
if problems:
raise Refused('app_unavailable', 'describe_program printed a bad {}.'.format(', '.join(problems)),
Expand Down
39 changes: 34 additions & 5 deletions lab/server/tests/test_app_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,40 @@ def test_the_request_is_one_fixed_line(self):
self.assertEqual(lines[1:], ['describe_program'])

def test_a_precheck_list_is_supported(self):
doc = dict(PROGRAM_CONTRACT, prechecks=[{'name': 'game_open', 'needs': 'a game in front'}])
self.app.replies['describe_program'] = describe_line(doc)
result = self.ask('describe_program')
self.assertTrue(result['prechecks_supported'])
self.assertEqual(result['program']['prechecks'], doc['prechecks'])
cases = [
dict(name='two checks, in order',
input=[{'name': 'black_border', 'needs': 'No black border.', 'skippable': False},
{'name': 'free_party_slots', 'needs': '1 free party slot.', 'skippable': True}],
expectation=['black_border', 'free_party_slots']),
dict(name='an empty list needs nothing', input=[], expectation=[]),
]
for case in cases:
with self.subTest(case['name']):
self.setUp()
doc = dict(PROGRAM_CONTRACT, prechecks=case['input'])
self.app.replies['describe_program'] = describe_line(doc)
result = self.ask('describe_program')
self.assertTrue(result['prechecks_supported'])
self.assertEqual([c['name'] for c in result['program']['prechecks']], case['expectation'])
self.assertEqual(result['program']['prechecks'], case['input'])

def test_a_bad_precheck_entry_is_refused(self):
good = {'name': 'black_border', 'needs': 'No black border.', 'skippable': False}
cases = [
dict(name='not an object', input=['black_border'], expectation='prechecks'),
dict(name='no name', input=[{k: v for k, v in good.items() if k != 'name'}], expectation='prechecks'),
dict(name='empty name', input=[dict(good, name='')], expectation='prechecks'),
dict(name='needs not a string', input=[dict(good, needs=None)], expectation='prechecks'),
dict(name='skippable not a bool', input=[dict(good, skippable='no')], expectation='prechecks'),
dict(name='a name twice', input=[good, dict(good, skippable=True)], expectation='prechecks'),
]
for case in cases:
with self.subTest(case['name']):
self.setUp()
self.app.replies['describe_program'] = describe_line(dict(PROGRAM_CONTRACT, prechecks=case['input']))
refusal = self.refusal('describe_program')
self.assertEqual(refusal['code'], 'app_unavailable')
self.assertIn(case['expectation'], refusal['message'])

def test_refusals(self):
bad_options = dict(PROGRAM_CONTRACT, options=['LANGUAGE'])
Expand Down
Loading