import test from 'node:test'; import assert from 'node:assert/strict'; import {mergeServerNotifications,acknowledgeNotifications,addLocalNotification} from '../src/services/notifications.js'; const rows=[{id:'one',baslik:'Tarih değişti',metin:'Yeni tarih',okundu:0,zaman:'2026-09-06T10:00:00Z'}]; test('refresh keeps stable notification identities and server unread state without acknowledging delivery',()=>{ const once=mergeServerNotifications([{id:'local',title:'Yerel',read:false}],rows); const twice=mergeServerNotifications(once,rows); assert.deepEqual(twice,once);assert.equal(twice[0].read,false);assert.equal(twice.length,2); assert.equal(mergeServerNotifications(twice,[{...rows[0],okundu:1}])[0].read,true); assert.throws(()=>mergeServerNotifications([],{}),/geçersiz/); }); test('read acknowledgement requires server success and only covers the captured unread snapshot',async()=>{ const current=mergeServerNotifications([{id:'local',read:false}],rows); await assert.rejects(acknowledgeNotifications(current,async()=>{throw new Error('depo kapalı');}),/depo kapalı/); assert.equal(current[0].read,false); await assert.rejects(acknowledgeNotifications(current,async()=>({success:true,okundu:0})),/kaydedilemedi/); const acknowledged=await acknowledgeNotifications(current,async ids=>{assert.deepEqual(ids,['one']);return {success:true,okundu:1};}); assert.deepEqual([...acknowledged],['server:one','local']);assert.equal(acknowledged.has('arrived-later'),false); }); test('local notification bursts preserve every loaded server record and its read state',()=>{ const remote=Array.from({length:100},(_,i)=>({id:String(i),baslik:'İhale '+i,metin:'Kaynak bildirimi',okundu:i%2,zaman:'2026-09-06T10:00:00Z'})); let list=mergeServerNotifications([],remote); const before=list.map(row=>({...row})); for(let i=0;i<40;i++)list=addLocalNotification(list,'Yerel '+i,'İşlem sonucu'); assert.deepEqual(list.filter(row=>row.serverId),before); const local=list.filter(row=>!row.serverId); assert.equal(local.length,30); assert.equal(local[0].title,'Yerel 39');assert.equal(local.at(-1).title,'Yerel 10'); assert.equal(new Set(local.map(row=>row.id)).size,30); const refreshed=mergeServerNotifications(list,remote.map(row=>({...row,okundu:1}))); assert.equal(refreshed.filter(row=>row.serverId).length,100); assert.ok(refreshed.filter(row=>row.serverId).every(row=>row.read)); assert.deepEqual(refreshed.filter(row=>!row.serverId),local); });