Fix issue #111: Entry sync on foundry keeps creating new journal entries
All checks were successful
Test / test (push) Successful in 2s

This commit is contained in:
Gitea OpenCode Agent
2026-10-16 16:57:28 +00:00
parent b5d5e570d5
commit eead3c9be8
4 changed files with 93 additions and 14 deletions

View File

@@ -238,13 +238,15 @@ function writeFoundryPaths(target, data) {
return target;
}
function makeWorld({ markdown = true } = {}) {
function makeWorld({ markdown = true, keepCreateFlags = true, failMapNote = false } = {}) {
const state = {
hooks: new Map(),
registered: new Map(),
requests: [],
timers: new Map(),
intervals: new Map(),
keepCreateFlags,
failMapNote,
// What Dragonroll answers the notes reconcile with. Mapping a page answers
// like the real backend: the note keeps the page id, so later reads carry it.
remoteNotes: [],
@@ -289,7 +291,9 @@ function makeWorld({ markdown = true } = {}) {
assert.equal(documentName, "JournalEntryPage", "the notes' pages are journal pages");
// Foundry's own page validation refuses a page without a name.
for (const doc of docs) assert.ok(doc.name, "a journal page needs a non-empty name");
const created = docs.map((doc) => makePage(this, doc));
// A world that does not keep the flags a create was handed: the page
// exists but carries no mark, which is what a lost pairing looks like.
const created = docs.map((doc) => makePage(this, keepCreateFlags ? doc : { ...doc, flags: {} }));
pages.push(...created);
return created;
},
@@ -326,6 +330,7 @@ function makeWorld({ markdown = true } = {}) {
const body = options.body ? JSON.parse(options.body) : null;
state.requests.push({ href, method: options.method, body });
if (href.includes("foundry/sync/map-note")) {
if (state.failMapNote) return { ok: false, status: 500, json: async () => ({ status: "error", msg: "errors.internal" }) };
const note = state.remoteNotes.find((entry) => String(entry._id) === String(body.noteId));
if (note) note.foundryPageId = body.foundryPageId;
return { ok: true, status: 200, json: async () => ({ status: "ok", note }) };
@@ -524,6 +529,57 @@ test("the campaign's page mapping claims a page whose flag was lost", async () =
assert.equal(orphan.getFlag(MODULE_ID, "noteId"), "note-1", "and the page is marked again");
});
test("a page under the note's title with no mark is claimed, not doubled", async () => {
const world = await makeWorld().linkedSetup();
const journal = world.addJournal("Dragonroll");
const existing = world.addPage(journal, { name: "Session 1", text: { content: "an older copy" } });
// The campaign has no page id for the note, and the page carries no flag: this
// is a page the module made before the pairing was kept, or one whose flag was
// lost. It is claimed instead of a second page being made under the same title.
world.remoteNotes = [sessionNote()];
await world.runIntervals();
assert.equal(journal.pages.contents.length, 1, "no second page under the note's title");
assert.equal(existing.text.content, "<marked>**The start**</marked>", "the note's text lands on the page the world already had");
assert.equal(existing.getFlag(MODULE_ID, "noteId"), "note-1", "and the page is marked as the note's");
assert.deepEqual(world.mapNoteRequests().map((request) => request.body.foundryPageId), [existing.id],
"the campaign is told which page the note now stands as");
await world.runIntervals();
assert.equal(journal.pages.contents.length, 1, "reconciling again does not add another");
});
test("two notes of one title do not share a single page", async () => {
const world = await makeWorld().linkedSetup();
const journal = world.addJournal("Dragonroll");
const existing = world.addPage(journal, { name: "Session 1", text: { content: "an older copy" } });
world.remoteNotes = [sessionNote(), { _id: "note-9", title: "Session 1", content: "second" }];
await world.runIntervals();
assert.equal(journal.pages.contents.length, 2, "the second note gets a page of its own");
assert.equal(existing.getFlag(MODULE_ID, "noteId"), "note-1", "the world's page is claimed by the first note");
assert.equal(journal.pages.contents[1].getFlag(MODULE_ID, "noteId"), "note-9");
});
test("a page whose mark and mapping were both lost is claimed again, not doubled", async () => {
// Both ways of recognising the pairing are gone: the create's flag did not stick,
// and the campaign never got to keep the page id. Without claiming the page it
// already made, every reconcile would write another one.
const world = await makeWorld({ keepCreateFlags: false, failMapNote: true }).linkedSetup();
world.remoteNotes = [sessionNote()];
await world.runIntervals();
await world.runIntervals();
await world.runIntervals();
const journal = world.journals()[0];
assert.equal(journal.pages.contents.length, 1, "one note stands as the one page it was given");
assert.equal(journal.pages.contents[0].getFlag(MODULE_ID, "noteId"), "note-1",
"and the page keeps the note it stands for even though the create did not mark it");
});
test("without Foundry's markdown renderer the fallback keeps the text legible", async () => {
const world = await makeWorld({ markdown: false }).linkedSetup();
world.remoteNotes = [{ _id: "note-1", title: "Session 1", content: "a & b < c\nline two\n\nsecond para" }];