test: anki.utils has almost no unit test coverage
pylib/anki/utils.py is one of the most-imported modules in the add-on API, but pylib/tests/test_utils.py only covered the three version helpers:
int_version()int_version_to_str()- the
ValueErrorpath ofint_version()
Everything else in the module was untested, including the helpers that add-ons call directly and that have non-obvious edge cases:
| Helper | Untested behaviour |
|---|---|
ids2str() |
empty list, already-string ids, ordering |
base62() / base91() |
injectivity, the actual alphabet, the empty string for 0 |
join_fields() / split_fields() |
split_fields("") is [""], not []; a field containing the separator does not survive a round trip |
checksum() |
str vs bytes, non-ascii |
invalid_filename() |
the dirsep / platform interaction for / and \ |
tmpfile() / namedtmp() |
remove=False keeps an existing file |
The base62() alphabet is the trap here. The table is string.ascii_letters + string.digits, so it is not the usual base62 ordering, and int(encoded, 62) will not decode it. Anyone reimplementing or porting this needs to know that.
None of these need a collection, so they can all be tested without the backend.
Proposal
Add unit tests for the deterministic helpers in anki.utils, leaving the ones that need a live collection or the backend (strip_html(), field_checksum(), timestamp_id(), max_id()) to the existing collection-backed tests.
This is deliberately one module per PR, following the discussion in #3974 and the coverage goal in milestone 21. It is test: prefixed, so it will need a linked issue like this one.
Related: #5147 (discussion about whether missing-test issues are wanted), #5371.
Source: ankitects/anki