rhizome/__tests__/lossless.ts

173 lines
3.9 KiB
TypeScript
Raw Normal View History

import {Delta, DeltaFilter} from '../src/delta.js';
import {Lossless} from '../src/lossless.js';
2024-12-31 12:28:24 -06:00
import {RhizomeNode} from '../src/node.js';
2024-12-23 17:29:38 -06:00
describe('Lossless', () => {
2024-12-31 12:28:24 -06:00
const node = new RhizomeNode();
it('creates a lossless view of keanu as neo in the matrix', () => {
const delta = new Delta({
2024-12-23 17:29:38 -06:00
creator: 'a',
host: 'h',
pointers: [{
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"
}]
});
2024-12-23 17:29:38 -06:00
2024-12-31 12:28:24 -06:00
const lossless = new Lossless(node);
2024-12-23 17:29:38 -06:00
lossless.ingestDelta(delta);
expect(lossless.view()).toMatchObject({
2024-12-23 17:29:38 -06:00
keanu: {
2024-12-23 23:30:21 -06:00
referencedAs: ["actor"],
properties: {
roles: [{
creator: "a",
host: "h",
pointers: [
{actor: "keanu"},
{role: "neo"},
{film: "the_matrix"},
{base_salary: 1000000},
{salary_currency: "usd"},
],
}],
},
2024-12-23 17:29:38 -06:00
},
neo: {
2024-12-23 23:30:21 -06:00
referencedAs: ["role"],
properties: {
actor: [{
creator: "a",
host: "h",
pointers: [
{actor: "keanu"},
{role: "neo"},
{film: "the_matrix"},
{base_salary: 1000000},
{salary_currency: "usd"},
],
}],
},
2024-12-23 17:29:38 -06:00
},
the_matrix: {
2024-12-23 23:30:21 -06:00
referencedAs: ["film"],
properties: {
cast: [{
creator: "a",
host: "h",
pointers: [
{actor: "keanu"},
{role: "neo"},
{film: "the_matrix"},
{base_salary: 1000000},
{salary_currency: "usd"},
],
}],
},
},
2024-12-23 17:29:38 -06:00
});
});
describe('can filter deltas', () => {
2024-12-31 12:28:24 -06:00
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: {
2024-12-23 23:30:21 -06:00
referencedAs: ["1", "14"],
properties: {
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"],
properties: {
value: [{
creator: 'A',
host: 'H',
pointers: [
{"1": "ace"},
]
}]
}
}
});
expect(lossless.view(["ace"], filter)).toMatchObject({
ace: {
2024-12-23 23:30:21 -06:00
referencedAs: ["1"],
properties: {
value: [{
creator: 'A',
host: 'H',
pointers: [
{"1": "ace"},
]
}]
}
}
});
});
2024-12-23 17:29:38 -06:00
});
});