refactor: update delta declarations to use DeltaBuilder in lossless tests

- Refactored delta declarations in __tests__/lossless.ts to use DeltaBuilder
- Added declareTransaction method to DeltaBuilder for better transaction handling
- Updated transaction pointer structure in DeltaBuilder to match expected format
- Fixed related tests to work with the new DeltaBuilder implementation
This commit is contained in:
Lentil Hoffman 2025-06-20 11:04:55 -05:00
parent 4542c4ce83
commit 5c1c8a23b8
Signed by: lentil
GPG Key ID: 0F5B99F3F4D0C087
3 changed files with 67 additions and 77 deletions

View File

@ -158,7 +158,7 @@ describe('DeltaBuilder', () => {
.buildV2(); .buildV2();
// Check for transaction ID in V2 pointers // Check for transaction ID in V2 pointers
expect(delta.pointers['_transaction']).toBe(txId); expect(delta.pointers['_transaction']).toEqual({ [txId]: 'deltas' });
}); });
it('should support negation', () => { it('should support negation', () => {

View File

@ -1,22 +1,19 @@
import {Delta, DeltaFilter, DeltaV2} from '../src/core'; import {Delta, DeltaFilter, DeltaV2} from '../src/core';
import {Lossless} from '../src/views'; import {Lossless} from '../src/views';
import {RhizomeNode} from '../src/node'; import {RhizomeNode} from '../src/node';
import {createDelta} from '../src/core/delta-builder';
describe('Lossless', () => { describe('Lossless', () => {
const node = new RhizomeNode(); const node = new RhizomeNode();
it('creates a lossless view of keanu as neo in the matrix', () => { it('creates a lossless view of keanu as neo in the matrix', () => {
const delta = new DeltaV2({ const delta = createDelta('a', 'h')
creator: 'a', .addPointer('actor', 'keanu', 'roles')
host: 'h', .addPointer('role', 'neo', 'actor')
pointers: { .addPointer('film', 'the_matrix', 'cast')
actor: {"keanu": "roles"}, .addPointer('base_salary', 1000000)
role: {"neo": "actor"}, .addPointer('salary_currency', 'usd')
film: {"the_matrix": "cast"}, .buildV1();
base_salary: 1000000,
salary_currency: "usd"
}
}).toV1();
expect(delta.pointers).toMatchObject([{ expect(delta.pointers).toMatchObject([{
localContext: "actor", localContext: "actor",
@ -95,17 +92,13 @@ describe('Lossless', () => {
}); });
it('accepts DeltaV2 instances', () => { it('accepts DeltaV2 instances', () => {
const delta = new DeltaV2({ const delta = createDelta('a', 'h')
creator: 'a', .addPointer('actor', 'keanu', 'roles')
host: 'h', .addPointer('role', 'neo', 'actor')
pointers: { .addPointer('film', 'the_matrix', 'cast')
actor: {"keanu": "roles"}, .addPointer('base_salary', 1000000)
role: {"neo": "actor"}, .addPointer('salary_currency', 'usd')
film: {"the_matrix": "cast"}, .buildV2();
base_salary: 1000000,
salary_currency: "usd"
}
});
const lossless = new Lossless(node); const lossless = new Lossless(node);
@ -167,26 +160,20 @@ describe('Lossless', () => {
const lossless = new Lossless(node); const lossless = new Lossless(node);
beforeAll(() => { beforeAll(() => {
lossless.ingestDelta(new Delta({ // First delta
creator: 'A', lossless.ingestDelta(
host: 'H', createDelta('A', 'H')
pointers: [{ .addPointer('1', 'ace', 'value')
localContext: "1", .buildV1()
target: "ace", );
targetContext: "value"
}]
}));
lossless.ingestDelta(new Delta({ // Second delta
creator: 'B', lossless.ingestDelta(
host: 'H', createDelta('B', 'H')
pointers: [{
// 10 11j 12q 13k 14a // 10 11j 12q 13k 14a
localContext: "14", .addPointer('14', 'ace', 'value')
target: "ace", .buildV1()
targetContext: "value" );
}]
}));
expect(lossless.view()).toMatchObject({ expect(lossless.view()).toMatchObject({
ace: { ace: {
@ -251,51 +238,42 @@ describe('Lossless', () => {
const transactionId = 'tx-filter-test'; const transactionId = 'tx-filter-test';
// Declare transaction with 3 deltas // Declare transaction with 3 deltas
losslessT.ingestDelta(new Delta({ losslessT.ingestDelta(
creator: 'system', createDelta('system', 'H')
host: 'H', .declareTransaction(transactionId, 3)
pointers: [ .buildV1()
{ localContext: '_transaction', target: transactionId, targetContext: 'size' }, );
{ localContext: 'size', target: 3 }
]
}));
// A1: First delta from creator A // A1: First delta from creator A
losslessT.ingestDelta(new Delta({ losslessT.ingestDelta(
creator: 'A', createDelta('A', 'H')
host: 'H', .inTransaction(transactionId)
pointers: [ .addPointer('step', 'process1', 'status')
{ localContext: '_transaction', target: transactionId, targetContext: 'deltas' }, .addPointer('value', 'started')
{ localContext: 'step', target: 'process1', targetContext: 'status' }, .buildV1()
{ localContext: 'value', target: 'started' } );
]
}));
// B: Delta from creator B // B: Delta from creator B
losslessT.ingestDelta(new Delta({ losslessT.ingestDelta(
creator: 'B', createDelta('B', 'H')
host: 'H', .inTransaction(transactionId)
pointers: [ .addPointer('step', 'process1', 'status')
{ localContext: '_transaction', target: transactionId, targetContext: 'deltas' }, .addPointer('value', 'processing')
{ localContext: 'step', target: 'process1', targetContext: 'status' }, .buildV1()
{ localContext: 'value', target: 'processing' } );
]
}));
// Transaction incomplete - nothing should show // Transaction incomplete - nothing should show
const incompleteView = losslessT.view(['process1']); const incompleteView = losslessT.view(['process1']);
expect(incompleteView.process1).toBeUndefined(); expect(incompleteView.process1).toBeUndefined();
// A2: Second delta from creator A completes transaction // A2: Second delta from creator A completes transaction
losslessT.ingestDelta(new Delta({ losslessT.ingestDelta(
creator: 'A', createDelta('A', 'H')
host: 'H', .inTransaction(transactionId)
pointers: [ .addPointer('step', 'process1', 'status')
{ localContext: '_transaction', target: transactionId, targetContext: 'deltas' }, .addPointer('value', 'completed')
{ localContext: 'step', target: 'process1', targetContext: 'status' }, .buildV1()
{ localContext: 'value', target: 'completed' } );
]
}));
// All deltas visible now // All deltas visible now
const completeView = losslessT.view(['process1']); const completeView = losslessT.view(['process1']);

View File

@ -51,6 +51,18 @@ export class DeltaBuilder {
this.transactionId = transactionId; this.transactionId = transactionId;
return this; return this;
} }
/**
* Declare a transaction with a size
* @param transactionId The ID of the transaction
* @param size The size of the transaction
* @returns
*/
declareTransaction(transactionId: string, size: number): this {
this.addPointer('_transaction', transactionId, 'size');
this.addPointer('size', size)
return this;
}
/** /**
* Mark this delta as a negation of another delta * Mark this delta as a negation of another delta
@ -99,7 +111,7 @@ export class DeltaBuilder {
const pointers = { ...this.pointers }; const pointers = { ...this.pointers };
if (this.transactionId) { if (this.transactionId) {
pointers['_transaction'] = this.transactionId; pointers['_transaction'] = { [this.transactionId]: 'deltas' };
} }
if (this.isNegation && this.negatedDeltaId) { if (this.isNegation && this.negatedDeltaId) {