Files
shopdb-flask/tests/test_plugin_prune_schema.py
cproudlock 534f179ee6 Make main the lean product a site receives
main was 16 days and 259 commits behind the line the product is actually built
on, at contract 0.30.0 against 0.35.0, with no per-PC credential code at all. It
was neither current nor curated - just stale. This brings it forward and, at the
same time, makes it mean something: main is what a receiving site installs, and
nothing else.

WHAT IT CARRIES. Twelve plugins - backups, computers, endpoints, guides,
knowledgebase, machines, network, notifications, printers, slides, tools,
warranty - plus applications, which is manifest-less core and always ships. The
only manifest dependency inside that set is endpoints on computers, and both are
in it, so the closure is self-contained.

WHAT IT DOES NOT. employees, geenforce, measuringtools, printedparts and usb
stay on the site line. So do the Windows installer tree, the VM rehearsal
helpers, the agent tooling config, one site's data loader, the internal audit
and findings records, and the planner spreadsheet. 1,135 tracked files against
1,438.

A REMOVAL THAT BREAKS THE APP IS NOT A REMOVAL, and two nearly shipped:

deploy/site-profile-universal.json still named all seventeen plugins. It is the
file docs/DEPLOY.md tells a site to provision from, so a fresh install would
have failed at `flask plugin apply-profile` with PluginError - the first thing a
receiving site does, broken by the branch meant to serve them. It now names the
twelve that exist, and its comment says the two must agree.

agent/ was never refreshed. Whoever populated this branch copied shopdb/,
plugins/, frontend/ and tests/ from the site line and left the agent tree at its
September state: 22 C# and PowerShell sources missing, including AgentCredentials,
EnrollmentService, AgentHttpFactory and the fleet-alert client, plus one file the
site line had deleted still present. The old tree is internally consistent, so it
compiles, and nothing in CI builds it - every gate was green over a server that
speaks enrollment and per-PC credentials to a bundled agent source that cannot.
Resynced whole.

DECISIONS THIS ENCODES, rather than defers:

- The plugin-authoring lab builds printedparts, whose manifest depends on
  employees, so it could not stay. The lab remains on the site line and main
  ships no build-along tutorial. That is a real loss for an adopting developer
  and is recorded here rather than discovered later.
- Recognition, Training and Recertification notifications are no longer seeded.
  Each splits one notification into a card per employee and renders a photo,
  which needs a plugin this branch does not bundle. Seeding them would offer an
  author a type that cannot resolve anybody. A site that installs employees gets
  them from that plugin's own seed.
- Windows/IIS is documented as the legacy path throughout, and the README leads
  with Docker, because that is what a receiving site gets.

The API description is regenerated for THIS branch - 347 paths and 483
operations, down from 415 and 573 - so the published contract describes what is
here rather than what the site line serves. docs/CONFIG.md lost the two
measuring-tool label keys the code no longer declares, which two lanes had
decided opposite ways.

Suite: 3124 passed, 6 skipped. Naming and style gates pass. The docs gates run
rather than skip, and every index line count is derived from the file it names.
2026-09-19 11:24:40 -04:00

111 lines
4.9 KiB
Python

"""Tests for `flask plugin prune-schema` (ADR-014 schema-lean per-site).
The shared core Alembic baseline creates every plugin's tables, so a lean site
that omits a plugin still carries that plugin's (empty) tables. prune-schema
drops the tables of every not-installed plugin, leaving core + chosen-plugin
tables. These tests pin the behaviors that matter: only not-installed plugins
are dropped, the non-empty guard refuses without --force, a full site (every
plugin installed) is a no-op, and an ORPHAN_TABLE_OWNERS table - one the core
chain creates for a plugin that no longer ships - is dropped too, because there
is no plugin left to "install" that would ever spare it.
"""
import pytest
from sqlalchemy import inspect, text
from shopdb.extensions import db as _db
from shopdb.plugins.cli import plugin_cli
from shopdb.plugins.alembic_template import (
ORPHAN_TABLE_OWNERS, PLUGIN_TABLE_OWNERS)
def _install_only(app, monkeypatch, installed_names):
"""Force discover_available to report exactly `installed_names` installed."""
pm = app.extensions['plugin_manager']
listing = [{'name': name, 'installed': name in installed_names}
for name in PLUGIN_TABLE_OWNERS]
monkeypatch.setattr(pm, 'discover_available', lambda: listing)
def test_prune_drops_only_not_installed(app, db, runner, monkeypatch):
"""A lean site keeps its plugins' tables and drops every other plugin's."""
_install_only(app, monkeypatch, {'machines', 'printers'})
before = set(inspect(_db.engine).get_table_names())
assert 'guides' in before and 'machines' in before # baseline sanity
result = runner.invoke(plugin_cli, ['prune-schema', '--yes'])
assert result.exit_code == 0, result.output
after = set(inspect(_db.engine).get_table_names())
# Kept: machines + printers owned tables.
for keep in PLUGIN_TABLE_OWNERS['machines'] + PLUGIN_TABLE_OWNERS['printers']:
assert keep in after, f'{keep} should have been kept'
# Dropped: every other plugin's tables.
for plugin, tables in PLUGIN_TABLE_OWNERS.items():
if plugin in ('machines', 'printers'):
continue
for tablename in tables:
assert tablename not in after, f'{tablename} should have been dropped'
# Core tables untouched.
for core in ('assets', 'users', 'settings'):
assert core in after
def test_prune_full_site_is_noop(app, db, runner, monkeypatch):
"""Every plugin installed -> nothing to prune, schema unchanged."""
_install_only(app, monkeypatch, set(PLUGIN_TABLE_OWNERS))
before = set(inspect(_db.engine).get_table_names())
result = runner.invoke(plugin_cli, ['prune-schema', '--yes', '--force'])
assert result.exit_code == 0, result.output
assert 'Nothing to prune' in result.output
assert set(inspect(_db.engine).get_table_names()) == before
def test_prune_refuses_non_empty_without_force(app, db, runner, monkeypatch):
"""A not-installed table that holds rows is not dropped without --force."""
_install_only(app, monkeypatch, {'machines'})
_db.session.execute(text(
"INSERT INTO printertypes (printertype, createddate, modifieddate, "
"isactive) VALUES ('kept-by-guard', '2026-01-01', '2026-01-01', 1)"))
_db.session.commit()
result = runner.invoke(plugin_cli, ['prune-schema', '--yes'])
assert result.exit_code == 1
assert 'REFUSING' in result.output
# The row-bearing table survives the refusal.
assert 'printertypes' in set(inspect(_db.engine).get_table_names())
def test_prune_force_drops_non_empty(app, db, runner, monkeypatch):
"""--force drops even a table that holds rows."""
_install_only(app, monkeypatch, {'machines'})
_db.session.execute(text(
"INSERT INTO printertypes (printertype, createddate, modifieddate, "
"isactive) VALUES ('dropped-by-force', '2026-01-01', '2026-01-01', 1)"))
_db.session.commit()
result = runner.invoke(plugin_cli, ['prune-schema', '--yes', '--force'])
assert result.exit_code == 0, result.output
assert 'printertypes' not in set(inspect(_db.engine).get_table_names())
def test_prune_drops_an_orphan_table_no_plugin_owns(app, db, runner, monkeypatch):
"""A table the core chain creates for a plugin that no longer ships.
This is the case with no plugin to install, so nothing can ever spare it:
the core chain builds it on every site and only prune-schema takes it away.
Built here by hand because the test database is created from the MODELS, and
by definition no model declares this table any more.
"""
_install_only(app, monkeypatch, set(PLUGIN_TABLE_OWNERS))
orphan = ORPHAN_TABLE_OWNERS['employees'][0]
_db.session.execute(text(f'CREATE TABLE {orphan} (sso INTEGER PRIMARY KEY)'))
_db.session.commit()
assert orphan in set(inspect(_db.engine).get_table_names())
result = runner.invoke(plugin_cli, ['prune-schema', '--yes'])
assert result.exit_code == 0, result.output
assert orphan not in set(inspect(_db.engine).get_table_names())