Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

test: cover lock-all bulk helpers + migration 024; fix scoped total over-count

[._.]/ Adam Eivy013620903840
diff --git a/client/src/components/universe/EntryThumbSlot.jsx b/client/src/components/universe/EntryThumbSlot.jsx
index ddf7e8fd40..0c61b3c304 100644
--- a/client/src/components/universe/EntryThumbSlot.jsx
+++ b/client/src/components/universe/EntryThumbSlot.jsx
@@ -31,16 +31,19 @@ export default function EntryThumbSlot({
onPreview = null,
canRender = true,
alt = 'Render',
- // When true, render the empty-state box with an even bigger Sparkles
- // affordance (used by canon rows that ride a wider card). Defaults to the
- // compact 48x48 footprint shared with variations.
+ // `'lg'` renders the empty-state box at 64x96 with a bigger Sparkles
+ // affordance (reserved for slots that ride a wider card). Defaults to the
+ // compact 48x80 (w-12 h-20) portrait footprint shared with variation +
+ // canon avatar rows — matched to the 2:3 aspect of typical 1024x1536
+ // universe renders so the slot doesn't crop the subject.
size = 'sm',
}) {
if (inFlightJobId) {
- // `xs` (48x48) matches the empty + completed states below so all three
- // states share a footprint and the row doesn't jump mid-render. `lg`
- // upgrades the pending box to 64x64 when the slot is configured for the
- // larger size (kept available for parity with the empty placeholder).
+ // `xs` (48x80) matches the empty + completed states below so all three
+ // states share a footprint and the row doesn't jump mid-render. `'lg'`
+ // upgrades the pending box to `sm` (64x64) when the slot is configured
+ // for the larger size — note `MediaJobThumb` doesn't have a portrait
+ // 64x96 variant, so the larger slot's pending state is square.
return (
<MediaJobThumb
jobId={inFlightJobId}
diff --git a/scripts/migrations/024-lock-canon-and-variations.test.js b/scripts/migrations/024-lock-canon-and-variations.test.js
new file mode 100644
index 0000000000..6f5643312b
--- /dev/null
+++ b/scripts/migrations/024-lock-canon-and-variations.test.js
@@ -0,0 +1,210 @@
+/**
+ * Test for migration 024 — lock canon + variations + composite sheets by
+ * default, mint stable ids on legacy id-less rows, best-effort back-fill
+ * variation imageRefs from surviving media-jobs.json entries.
+ */
+import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { mkdtempSync, rmSync, writeFileSync, readFileSync, mkdirSync } from 'fs';
+import { tmpdir } from 'os';
+import { join } from 'path';
+
+import migration from './024-lock-canon-and-variations.js';
+
+const writeJson = (path, value) => writeFileSync(path, JSON.stringify(value, null, 2) + '\n');
+const readJson = (path) => JSON.parse(readFileSync(path, 'utf-8'));
+
+describe('migration 024 — lock canon + variations, back-fill thumbnails', () => {
+ let rootDir;
+ let dataDir;
+ let universesPath;
+ let jobsPath;
+
+ beforeEach(() => {
+ rootDir = mkdtempSync(join(tmpdir(), 'migration-024-'));
+ dataDir = join(rootDir, 'data');
+ mkdirSync(dataDir, { recursive: true });
+ universesPath = join(dataDir, 'universe-builder.json');
+ jobsPath = join(dataDir, 'media-jobs.json');
+ });
+
+ afterEach(() => {
+ rmSync(rootDir, { recursive: true, force: true });
+ });
+
+ it('stamps locked:true on canon entries that have no locked field', async () => {
+ writeJson(universesPath, {
+ universes: [
+ {
+ id: 'u-1',
+ characters: [{ id: 'c-1', name: 'Alex' }, { id: 'c-2', name: 'Bee' }],
+ places: [{ id: 'p-1', name: 'Vault' }],
+ objects: [],
+ },
+ ],
+ });
+ const result = await migration.up({ rootDir });
+ expect(result.lockedCanon).toBe(3);
+ const after = readJson(universesPath);
+ expect(after.universes[0].characters.every((c) => c.locked === true)).toBe(true);
+ expect(after.universes[0].places[0].locked).toBe(true);
+ });
+
+ it('preserves explicit locked:false (does not re-lock entries the user already unlocked)', async () => {
+ writeJson(universesPath, {
+ universes: [
+ {
+ id: 'u-1',
+ characters: [
+ { id: 'c-1', name: 'Alex', locked: false },
+ { id: 'c-2', name: 'Bee' }, // missing → should lock
+ ],
+ },
+ ],
+ });
+ const result = await migration.up({ rootDir });
+ expect(result.lockedCanon).toBe(1);
+ const after = readJson(universesPath);
+ expect(after.universes[0].characters.find((c) => c.id === 'c-1').locked).toBe(false);
+ expect(after.universes[0].characters.find((c) => c.id === 'c-2').locked).toBe(true);
+ });
+
+ it('mints stable ids for variations that lack them, locks them, leaves prior ids untouched', async () => {
+ writeJson(universesPath, {
+ universes: [
+ {
+ id: 'u-1',
+ categories: {
+ landscapes: {
+ variations: [
+ { label: 'No id', prompt: 'a' },
+ { id: 'var-existing', label: 'Has id', prompt: 'b' },
+ ],
+ },
+ },
+ },
+ ],
+ });
+ const result = await migration.up({ rootDir });
+ expect(result.mintedVariationIds).toBe(1);
+ expect(result.lockedVariations).toBe(2);
+ const after = readJson(universesPath);
+ const variations = after.universes[0].categories.landscapes.variations;
+ // First entry: minted id starts with `var-`, locked:true.
+ expect(variations[0].id).toMatch(/^var-/);
+ expect(variations[0].locked).toBe(true);
+ // Second entry: id preserved verbatim, locked:true added.
+ expect(variations[1].id).toBe('var-existing');
+ expect(variations[1].locked).toBe(true);
+ });
+
+ it('mints stable ids for composite sheets + locks them', async () => {
+ writeJson(universesPath, {
+ universes: [
+ {
+ id: 'u-1',
+ compositeSheets: [{ kind: 'reference_sheet', label: 'X', prompt: 'long prompt' }],
+ },
+ ],
+ });
+ const result = await migration.up({ rootDir });
+ expect(result.mintedSheetIds).toBe(1);
+ expect(result.lockedSheets).toBe(1);
+ const after = readJson(universesPath);
+ expect(after.universes[0].compositeSheets[0].id).toMatch(/^sheet-/);
+ expect(after.universes[0].compositeSheets[0].locked).toBe(true);
+ });
+
+ it('back-fills imageRefs on variations from completed media jobs via category+label match', async () => {
+ writeJson(universesPath, {
+ universes: [
+ {
+ id: 'u-1',
+ categories: {
+ landscapes: {
+ variations: [{ id: 'var-1', label: 'Crystal Canyon', prompt: 'a' }],
+ },
+ },
+ },
+ ],
+ });
+ writeJson(jobsPath, {
+ jobs: [
+ {
+ id: 'job-old',
+ kind: 'image',
+ status: 'completed',
+ params: {
+ universeRun: {
+ universeId: 'u-1',
+ category: 'landscapes',
+ label: 'Crystal Canyon',
+ },
+ },
+ result: { filename: 'render-aaa.png' },
+ },
+ // Non-image / non-completed jobs are ignored.
+ { kind: 'video', status: 'completed', params: { universeRun: { universeId: 'u-1', category: 'landscapes', label: 'Crystal Canyon' } }, result: { filename: 'ignore-me.mp4' } },
+ { kind: 'image', status: 'running', params: { universeRun: { universeId: 'u-1', category: 'landscapes', label: 'Crystal Canyon' } }, result: { filename: 'wip.png' } },
+ ],
+ });
+ const result = await migration.up({ rootDir });
+ expect(result.appendedRefs).toBe(1);
+ const after = readJson(universesPath);
+ expect(after.universes[0].categories.landscapes.variations[0].imageRefs).toEqual(['render-aaa.png']);
+ });
+
+ it('normalizes category keys when matching jobs against variations', async () => {
+ // Job tagged with a non-normalized category name should still match the
+ // sanitized variation bucket (mirrors the runtime `normalizeCategoryKey`
+ // behavior).
+ writeJson(universesPath, {
+ universes: [
+ {
+ id: 'u-1',
+ categories: {
+ raider_pirate_clans: {
+ variations: [{ id: 'var-1', label: 'Wake Jackals', prompt: 'a' }],
+ },
+ },
+ },
+ ],
+ });
+ writeJson(jobsPath, {
+ jobs: [
+ {
+ kind: 'image', status: 'completed',
+ params: { universeRun: { universeId: 'u-1', category: 'Raider / Pirate Clans', label: 'wake jackals' } },
+ result: { filename: 'render-bbb.png' },
+ },
+ ],
+ });
+ await migration.up({ rootDir });
+ const after = readJson(universesPath);
+ expect(after.universes[0].categories.raider_pirate_clans.variations[0].imageRefs).toEqual(['render-bbb.png']);
+ });
+
+ it('is idempotent — second run reports zero changes', async () => {
+ writeJson(universesPath, {
+ universes: [
+ {
+ id: 'u-1',
+ characters: [{ id: 'c-1', name: 'Alex' }],
+ categories: { landscapes: { variations: [{ label: 'Tower', prompt: 'a' }] } },
+ },
+ ],
+ });
+ const first = await migration.up({ rootDir });
+ expect(first.lockedCanon).toBe(1);
+ expect(first.lockedVariations).toBe(1);
+ const second = await migration.up({ rootDir });
+ expect(second.lockedCanon).toBe(0);
+ expect(second.lockedVariations).toBe(0);
+ expect(second.mintedVariationIds).toBe(0);
+ });
+
+ it('returns `no-universes` when the data file is missing', async () => {
+ // Skip writing universesPath — the loader should bail.
+ const result = await migration.up({ rootDir });
+ expect(result.reason).toBe('no-universes');
+ });
+});
diff --git a/server/services/universeBuilder.js b/server/services/universeBuilder.js
index 376b9efb75..981e9c8ea8 100644
--- a/server/services/universeBuilder.js
+++ b/server/services/universeBuilder.js
@@ -1209,11 +1209,14 @@ export async function setVariationsLockAll(universeId, { categoryKey = null, loc
let touchedCategories = false;
for (const [key, bucket] of Object.entries(categories)) {
const variations = Array.isArray(bucket?.variations) ? bucket.variations : [];
- total += variations.length;
if (categoryKey && key !== categoryKey) {
nextCategories[key] = bucket;
continue;
}
+ // Increment `total` only for buckets the caller actually targeted —
+ // otherwise a single-bucket lock-all would report every variation in
+ // every bucket as the denominator and the response toast lies.
+ total += variations.length;
let bucketTouched = false;
const nextVariations = variations.map((v) => {
if (!v || typeof v !== 'object') return v;
diff --git a/server/services/universeBuilder.test.js b/server/services/universeBuilder.test.js
index 911771645f..c2651cf520 100644
--- a/server/services/universeBuilder.test.js
+++ b/server/services/universeBuilder.test.js
@@ -1208,6 +1208,83 @@ describe("universeBuilder service", () => {
});
});
+ describe("setVariationsLockAll", () => {
+ const seedTwoBuckets = () =>
+ seedWorld({
+ categories: {
+ landscapes: {
+ variations: [
+ { label: "A", prompt: "a" },
+ { label: "B", prompt: "b", locked: false },
+ ],
+ },
+ outfits: {
+ variations: [
+ { label: "X", prompt: "x" },
+ { label: "Y", prompt: "y" },
+ ],
+ },
+ },
+ });
+
+ it("scopes total + changed counts to a single bucket when categoryKey is set", async () => {
+ // Regression guard: previously `total += variations.length` ran before
+ // the categoryKey filter, so a single-bucket call over-reported the
+ // denominator as every variation universe-wide.
+ const w = await seedTwoBuckets();
+ const res = await svc.setVariationsLockAll(w.id, {
+ categoryKey: "landscapes",
+ locked: false,
+ });
+ expect(res.total).toBe(2); // only landscapes' variations counted
+ expect(res.changed).toBe(1); // only "A" flipped (true → false); "B" already false
+ const reread = (await svc.listUniverses())[0];
+ expect(reread.categories.landscapes.variations.every((v) => v.locked === false)).toBe(true);
+ // Other bucket untouched.
+ expect(reread.categories.outfits.variations.every((v) => v.locked === true)).toBe(true);
+ });
+
+ it("universe-wide path locks every variation across every bucket", async () => {
+ const w = await seedTwoBuckets();
+ // First, unlock everything explicitly so the test's "lock all" call has
+ // real work to do (sanitizer defaults new variations to locked).
+ await svc.setVariationsLockAll(w.id, { locked: false });
+ const res = await svc.setVariationsLockAll(w.id, { locked: true });
+ expect(res.total).toBe(4); // 2 landscapes + 2 outfits
+ expect(res.changed).toBe(4);
+ const reread = (await svc.listUniverses())[0];
+ for (const bucket of Object.values(reread.categories)) {
+ expect(bucket.variations.every((v) => v.locked === true)).toBe(true);
+ }
+ });
+
+ it("includeSheets: true also flips composite sheets in the same call (only when no categoryKey)", async () => {
+ const w = await seedWorld({
+ compositeSheets: [
+ { label: "Cover board", prompt: "cover board prompt long enough" },
+ ],
+ });
+ await svc.setVariationsLockAll(w.id, { locked: false, includeSheets: true });
+ const reread = (await svc.listUniverses())[0];
+ expect(reread.compositeSheets[0].locked).toBe(false);
+ });
+
+ it("ignores includeSheets when a categoryKey is set (single-bucket scope is strict)", async () => {
+ const w = await seedWorld({
+ compositeSheets: [
+ { label: "Cover board", prompt: "cover board prompt long enough" },
+ ],
+ });
+ await svc.setVariationsLockAll(w.id, {
+ categoryKey: "landscapes",
+ locked: false,
+ includeSheets: true,
+ });
+ const reread = (await svc.listUniverses())[0];
+ expect(reread.compositeSheets[0].locked).toBe(true); // still locked
+ });
+ });
+
describe("locked", () => {
it("round-trips a sparse lock map and replaces wholesale on patch", async () => {
const w = await seedWorld({
diff --git a/server/services/universeCanon.test.js b/server/services/universeCanon.test.js
index 8714e08e3f..4bb524d8cc 100644
--- a/server/services/universeCanon.test.js
+++ b/server/services/universeCanon.test.js
@@ -104,6 +104,53 @@ describe('universeCanon — setCanonEntryLock', () => {
});
});
+describe('universeCanon — setCanonKindLockAll', () => {
+ it('locks every entry of the kind, counts only entries whose state changed', async () => {
+ // Two already-locked + one explicitly-unlocked: only the unlocked entry
+ // should count toward `changed`. Pins the "no-op on already-target"
+ // contract that the toast relies on for "Locked N characters".
+ const w = await seedUniverseWithCharacters([
+ { name: 'Alex', physicalDescription: 'a', locked: true },
+ { name: 'Beth', physicalDescription: 'b', locked: true },
+ { name: 'Cara', physicalDescription: 'c', locked: false },
+ ]);
+ const result = await canonSvc.setCanonKindLockAll(w.id, 'character', true);
+ expect(result.changed).toBe(1);
+ expect(result.total).toBe(3);
+ expect(result.locked).toBe(true);
+ // Persisted: every entry comes back `locked: true` on the next read.
+ const reread = (await svc.listUniverses())[0];
+ expect(reread.characters.every((c) => c.locked === true)).toBe(true);
+ });
+
+ it('unlock-all writes explicit locked:false so the bit survives the lock-by-default sanitizer', async () => {
+ const w = await seedUniverseWithCharacters([
+ { name: 'Alex', physicalDescription: 'a', locked: true },
+ { name: 'Beth', physicalDescription: 'b', locked: true },
+ ]);
+ const result = await canonSvc.setCanonKindLockAll(w.id, 'character', false);
+ expect(result.changed).toBe(2);
+ const reread = (await svc.listUniverses())[0];
+ expect(reread.characters.every((c) => c.locked === false)).toBe(true);
+ });
+
+ it('short-circuits a write when nothing changes', async () => {
+ const w = await seedUniverseWithCharacters([
+ { name: 'Alex', physicalDescription: 'a', locked: true },
+ ]);
+ const result = await canonSvc.setCanonKindLockAll(w.id, 'character', true);
+ expect(result.changed).toBe(0);
+ expect(result.total).toBe(1);
+ });
+
+ it('rejects unknown kind with a 400-coded ServerError', async () => {
+ const w = await seedUniverseWithCharacters([]);
+ await expect(
+ canonSvc.setCanonKindLockAll(w.id, 'monster', true),
+ ).rejects.toMatchObject({ status: 400, code: 'UNIVERSE_CANON_INVALID_KIND' });
+ });
+});
+
describe('universeCanon — refineUniverseCharacter respects locks', () => {
it('refuses with 409 when the target character is locked', async () => {
const w = await seedUniverseWithCharacters([