import {Delta, DeltaFilter, DeltaV2} from '../src/delta'; import {Lossless} from '../src/lossless'; import {RhizomeNode} from '../src/node'; describe('Lossless', () => { const node = new RhizomeNode(); it('creates a lossless view of keanu as neo in the matrix', () => { const delta = new DeltaV2({ creator: 'a', host: 'h', pointers: { actor: {"keanu": "roles"}, role: {"neo": "actor"}, film: {"the_matrix": "cast"}, base_salary: 1000000, salary_currency: "usd" } }).toV1(); expect(delta.pointers).toMatchObject([{ localContext: "actor", target: "keanu", targetContext: "roles" }, { localContext: "role", target: "neo", targetContext: "actor" }, { localContext: "film", target: "the_matrix", targetContext: "cast" }, { localContext: "base_salary", target: 1000000 }, { localContext: "salary_currency", target: "usd" }]); const lossless = new Lossless(node); lossless.ingestDelta(delta); expect(lossless.view()).toMatchObject({ keanu: { referencedAs: ["actor"], propertyDeltas: { roles: [{ creator: "a", host: "h", pointers: [ {actor: "keanu"}, {role: "neo"}, {film: "the_matrix"}, {base_salary: 1000000}, {salary_currency: "usd"}, ], }], }, }, neo: { referencedAs: ["role"], propertyDeltas: { actor: [{ creator: "a", host: "h", pointers: [ {actor: "keanu"}, {role: "neo"}, {film: "the_matrix"}, {base_salary: 1000000}, {salary_currency: "usd"}, ], }], }, }, the_matrix: { referencedAs: ["film"], propertyDeltas: { cast: [{ creator: "a", host: "h", pointers: [ {actor: "keanu"}, {role: "neo"}, {film: "the_matrix"}, {base_salary: 1000000}, {salary_currency: "usd"}, ], }], }, }, }); }); describe('can filter deltas', () => { const lossless = new Lossless(node); beforeAll(() => { lossless.ingestDelta(new Delta({ creator: 'A', host: 'H', pointers: [{ localContext: "1", target: "ace", targetContext: "value" }] })); lossless.ingestDelta(new Delta({ creator: 'B', host: 'H', pointers: [{ // 10 11j 12q 13k 14a localContext: "14", target: "ace", targetContext: "value" }] })); expect(lossless.view()).toMatchObject({ ace: { referencedAs: ["1", "14"], propertyDeltas: { value: [{ creator: 'A', host: 'H', pointers: [ {"1": "ace"}, ] }, { creator: 'B', host: 'H', pointers: [ {"14": "ace"}, ] }], } } }); }); it('filter by creator and host', () => { const filter: DeltaFilter = ({creator, host}) => { return creator === 'A' && host === 'H'; }; expect(lossless.view(undefined, filter)).toMatchObject({ ace: { referencedAs: ["1"], propertyDeltas: { value: [{ creator: 'A', host: 'H', pointers: [ {"1": "ace"}, ] }] } } }); expect(lossless.view(["ace"], filter)).toMatchObject({ ace: { referencedAs: ["1"], propertyDeltas: { value: [{ creator: 'A', host: 'H', pointers: [ {"1": "ace"}, ] }] } } }); }); // TODO: Test with transactions, say A1 -- B -- A2 }); });