import { CoValueCore, LocalNode } from "../exports"; import { NewContentMessage, SyncMessage } from "../sync"; import { CoValueKnownState } from "../knownState.js"; import { LazyLoadMessage, LazyLoadResultMessage } from "./testUtils.js"; function simplifySessions(msg: Pick) { const count = Object.values(msg.sessions).reduce( (acc, session) => acc + session, 0, ); if (msg.header) { return `header/${count}`; } return `empty`; } function simplifyNewContent(content: NewContentMessage["new"]) { if (!content) { return undefined; } return Object.values(content) .map((c) => `After: ${c.after} New: ${c.newTransactions.length}`) .join(" | "); } type TestMessage = SyncMessage | LazyLoadMessage | LazyLoadResultMessage; export function toSimplifiedMessages( coValues: Record, messages: { from: string; to: string; msg: TestMessage; }[], ) { function getCoValue(id: string) { for (const [name, coValue] of Object.entries(coValues)) { if (coValue.id === id) { return name; } } return `unknown/${id}`; } function toDebugString(from: string, to: string, msg: TestMessage) { switch (msg.action) { case "known": return `${from} -> ${to} | KNOWN ${msg.isCorrection ? "CORRECTION " : ""}${getCoValue(msg.id)} sessions: ${simplifySessions(msg)}`; case "load": return `${from} -> ${to} | LOAD ${getCoValue(msg.id)} sessions: ${simplifySessions(msg)}`; case "done": return `${from} -> ${to} | DONE ${getCoValue(msg.id)}`; case "content": return `${from} -> ${to} | CONTENT ${getCoValue(msg.id)} header: ${Boolean(msg.header)} new: ${simplifyNewContent(msg.new)}${msg.expectContentUntil ? ` expectContentUntil: ${simplifySessions({ sessions: msg.expectContentUntil, header: true })}` : ""}`; case "lazyLoad": return `${from} -> ${to} | GET_KNOWN_STATE ${getCoValue(msg.id)}`; case "lazyLoadResult": return `${from} -> ${to} | GET_KNOWN_STATE_RESULT ${getCoValue(msg.id)} sessions: ${simplifySessions(msg)}`; case "reconcile": return `${from} -> ${to} | RECONCILE`; case "reconcile-ack": return `${from} -> ${to} | RECONCILE_ACK`; } } return messages.map((m) => toDebugString(m.from, m.to, m.msg)); } export function nodeRelatedKnownCoValues(node: LocalNode, name: string) { const account = node.expectCurrentAccount("relatedKnownCoValues"); const profileID = account.get("profile"); const profile = profileID && node.expectCoValueLoaded(profileID); return { [`${name}Account`]: account, [`${name}Profile`]: profile, [`${name}ProfileGroup`]: profile?.getGroup().core, }; } export function debugMessages( coValues: Record, messages: { from: string; to: string; msg: TestMessage; }[], ) { console.log(toSimplifiedMessages(coValues, messages)); }