mirror of
https://github.com/jkingsman/Remote-Terminal-for-MeshCore.git
synced 2026-08-12 03:33:03 +02:00
Investigate all fields on reconcile calls
This commit is contained in:
@@ -89,15 +89,20 @@ export function updateAck(messageId: number, ackCount: number, paths?: MessagePa
|
|||||||
* Preserves any older paginated messages not present in the fetched page.
|
* Preserves any older paginated messages not present in the fetched page.
|
||||||
*/
|
*/
|
||||||
export function reconcile(current: Message[], fetched: Message[]): Message[] | null {
|
export function reconcile(current: Message[], fetched: Message[]): Message[] | null {
|
||||||
const currentById = new Map<number, number>();
|
const currentById = new Map<number, { acked: number; pathsLen: number; text: string }>();
|
||||||
for (const m of current) {
|
for (const m of current) {
|
||||||
currentById.set(m.id, m.acked);
|
currentById.set(m.id, { acked: m.acked, pathsLen: m.paths?.length ?? 0, text: m.text });
|
||||||
}
|
}
|
||||||
|
|
||||||
let needsUpdate = false;
|
let needsUpdate = false;
|
||||||
for (const m of fetched) {
|
for (const m of fetched) {
|
||||||
const currentAck = currentById.get(m.id);
|
const cur = currentById.get(m.id);
|
||||||
if (currentAck === undefined || currentAck !== m.acked) {
|
if (
|
||||||
|
!cur ||
|
||||||
|
cur.acked !== m.acked ||
|
||||||
|
cur.pathsLen !== (m.paths?.length ?? 0) ||
|
||||||
|
cur.text !== m.text
|
||||||
|
) {
|
||||||
needsUpdate = true;
|
needsUpdate = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -363,6 +363,43 @@ describe('messageCache', () => {
|
|||||||
expect(merged).not.toBeNull();
|
expect(merged).not.toBeNull();
|
||||||
expect(merged!).toHaveLength(1);
|
expect(merged!).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('detects stale paths', () => {
|
||||||
|
const current = [
|
||||||
|
createMessage({ id: 1, acked: 1, paths: [{ path: '1A', received_at: 1700000000 }] }),
|
||||||
|
];
|
||||||
|
const fetched = [
|
||||||
|
createMessage({
|
||||||
|
id: 1,
|
||||||
|
acked: 1,
|
||||||
|
paths: [
|
||||||
|
{ path: '1A', received_at: 1700000000 },
|
||||||
|
{ path: '2B', received_at: 1700000001 },
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
|
const merged = messageCache.reconcile(current, fetched);
|
||||||
|
expect(merged).not.toBeNull();
|
||||||
|
expect(merged![0].paths).toHaveLength(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detects stale text (e.g. post-decryption)', () => {
|
||||||
|
const current = [createMessage({ id: 1, text: '[encrypted]' })];
|
||||||
|
const fetched = [createMessage({ id: 1, text: 'Hello world' })];
|
||||||
|
|
||||||
|
const merged = messageCache.reconcile(current, fetched);
|
||||||
|
expect(merged).not.toBeNull();
|
||||||
|
expect(merged![0].text).toBe('Hello world');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns null when acked, paths length, and text all match', () => {
|
||||||
|
const paths = [{ path: '1A', received_at: 1700000000 }];
|
||||||
|
const current = [createMessage({ id: 1, acked: 2, paths, text: 'Hello' })];
|
||||||
|
const fetched = [createMessage({ id: 1, acked: 2, paths, text: 'Hello' })];
|
||||||
|
|
||||||
|
expect(messageCache.reconcile(current, fetched)).toBeNull();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('clear', () => {
|
describe('clear', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user