fix(destination-buffer): return entries in chronological order (oldest-first) and adjust pagination semantics

This commit is contained in:
2026-02-20 08:57:22 +00:00
parent dbcfeba16d
commit 9a61a3a9af
4 changed files with 24 additions and 13 deletions

View File

@@ -1,5 +1,13 @@
# Changelog # Changelog
## 2026-02-20 - 3.2.1 - fix(destination-buffer)
return entries in chronological order (oldest-first) and adjust pagination semantics
- Change getEntries to return the most recent entries in chronological (oldest-first) order instead of newest-first
- Adjust pagination to compute slice indices from the newest end (start = max(0, len - limit - offset), end = len - offset)
- Update tests to expect chronological ordering and clarified pagination examples
- Modified files: ts_destination_buffer/classes.destinationbuffer.ts, test/test.destination-buffer.node.ts
## 2026-02-19 - 3.2.0 - feat(destination-buffer) ## 2026-02-19 - 3.2.0 - feat(destination-buffer)
add SmartlogDestinationBuffer in-memory circular buffer destination with query/filter/pagination and tests add SmartlogDestinationBuffer in-memory circular buffer destination with query/filter/pagination and tests

View File

@@ -36,11 +36,11 @@ tap.test('should store log entries via handleLog', async () => {
expect(buffer.getEntryCount()).toEqual(3); expect(buffer.getEntryCount()).toEqual(3);
}); });
tap.test('should retrieve entries newest-first', async () => { tap.test('should retrieve entries in chronological order (oldest-first)', async () => {
const entries = buffer.getEntries(); const entries = buffer.getEntries();
expect(entries.length).toEqual(3); expect(entries.length).toEqual(3);
expect(entries[0].message).toEqual('Watch out'); expect(entries[0].message).toEqual('Hello world');
expect(entries[2].message).toEqual('Hello world'); expect(entries[2].message).toEqual('Watch out');
}); });
tap.test('should filter entries by level', async () => { tap.test('should filter entries by level', async () => {
@@ -59,10 +59,13 @@ tap.test('should filter entries by search string', async () => {
}); });
tap.test('should support limit and offset pagination', async () => { tap.test('should support limit and offset pagination', async () => {
// limit=2, offset=0 → last 2 entries in chronological order
const page1 = buffer.getEntries({ limit: 2, offset: 0 }); const page1 = buffer.getEntries({ limit: 2, offset: 0 });
expect(page1.length).toEqual(2); expect(page1.length).toEqual(2);
expect(page1[0].message).toEqual('Watch out'); expect(page1[0].message).toEqual('Something failed');
expect(page1[1].message).toEqual('Watch out');
// limit=2, offset=2 → skip 2 from end, return up to 2
const page2 = buffer.getEntries({ limit: 2, offset: 2 }); const page2 = buffer.getEntries({ limit: 2, offset: 2 });
expect(page2.length).toEqual(1); expect(page2.length).toEqual(1);
expect(page2[0].message).toEqual('Hello world'); expect(page2[0].message).toEqual('Hello world');
@@ -94,10 +97,10 @@ tap.test('should enforce circular buffer max entries', async () => {
expect(smallBuffer.getEntryCount()).toEqual(5); expect(smallBuffer.getEntryCount()).toEqual(5);
// Should have kept the latest 5 (messages 5-9) // Should have kept the latest 5 (messages 5-9), returned chronologically
const entries = smallBuffer.getEntries({ limit: 10 }); const entries = smallBuffer.getEntries({ limit: 10 });
expect(entries[0].message).toEqual('Message 9'); expect(entries[0].message).toEqual('Message 5');
expect(entries[4].message).toEqual('Message 5'); expect(entries[4].message).toEqual('Message 9');
}); });
tap.test('should clear all entries', async () => { tap.test('should clear all entries', async () => {

View File

@@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartlog', name: '@push.rocks/smartlog',
version: '3.2.0', version: '3.2.1',
description: 'A minimalistic, distributed, and extensible logging tool supporting centralized log management.' description: 'A minimalistic, distributed, and extensible logging tool supporting centralized log management.'
} }

View File

@@ -50,11 +50,11 @@ export class SmartlogDestinationBuffer implements ILogDestination {
results = results.filter((pkg) => pkg.timestamp >= options.since); results = results.filter((pkg) => pkg.timestamp >= options.since);
} }
// Return newest-first, with pagination // Return most recent `limit` entries in chronological order (oldest-first)
return results // offset skips from the newest end
.slice() const start = Math.max(0, results.length - limit - offset);
.reverse() const end = results.length - offset;
.slice(offset, offset + limit); return results.slice(Math.max(0, start), Math.max(0, end));
} }
public getEntryCount(): number { public getEntryCount(): number {