import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import vm from 'node:vm'; let records = [{ id:'legacy',date:'2026-10-13',text:'Original daily summary',mood:'clear',energy:5 }]; let favorites = [{id:'favorite-legacy',memoryId:'legacy'}]; const context = vm.createContext({ sys:()=>({tasksBoard:{tasks:[],goals:[]}}), mems:()=>structuredClone(records), museum:()=>structuredClone(favorites), save:(key,value)=>{if(key==='lifeos_memories') records=structuredClone(value);if(key==='lifeos_museum') favorites=structuredClone(value);}, renderAll(){}, filmFor:()=>({filmTitle:'Prompt',filmSubtitle:'',filmLine:''}), day:()=> '2026-10-13', crypto:globalThis.crypto }); context.window=context; vm.runInContext(readFileSync(new URL('../public/data-actions.js',import.meta.url),'utf8'),context); const action=context.LifeOSActions; const first=action.createMemory({kind:'moment',text:'First moment',date:'2026-10-13'}); const second=action.createMemory({kind:'moment',text:'Second moment',date:'2026-10-13'}); assert.notEqual(first.id,second.id); assert.equal(records.length,3); assert.equal(records.find(item=>item.id==='legacy').text,'Original daily summary'); assert.equal(first.contextRecorded,false,'quick notes must not invent energy/weather observations'); const daily=action.createMemory({date:'2026-10-13',text:'Updated daily summary',replace:true}); assert.equal(daily.id,'legacy'); assert.equal(records.length,3,'updating summary preserves all same-day moments'); assert.equal(records.filter(item=>item.kind==='moment').length,2); assert.throws(()=>action.createMemory({date:'2026-10-13',text:'Duplicate summary'}),/memoryExists/); assert.throws(()=>action.createMemory({kind:'moment',text:' '}),/invalidTitle/); assert.throws(()=>action.createMemory({kind:'moment',text:'Note',date:'2026-02-30'}),/invalidDate/); action.deleteMemory(first.id); assert.equal(records.length,3); assert.equal(context.mems().length,2); assert.ok(records.find(item=>item.id===first.id).trashedAt); action.restoreMemory(first.id); assert.equal(context.mems().length,3); action.deleteMemory(first.id); action.purgeMemory(first.id); assert.equal(records.length,2); assert.ok(records.some(item=>item.id===second.id)); assert.ok(records.some(item=>item.id==='legacy')); const renderer = readFileSync(new URL('../public/i18n-renderers.js',import.meta.url),'utf8'); context.importData = { memories:[{id:'imported-moment',kind:'moment',date:'2026-10-13',text:'Imported'}, {id:'duplicate-daily',date:'2026-10-13',text:'Should not replace summary'}], museum:[] }; context.rootModal = () => ({innerHTML:''}); context.toast=()=>{}; context.setTimeout=()=>0; vm.runInContext(renderer.slice(renderer.indexOf(' window.applyImport = mode => {'), renderer.indexOf(' window.resetConfirm =')),context); context.applyImport('merge'); assert.ok(records.some(item=>item.id==='imported-moment'),'merge preserves another moment on an existing date'); assert.ok(!records.some(item=>item.id==='duplicate-daily'),'merge does not introduce a second daily summary'); context.applyImport('merge'); assert.equal(records.filter(item=>item.id==='imported-moment').length,1,'repeated import is idempotent'); action.deleteMemory('legacy'); assert.equal(context.museum().length,0,'trashed favorites are hidden'); assert.equal(favorites.length,1,'favorite relationship survives trashing'); const replacement = action.createMemory({date:'2026-10-13',text:'Replacement summary'}); assert.ok(records.some(item=>item.id==='legacy' && item.trashedAt),'creating a summary retains trash'); assert.throws(()=>action.restoreMemory('legacy'),/memoryExists/,'restore never overwrites a new summary'); action.deleteMemory(replacement.id); action.restoreMemory('legacy'); assert.equal(context.museum().length,1,'restoring retains favorite ID'); assert.equal(context.museum()[0].id,'favorite-legacy'); action.deleteMemory('legacy'); action.purgeMemory('legacy'); assert.equal(favorites.length,0,'permanent deletion removes only the corresponding favorite'); console.log('Life OS same-day moments, legacy summary compatibility and deletion valid');