Files
computer-playbook/tests/unit/python/cli/meta/ci/test_chunks.py
T
kevinveenbirkenbachandClaude Opus 5 575a5a87ac refactor(layout): sort shipped scripts by language and split the unit suite
Every script a role ships now lives under `files/<language>/`, and the unit
suite is split the same way: `tests/unit/python/` beside new sibling trees for
the other languages. `tests/lint/filesystem/test_role_files_layout.py` keeps it
that way; directories whose shape is dictated from outside carry a `.nocheck`
naming the constraint (Docker build contexts, WordPress mu-plugins, the Joomla
extension, Odoo addons, Playwright suites, the `files/test/test.sh` harness
that `test-e2e-cli` resolves generically).

This cannot be applied in halves. Moving a file and repointing what names it
are the same change: 14 references resolve only at deploy time, so the tree is
green either before or after, never between.

Those 14 were found by static resolution over every reference class, not by the
test suite -- a full `make quality-high` was green while all of them were
broken:

  * 7 `src:` values built from a role variable holding a bare filename
    (svc-bkp-remote-2-local, decidim, espocrm, friendica, joomla, pixelfed x2).
    The variable doubles as the destination name inside the container, so the
    language segment belongs at the call site, not in the variable.
  * `sys-service/tasks/04_base.yml` resolved every consumer's systemd script
    through `first_found(..., errors='strict')` over `files/script.{sh,py}`.
    18 roles ship exactly that and have no template fallback: the play would
    have died on each.
  * `sys-front-inj-javascript` fell back to `<role>/files/javascript.js`; the
    interface now reads `files/javascript/javascript.js`.
  * `sys-front-inj-logout/templates/head_sub.j2` built the cache-busting mtime
    path through `path_absolute(... 'files', ...)`.
  * 4 `ansible.builtin.script:` invocations naming a bare command
    (pull.py, prune.sh, set_postgres_superuser_password.sh, ganesha-serve.sh).
  * `dev-python/files/shell/install.sh` pointed at a sibling role's script.

Depth-dependent paths inside the moved trees were bumped accordingly: 59
`parents[N]` computations, 12 relative climbs, and the per-role recover CLI,
whose path `cli/administration/recover/recoverers.py` still assembled from the
pre-move location.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-22 01:31:09 +02:00

101 lines
3.4 KiB
Python

from __future__ import annotations
import unittest
from cli.meta.ci import chunks
class TestSliceChunks(unittest.TestCase):
def test_rows_are_cut_into_consecutive_blocks(self) -> None:
self.assertEqual(chunks.slice_chunks([0, 1, 2, 3, 4], 2), [[0, 1], [2, 3], [4]])
def test_an_empty_list_yields_no_block(self) -> None:
self.assertEqual(chunks.slice_chunks([], 3), [])
def test_a_zero_size_is_rejected(self) -> None:
with self.assertRaises(ValueError):
chunks.slice_chunks([1], 0)
class TestOffset(unittest.TestCase):
def test_the_head_leads_when_no_offset_is_asked_for(self) -> None:
plan = chunks.plan([], list(range(256)), size=80, blocks=2, budget=256)
self.assertEqual(plan[0][0], 0)
def test_an_offset_moves_the_window_by_exactly_that_many_rows(self) -> None:
plan = chunks.plan(
[], list(range(256)), size=80, blocks=2, budget=256, offset=5
)
self.assertEqual(plan[0][0], 5)
def test_an_offset_past_the_tail_yields_nothing_instead_of_wrapping(self) -> None:
plan = chunks.plan(
[], list(range(10)), size=80, blocks=2, budget=256, offset=99
)
self.assertEqual(plan, [])
def test_a_negative_offset_reads_as_the_head(self) -> None:
plan = chunks.plan(
[], list(range(20)), size=80, blocks=2, budget=256, offset=-3
)
self.assertEqual(plan[0][0], 0)
class TestPlan(unittest.TestCase):
def test_priority_gets_its_own_short_chunk(self) -> None:
plan = chunks.plan(
list(range(5)),
list(range(100, 300)),
size=80,
blocks=3,
budget=211,
)
self.assertEqual([len(c) for c in plan], [5, 80, 80])
self.assertEqual(plan[0], list(range(5)))
def test_the_seam_chunk_is_never_topped_up_with_regular_rows(self) -> None:
plan = chunks.plan(
list(range(5)),
list(range(100, 300)),
size=80,
blocks=3,
budget=211,
)
self.assertTrue(all(row < 5 for row in plan[0]))
def test_without_priority_the_first_chunk_is_regular(self) -> None:
plan = chunks.plan([], list(range(200)), size=80, blocks=3, budget=211)
self.assertEqual([len(c) for c in plan], [80, 80, 40])
def test_the_block_count_bounds_the_sweep(self) -> None:
plan = chunks.plan([], list(range(500)), size=80, blocks=2, budget=500)
self.assertEqual(len(plan), 2)
def test_the_job_budget_bounds_the_sweep(self) -> None:
plan = chunks.plan([], list(range(500)), size=80, blocks=3, budget=100)
self.assertEqual(sum(len(c) for c in plan), 100)
def test_priority_alone_may_fill_every_block(self) -> None:
plan = chunks.plan(
list(range(300)), list(range(50)), size=80, blocks=3, budget=300
)
self.assertEqual([len(c) for c in plan], [80, 80, 80])
def test_priority_is_never_moved_by_the_offset(self) -> None:
plan = chunks.plan(
list(range(5)),
list(range(100, 300)),
size=80,
blocks=3,
budget=211,
offset=40,
)
self.assertEqual(plan[0], list(range(5)))
def test_an_empty_selection_yields_no_chunk(self) -> None:
self.assertEqual(chunks.plan([], [], size=80, blocks=3, budget=211), [])
if __name__ == "__main__":
unittest.main()