From 58ca879b017da62e77f40752d8d94fef3f315a1e Mon Sep 17 00:00:00 2001 From: Christophe Besson Date: Fri, 28 Aug 2026 10:09:38 +0200 Subject: feat(hub): group names unique per owner, shown as name@owner MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A group's identity stays its UUID. What changes is that "the name is unique" — until now an unenforced expectation — becomes real, scoped to the owner account, and the owner's username is surfaced so two groups called "photos" on different nodes can be told apart. Hub: - `groups` gains a functional unique index `uq_groups_owner_name` on `(admin_id, lower(name))` (model + migration c3d4e5f6a7b8). The migration pre-flights: if the data already clashes it aborts and lists the offending (admin_id, name) pairs rather than renaming anyone's group. meshbay.org checked clean. - `create_group` trims the name, rejects blank (422) and an owner-scoped case-insensitive clash (409), with an IntegrityError backstop for the race, and returns `owner_username`. - `owner_username` added to `/v1/groups/mine`, `GET /v1/groups` (local rows), `POST /v1/groups/{id}/join`, `GET /v1/admin/groups`. SPA: - new `static/group-name.js` — `` renders the name with the `@owner` handle on a smaller grey line under it. - used in the sidebar, the group-page header, Explore cards, the Admin groups table, and cross-group Search (via a widened `cacheGroupIndex` carrying the owner). Delete/leave confirmations show `name@owner` inline. - federated Explore rows show `@` instead of an account. Design record and the locked decisions: ~/next/groupnames.md (out of repo). MNP unchanged. Tests: test_group_name_unique.py, test_group_name_migration.py. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_018gKJ85aZyvEwarXMFzFEwi --- .../meshbay-hub/tests/test_group_name_migration.py | 64 ++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 packages/meshbay-hub/tests/test_group_name_migration.py (limited to 'packages/meshbay-hub/tests/test_group_name_migration.py') diff --git a/packages/meshbay-hub/tests/test_group_name_migration.py b/packages/meshbay-hub/tests/test_group_name_migration.py new file mode 100644 index 0000000..9813e04 --- /dev/null +++ b/packages/meshbay-hub/tests/test_group_name_migration.py @@ -0,0 +1,64 @@ +""" +The uq_groups_owner_name migration refuses to run over data that already +violates it, naming the offending pairs — it must never silently rename a +group. +""" + +import sqlite3 +from pathlib import Path + +import pytest +from alembic import command +from alembic.config import Config + +HUB = Path(__file__).resolve().parents[1] +BEFORE = "b1c2d3e4f5a6" +AFTER = "c3d4e5f6a7b8" + +_USER = ("INSERT INTO users (id, username, email, pw_hash, pw_salt, hub_id) " + "VALUES ('u1', 'alice', 'a@x', x'00', x'00', 'h')") +_GROUP = ("INSERT INTO groups (id, name, admin_id, visibility, join_policy, status) " + "VALUES (?, ?, 'u1', 'private', 'invite', 'active')") + + +@pytest.fixture +def at_before(tmp_path, monkeypatch): + """A hub DB migrated up to the revision just before uq_groups_owner_name.""" + db = tmp_path / "hub.db" + monkeypatch.setenv("MESHBAY_DATABASE_URL", f"sqlite+aiosqlite:///{db}") + cfg = Config(str(HUB / "alembic.ini")) + command.upgrade(cfg, BEFORE) + return db, cfg + + +def test_migration_aborts_on_a_pre_existing_duplicate(at_before): + db, cfg = at_before + con = sqlite3.connect(db) + con.execute(_USER) + con.execute(_GROUP, ("g1", "Photos")) + con.execute(_GROUP, ("g2", "photos")) # same owner, same name bar case + con.commit() + con.close() + + with pytest.raises(Exception) as exc: + command.upgrade(cfg, AFTER) + assert "uq_groups_owner_name" in str(exc.value) + assert "photos" in str(exc.value).lower() + + +def test_migration_runs_when_data_is_clean(at_before): + db, cfg = at_before + con = sqlite3.connect(db) + con.execute(_USER) + con.execute(_GROUP, ("g1", "Photos")) + con.execute(_GROUP, ("g2", "Videos")) + con.commit() + con.close() + + command.upgrade(cfg, AFTER) + + con = sqlite3.connect(db) + idx = [r[0] for r in con.execute( + "SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='groups'")] + con.close() + assert "uq_groups_owner_name" in idx -- cgit v1.2.3