|
| 1 | +"""This file defines the core types used in the Markus Autotesting system.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | +from functools import reduce |
| 5 | +from typing import Annotated, Any, Optional, Union |
| 6 | + |
| 7 | +from msgspec import Meta, Struct |
| 8 | + |
| 9 | + |
| 10 | +class _AutotestSettings(Struct, kw_only=True): |
| 11 | + """The main test settings data type.""" |
| 12 | + |
| 13 | + testers: Annotated[list, Meta(title="Testers", min_length=1)] |
| 14 | + """The list of testers configured for these settings.""" |
| 15 | + |
| 16 | + _user: Optional[str] = None |
| 17 | + """The user who last updated the test settings.""" |
| 18 | + |
| 19 | + _last_access: Optional[int] = None |
| 20 | + """The last time the test settings were updated.""" |
| 21 | + |
| 22 | + _files: Optional[str] = None |
| 23 | + """A list of test files associated with these settings.""" |
| 24 | + |
| 25 | + _env_status: Optional[str] = None |
| 26 | + """The status of the test environment creation for these settings.""" |
| 27 | + |
| 28 | + _error: Optional[str] = None |
| 29 | + """An error message, populated when creating a test environment fails.""" |
| 30 | + |
| 31 | + |
| 32 | +def create_autotest_settings_class(tester_classes: list[type]) -> type[Struct]: |
| 33 | + """Dynamically create an AutotestSettings data type. |
| 34 | +
|
| 35 | + This uses _AutotestSettings as the base class, replacing the `testers` annotation |
| 36 | + with a union of the provided tester_classes. |
| 37 | + """ |
| 38 | + return type( |
| 39 | + "AutotestSettings", |
| 40 | + (_AutotestSettings,), |
| 41 | + { |
| 42 | + "__annotations__": { |
| 43 | + "testers": Annotated[ |
| 44 | + list[reduce(lambda a, b: Union[a, b], tester_classes)], |
| 45 | + Meta(title="Testers", min_length=1), |
| 46 | + ] |
| 47 | + } |
| 48 | + }, |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +class BaseTesterSettings( |
| 53 | + Struct, |
| 54 | + kw_only=True, |
| 55 | + tag_field="tester_type", |
| 56 | + tag=lambda x: x.removesuffix("TesterSettings").lower(), |
| 57 | +): |
| 58 | + """The settings for an individual tester. |
| 59 | +
|
| 60 | + This is a tagged union of various testers, using `tester_type` as the discriminator field. |
| 61 | + """ |
| 62 | + |
| 63 | + test_data: Optional[Any] = None |
| 64 | + """The settings for the tester.""" |
| 65 | + |
| 66 | + @property |
| 67 | + def tester_type(self) -> str: |
| 68 | + return self.__class__.__name__.removesuffix("TesterSettings").lower() |
| 69 | + |
| 70 | + _env: Optional[dict[str, Any]] = None |
| 71 | + """Environment variables to pass to the tester.""" |
| 72 | + |
| 73 | + |
| 74 | +class BaseTestData( |
| 75 | + Struct, |
| 76 | + kw_only=True, |
| 77 | +): |
| 78 | + """Base class for all `test_data` fields. |
| 79 | +
|
| 80 | + This is subclassed to specify tester-specific `test_data` settings. |
| 81 | + """ |
| 82 | + |
| 83 | + category: Annotated[ |
| 84 | + list[Annotated[str, Meta(extra_json_schema={"enum": [], "enumNames": []})]], |
| 85 | + Meta(title="Category", extra_json_schema={"uniqueItems": True}), |
| 86 | + ] |
| 87 | + |
| 88 | + script_files: Annotated[ |
| 89 | + list[AutotestFile], |
| 90 | + Meta(title="Test files", min_length=1, extra_json_schema={"uniqueItems": True}), |
| 91 | + ] |
| 92 | + """The file(s) that contain the tests to execute.""" |
| 93 | + |
| 94 | + timeout: Annotated[int, Meta(title="Timeout")] = 30 |
| 95 | + """The timeout in seconds for this test group.""" |
| 96 | + |
| 97 | + feedback_file_names: Annotated[list[str], Meta(title="Feedback files")] |
| 98 | + """A list of file paths generated by executing this test group.""" |
| 99 | + |
| 100 | + extra_info: dict = {} |
| 101 | + """TODO""" |
| 102 | + |
| 103 | + |
| 104 | +AutotestFile = Annotated[str, Meta(extra_json_schema={"enum": []})] |
| 105 | +"""A placeholder in the JSON schema representing one of the files provided to the autotesting environment.""" |
0 commit comments