From d909649f2e68a3da6a6db99f999ad159739a3564 Mon Sep 17 00:00:00 2001 From: Kevin Rassool Date: Thu, 27 Aug 2026 21:15:45 +1000 Subject: [PATCH 1/4] fix: prevent custom pages overwriting each other during page sync syncPages names each local file by page.name, but every custom (page-builder) page shares the name "legacy". On campaigns with more than one custom page, all of them are written to the same legacy.json (last write wins), so most custom pages never appear locally and the surviving file's identity changes between syncs. Name files by the page's unique name when it is unique across the campaign, and fall back to the page path otherwise (cause-areas.json, terms.json, ...). Residual collisions after sanitization are disambiguated with a short uuid suffix. Deploy is unaffected: pages are PATCHed by the uuid stored inside each file. Co-Authored-By: Claude Fable 5 --- src/actions/sync.js | 49 +++++++++++++++++++++++++------ tests/sync.test.js | 71 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 tests/sync.test.js diff --git a/src/actions/sync.js b/src/actions/sync.js index 67287b9..ceb9488 100644 --- a/src/actions/sync.js +++ b/src/actions/sync.js @@ -55,13 +55,43 @@ export async function syncStyles() { } } -function pageFileName(page) { - const base = - page.name || - (page.path && page.path !== '/' - ? page.path.replace(/^\//, '').replace(/\//g, '-') - : 'home'); - return `${base.replace(/[^a-zA-Z0-9._-]/g, '_')}.json`; +function pagePathBase(page) { + return page.path && page.path !== '/' + ? page.path.replace(/^\//, '').replace(/\//g, '-') + : 'home'; +} + +/** + * Compute a unique local file name for every page in a campaign. + * + * `page.name` is only unique for template pages — every custom + * (page-builder) page shares the name "legacy", so naming files by + * `page.name` alone makes all custom pages overwrite each other into a + * single legacy.json. Whenever a name is shared by more than one page, + * fall back to the page's path instead. + * + * @param {Array} pages Pages belonging to one campaign + * @returns {string[]} File name for each page, in the same order + */ +export function pageFileNames(pages) { + const nameCounts = new Map(); + for (const page of pages) { + if (page.name) { + nameCounts.set(page.name, (nameCounts.get(page.name) || 0) + 1); + } + } + + const used = new Set(); + return pages.map((page) => { + const nameIsUnique = page.name && nameCounts.get(page.name) === 1; + const base = nameIsUnique ? page.name : pagePathBase(page); + let fileName = `${base.replace(/[^a-zA-Z0-9._-]/g, '_')}.json`; + if (used.has(fileName) && page.uuid) { + fileName = fileName.replace(/\.json$/, `-${page.uuid.slice(0, 8)}.json`); + } + used.add(fileName); + return fileName; + }); } export async function syncPages() { @@ -87,7 +117,8 @@ export async function syncPages() { path: `/campaigns/${uuid}/pages?private=1&includeBody=1&limit=999`, }); - for (const page of pages.data) { + const fileNames = pageFileNames(pages.data); + for (const [index, page] of pages.data.entries()) { const out = { uuid: page.uuid, path: page.path, @@ -107,7 +138,7 @@ export async function syncPages() { }; fs.writeFileSync( - path.join(pagesDir, pageFileName(page)), + path.join(pagesDir, fileNames[index]), JSON.stringify(out, null, 4) ); } diff --git a/tests/sync.test.js b/tests/sync.test.js new file mode 100644 index 0000000..84ea3ac --- /dev/null +++ b/tests/sync.test.js @@ -0,0 +1,71 @@ +import { describe, expect, test } from 'vitest'; + +import { pageFileNames } from '../src/actions/sync.js'; + +describe('pageFileNames', () => { + test('names template pages by their unique name', () => { + const pages = [ + { uuid: 'u1', name: 'home', path: '/' }, + { uuid: 'u2', name: 'dashboard', path: '/dashboard' }, + { uuid: 'u3', name: 'profile', path: '/:id' }, + ]; + expect(pageFileNames(pages)).toEqual([ + 'home.json', + 'dashboard.json', + 'profile.json', + ]); + }); + + test('falls back to path when multiple pages share a name', () => { + // Custom (page-builder) pages all share the name "legacy" — naming + // files by name alone collapses them into a single legacy.json. + const pages = [ + { uuid: 'u1', name: 'legacy', path: '/cause-areas' }, + { uuid: 'u2', name: 'legacy', path: '/terms' }, + { uuid: 'u3', name: 'legacy', path: '/about/team' }, + { uuid: 'u4', name: 'home', path: '/' }, + ]; + expect(pageFileNames(pages)).toEqual([ + 'cause-areas.json', + 'terms.json', + 'about-team.json', + 'home.json', + ]); + }); + + test('keeps name-based file for a name used by only one page', () => { + const pages = [ + { uuid: 'u1', name: 'legacy', path: '/only-custom-page' }, + { uuid: 'u2', name: 'home', path: '/' }, + ]; + expect(pageFileNames(pages)).toEqual(['legacy.json', 'home.json']); + }); + + test('uses path for pages without a name', () => { + const pages = [ + { uuid: 'u1', name: null, path: '/register' }, + { uuid: 'u2', name: null, path: '/signup' }, + ]; + expect(pageFileNames(pages)).toEqual(['register.json', 'signup.json']); + }); + + test('sanitizes special characters in paths', () => { + const pages = [ + { uuid: 'u1', name: null, path: '/reset/:passwordResetToken?' }, + ]; + expect(pageFileNames(pages)).toEqual([ + 'reset-_passwordResetToken_.json', + ]); + }); + + test('disambiguates residual file name collisions with a uuid suffix', () => { + const pages = [ + { uuid: 'aaaaaaaa-1111', name: 'legacy', path: '/promo!' }, + { uuid: 'bbbbbbbb-2222', name: 'legacy', path: '/promo?' }, + ]; + expect(pageFileNames(pages)).toEqual([ + 'promo_.json', + 'promo_-bbbbbbbb.json', + ]); + }); +}); From b27b366a63ec74a225c44cf1455357644b132471 Mon Sep 17 00:00:00 2001 From: Kevin Rassool Date: Thu, 27 Aug 2026 21:23:24 +1000 Subject: [PATCH 2/4] fix: reserve unique-name filenames before assigning path-based names Two-pass assignment so a custom page whose path sanitizes to a template page's name (e.g. /profile vs the profile template at /:id) can never take that template's filename, regardless of API order. The custom page gets the uuid-suffixed name instead. Addresses Bugbot review on #85. Co-Authored-By: Claude Fable 5 --- src/actions/sync.js | 38 ++++++++++++++++++++++++++++++-------- tests/sync.test.js | 16 ++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/actions/sync.js b/src/actions/sync.js index ceb9488..ff0b5db 100644 --- a/src/actions/sync.js +++ b/src/actions/sync.js @@ -81,16 +81,38 @@ export function pageFileNames(pages) { } } + const sanitize = (base) => `${base.replace(/[^a-zA-Z0-9._-]/g, '_')}.json`; + + // First pass: pages with a unique name keep their name-based file. + // Reserving these up front keeps template filenames stable regardless + // of API order (a custom page at e.g. /profile must never take + // profile.json from the profile template page). const used = new Set(); - return pages.map((page) => { - const nameIsUnique = page.name && nameCounts.get(page.name) === 1; - const base = nameIsUnique ? page.name : pagePathBase(page); - let fileName = `${base.replace(/[^a-zA-Z0-9._-]/g, '_')}.json`; - if (used.has(fileName) && page.uuid) { - fileName = fileName.replace(/\.json$/, `-${page.uuid.slice(0, 8)}.json`); + const fileNames = pages.map((page) => { + if (page.name && nameCounts.get(page.name) === 1) { + const fileName = sanitize(page.name); + used.add(fileName); + return fileName; + } + return null; + }); + + // Second pass: pages with a shared or missing name fall back to their + // path, with a short uuid suffix on any residual collision. + return fileNames.map((fileName, index) => { + if (fileName) { + return fileName; + } + const page = pages[index]; + let candidate = sanitize(pagePathBase(page)); + if (used.has(candidate) && page.uuid) { + candidate = candidate.replace( + /\.json$/, + `-${page.uuid.slice(0, 8)}.json` + ); } - used.add(fileName); - return fileName; + used.add(candidate); + return candidate; }); } diff --git a/tests/sync.test.js b/tests/sync.test.js index 84ea3ac..384c968 100644 --- a/tests/sync.test.js +++ b/tests/sync.test.js @@ -58,6 +58,22 @@ describe('pageFileNames', () => { ]); }); + test('template filenames are reserved regardless of API order', () => { + // A custom page at the literal path /profile must not take + // profile.json from the profile template page (path /:id), even + // when the API returns the custom page first. + const pages = [ + { uuid: 'cccccccc-3333', name: 'legacy', path: '/profile' }, + { uuid: 'dddddddd-4444', name: 'legacy', path: '/terms' }, + { uuid: 'eeeeeeee-5555', name: 'profile', path: '/:id' }, + ]; + expect(pageFileNames(pages)).toEqual([ + 'profile-cccccccc.json', + 'terms.json', + 'profile.json', + ]); + }); + test('disambiguates residual file name collisions with a uuid suffix', () => { const pages = [ { uuid: 'aaaaaaaa-1111', name: 'legacy', path: '/promo!' }, From fa82592faa14098598ec18b37697c41aa48d7c6b Mon Sep 17 00:00:00 2001 From: KeinerM Date: Wed, 18 Nov 2026 13:42:00 -0500 Subject: [PATCH 3/4] fix: remove stale page files left under old names after sync Repos synced before the page naming fix keep the old file (for example legacy.json) next to the new path-named file, both with the same uuid. raisely local can pick the stale file, and raisely deploy sends both as updates for the same page. After writing pages, delete a page file only when its uuid was part of this sync and the page was written under a different file name. Files for pages that weren't synced, and files that aren't valid JSON, stay. --- src/actions/sync.js | 37 ++++++++++++ tests/sync.test.js | 135 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 170 insertions(+), 2 deletions(-) diff --git a/src/actions/sync.js b/src/actions/sync.js index ff0b5db..fe82ce2 100644 --- a/src/actions/sync.js +++ b/src/actions/sync.js @@ -116,6 +116,39 @@ export function pageFileNames(pages) { }); } +/** + * Delete page files left behind under an old name. + * + * A file is removed only when its uuid was part of this sync and the page + * was written under a different file name. Files for pages that weren't + * synced, and files that aren't valid JSON, are kept. + * + * @param {string} pagesDir Directory holding the campaign's page files + * @param {Map} fileNameByUuid File name written for each synced page uuid + * @returns {string[]} File names that were removed + */ +export function removeStalePageFiles(pagesDir, fileNameByUuid) { + const removed = []; + for (const entry of fs.readdirSync(pagesDir, { withFileTypes: true })) { + if (!entry.isFile() || !entry.name.endsWith('.json')) continue; + + const filePath = path.join(pagesDir, entry.name); + let uuid; + try { + uuid = JSON.parse(fs.readFileSync(filePath, 'utf8'))?.uuid; + } catch { + continue; + } + + const current = fileNameByUuid.get(uuid); + if (current && current !== entry.name) { + fs.unlinkSync(filePath); + removed.push(entry.name); + } + } + return removed; +} + export async function syncPages() { const config = await loadConfig(); @@ -140,6 +173,7 @@ export async function syncPages() { }); const fileNames = pageFileNames(pages.data); + const fileNameByUuid = new Map(); for (const [index, page] of pages.data.entries()) { const out = { uuid: page.uuid, @@ -163,7 +197,10 @@ export async function syncPages() { path.join(pagesDir, fileNames[index]), JSON.stringify(out, null, 4) ); + fileNameByUuid.set(page.uuid, fileNames[index]); } + + removeStalePageFiles(pagesDir, fileNameByUuid); } loader.succeed(); } catch (e) { diff --git a/tests/sync.test.js b/tests/sync.test.js index 384c968..40175fe 100644 --- a/tests/sync.test.js +++ b/tests/sync.test.js @@ -1,6 +1,32 @@ -import { describe, expect, test } from 'vitest'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; -import { pageFileNames } from '../src/actions/sync.js'; +const mocks = vi.hoisted(() => ({ + api: vi.fn(), + loadConfig: vi.fn(), +})); + +vi.mock('../src/actions/api.js', () => ({ + default: mocks.api, +})); + +vi.mock('../src/config.js', () => ({ + loadConfig: mocks.loadConfig, +})); + +vi.mock('ora', () => ({ + default: () => ({ + start: () => ({ succeed: vi.fn(), fail: vi.fn() }), + }), +})); + +import { + pageFileNames, + removeStalePageFiles, + syncPages, +} from '../src/actions/sync.js'; describe('pageFileNames', () => { test('names template pages by their unique name', () => { @@ -85,3 +111,108 @@ describe('pageFileNames', () => { ]); }); }); + +describe('page file cleanup', () => { + let tmpDir; + + const writeJson = (dir, name, value) => + fs.writeFileSync(path.join(dir, name), JSON.stringify(value)); + + beforeEach(() => { + vi.clearAllMocks(); + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'raisely-sync-')); + }); + + afterEach(() => { + vi.restoreAllMocks(); + fs.rmSync(tmpDir, { recursive: true, force: true }); + }); + + test('removeStalePageFiles removes only old files of synced pages', () => { + writeJson(tmpDir, 'magicLink.json', { uuid: 'a0a04680-1111' }); + writeJson(tmpDir, 'magic-link-a0a04680.json', { + uuid: 'a0a04680-1111', + }); + writeJson(tmpDir, 'archived.json', { uuid: 'not-synced' }); + writeJson(tmpDir, 'no-uuid.json', { title: 'Draft' }); + fs.writeFileSync(path.join(tmpDir, 'broken.json'), '{ not json'); + fs.writeFileSync(path.join(tmpDir, 'notes.txt'), 'a0a04680-1111'); + fs.mkdirSync(path.join(tmpDir, 'nested.json')); + + const removed = removeStalePageFiles( + tmpDir, + new Map([['a0a04680-1111', 'magic-link-a0a04680.json']]) + ); + + expect(removed).toEqual(['magicLink.json']); + expect(fs.readdirSync(tmpDir).sort()).toEqual([ + 'archived.json', + 'broken.json', + 'magic-link-a0a04680.json', + 'nested.json', + 'no-uuid.json', + 'notes.txt', + ]); + }); + + test('syncPages replaces an old legacy.json with path-named files', async () => { + const pagesDir = path.join(tmpDir, 'campaigns', 'my-campaign', 'pages'); + fs.mkdirSync(pagesDir, { recursive: true }); + writeJson(pagesDir, 'legacy.json', { + uuid: 'u-renamed', + body: 'stale content', + }); + writeJson(pagesDir, 'home.json', { uuid: 'u-home', body: 'old home' }); + writeJson(pagesDir, 'deleted-page.json', { uuid: 'u-deleted' }); + fs.writeFileSync(path.join(pagesDir, 'scratch.json'), 'not json'); + + vi.spyOn(process, 'cwd').mockReturnValue(tmpDir); + mocks.loadConfig.mockResolvedValue({ campaigns: ['campaign-uuid'] }); + mocks.api.mockImplementation(async ({ path: apiPath }) => { + if (apiPath.startsWith('/campaigns/campaign-uuid/pages')) { + return { + data: [ + { + uuid: 'u-renamed', + name: 'legacy', + path: '/renamed-home-page', + body: 'fresh content', + }, + { uuid: 'u-terms', name: 'legacy', path: '/terms' }, + { + uuid: 'u-home', + name: 'home', + path: '/', + body: 'new home', + }, + ], + }; + } + return { data: { path: 'my-campaign' } }; + }); + + await syncPages(); + + expect(fs.readdirSync(pagesDir).sort()).toEqual([ + 'deleted-page.json', + 'home.json', + 'renamed-home-page.json', + 'scratch.json', + 'terms.json', + ]); + const renamed = JSON.parse( + fs.readFileSync( + path.join(pagesDir, 'renamed-home-page.json'), + 'utf8' + ) + ); + expect(renamed).toMatchObject({ + uuid: 'u-renamed', + body: 'fresh content', + }); + const home = JSON.parse( + fs.readFileSync(path.join(pagesDir, 'home.json'), 'utf8') + ); + expect(home).toMatchObject({ uuid: 'u-home', body: 'new home' }); + }); +}); From a78226e6828fe28a97af9182943eb89f209f899c Mon Sep 17 00:00:00 2001 From: KeinerM Date: Wed, 18 Nov 2026 14:26:46 -0500 Subject: [PATCH 4/4] fix: handle page file names that differ only by case On case-insensitive filesystems (macOS, Windows default) magicLink.json and magiclink.json are the same file, so two pages could still write to one file, and stale cleanup could delete a file that sync had written. Compare names case-insensitively when assigning page file names, and skip cleanup when the old entry is the same file as the one just written. Addresses Bugbot review on #85. --- src/actions/sync.js | 35 +++++++++++++++++++++++++++++------ tests/sync.test.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/actions/sync.js b/src/actions/sync.js index fe82ce2..d3c858f 100644 --- a/src/actions/sync.js +++ b/src/actions/sync.js @@ -83,16 +83,23 @@ export function pageFileNames(pages) { const sanitize = (base) => `${base.replace(/[^a-zA-Z0-9._-]/g, '_')}.json`; + // Compare names case-insensitively: on macOS and Windows, magicLink.json + // and magiclink.json are the same file. + const used = new Set(); + const isUsed = (fileName) => used.has(fileName.toLowerCase()); + const markUsed = (fileName) => used.add(fileName.toLowerCase()); + // First pass: pages with a unique name keep their name-based file. // Reserving these up front keeps template filenames stable regardless // of API order (a custom page at e.g. /profile must never take // profile.json from the profile template page). - const used = new Set(); const fileNames = pages.map((page) => { if (page.name && nameCounts.get(page.name) === 1) { const fileName = sanitize(page.name); - used.add(fileName); - return fileName; + if (!isUsed(fileName)) { + markUsed(fileName); + return fileName; + } } return null; }); @@ -105,13 +112,13 @@ export function pageFileNames(pages) { } const page = pages[index]; let candidate = sanitize(pagePathBase(page)); - if (used.has(candidate) && page.uuid) { + if (isUsed(candidate) && page.uuid) { candidate = candidate.replace( /\.json$/, `-${page.uuid.slice(0, 8)}.json` ); } - used.add(candidate); + markUsed(candidate); return candidate; }); } @@ -141,7 +148,11 @@ export function removeStalePageFiles(pagesDir, fileNameByUuid) { } const current = fileNameByUuid.get(uuid); - if (current && current !== entry.name) { + if ( + current && + current !== entry.name && + !isSameFile(filePath, path.join(pagesDir, current)) + ) { fs.unlinkSync(filePath); removed.push(entry.name); } @@ -149,6 +160,18 @@ export function removeStalePageFiles(pagesDir, fileNameByUuid) { return removed; } +// On case-insensitive filesystems, writing profile.json can land in an +// existing Profile.json, so the names differ but the file is the same. +function isSameFile(a, b) { + try { + const statA = fs.statSync(a); + const statB = fs.statSync(b); + return statA.dev === statB.dev && statA.ino === statB.ino; + } catch { + return false; + } +} + export async function syncPages() { const config = await loadConfig(); diff --git a/tests/sync.test.js b/tests/sync.test.js index 40175fe..765d985 100644 --- a/tests/sync.test.js +++ b/tests/sync.test.js @@ -100,6 +100,19 @@ describe('pageFileNames', () => { ]); }); + test('treats file names that differ only by case as a collision', () => { + const pages = [ + { uuid: 'aaaaaaaa-1111', name: 'legacy', path: '/magiclink' }, + { uuid: 'bbbbbbbb-2222', name: 'legacy', path: '/terms' }, + { uuid: 'cccccccc-3333', name: 'magicLink', path: '/magic-link' }, + ]; + expect(pageFileNames(pages)).toEqual([ + 'magiclink-aaaaaaaa.json', + 'terms.json', + 'magicLink.json', + ]); + }); + test('disambiguates residual file name collisions with a uuid suffix', () => { const pages = [ { uuid: 'aaaaaaaa-1111', name: 'legacy', path: '/promo!' }, @@ -155,6 +168,22 @@ describe('page file cleanup', () => { ]); }); + test('removeStalePageFiles keeps one file when names differ only by case', () => { + // On case-insensitive filesystems the second write lands in + // Profile.json, so both names point to the same file. + writeJson(tmpDir, 'Profile.json', { uuid: 'u-profile', body: 'old' }); + writeJson(tmpDir, 'profile.json', { uuid: 'u-profile', body: 'new' }); + + removeStalePageFiles(tmpDir, new Map([['u-profile', 'profile.json']])); + + const files = fs.readdirSync(tmpDir); + expect(files).toHaveLength(1); + expect(files[0].toLowerCase()).toBe('profile.json'); + expect( + JSON.parse(fs.readFileSync(path.join(tmpDir, files[0]), 'utf8')) + ).toEqual({ uuid: 'u-profile', body: 'new' }); + }); + test('syncPages replaces an old legacy.json with path-named files', async () => { const pagesDir = path.join(tmpDir, 'campaigns', 'my-campaign', 'pages'); fs.mkdirSync(pagesDir, { recursive: true });