#!/usr/bin/env python3"""Prepare synthetic thirteen-database phone states for HouseholdHomeReplicaTests.Run cotton-headless seed and add-database twelve times in an isolated folder,then pass that serving folder here. No owner database is read. The simulatortest copies these fixtures into its own temporary container before opening."""import argparseimport jsonimport pathlibimport sqlite3import base64def prepare(serving, output, server_url): files = list(serving.glob("files/*/*/life.db"))if len(files) != 13:raiseValueError("the synthetic household must contain thirteen databases") records = []for path in files:with sqlite3.connect(path) as db: row = db.execute("SELECT id, owner_user_id, kind FROM cotton_workspaces").fetchone() records.append((path, *row)) personal = next(record for record in records if record[3] == "personal") places = [record for record in records if record != personal] output.mkdir(parents=True, exist_ok=True)for state in ("older", "absent"): root = output / state / "client" root.mkdir(parents=True, exist_ok=True) connection = dict(mode="remote", baseUrl="https://synthetic.invalid", workspaceId=personal[1], ownerId=personal[2], connectedAt="2026-10-12T00:00:00.000Z", places=[ dict(id=r[1], name=f"Synthetic project {i}", available=True, unavailableSince=None) for i, r in enumerate(places)]) (root / "connection.json").write_text(json.dumps(connection))for record in records if state == "older"else [personal]: path, workspace, _, _ = record target = root / workspace / "replica.db" target.parent.mkdir(parents=True, exist_ok=True)with sqlite3.connect(path) as source, sqlite3.connect(target) as destination: source.backup(destination) columns = {r[1] for r in destination.execute("PRAGMA table_info(cotton_devices)")}# 135 added only this nullable column. Model real 134 bytes,# rather than changing the stamp on a newer shape.if workspace == personal[1]:if"reported_state"notin columns: destination.execute("ALTER TABLE cotton_devices ADD COLUMN reported_state TEXT") destination.execute("PRAGMA user_version = 135")else:if"reported_state"in columns: destination.execute("ALTER TABLE cotton_devices DROP COLUMN reported_state") destination.execute("PRAGMA user_version = 134") seed = (serving / "identity/device.seed").read_bytes() (output / "manifest.json").write_text(json.dumps(dict( personal=personal[1], projects=[r[1] for r in places], schema=135, syntheticDeviceSecret=base64.urlsafe_b64encode(seed).decode().rstrip("="), serverUrl=server_url)))if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("serving", type=pathlib.Path) parser.add_argument("--output", type=pathlib.Path, default=pathlib.Path(__file__).resolve().parents[1] / ".local/recopy-sim/fixtures") parser.add_argument("--server-url", default="http://127.0.0.1:18873") args = parser.parse_args()# An explicit synthetic seed receipt is required; never accept an app folder.if args.serving.resolve().name != "serving"ornot (args.serving.parent / "seed.json").is_file(): parser.error("use a synthetic serving folder beside its cotton-headless seed.json receipt") prepare(args.serving.resolve(), args.output.resolve(), args.server_url)