rhizome/__tests__/lossy.ts

78 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-12-23 23:30:54 -06:00
import {Lossless, LosslessViewMany} from "../src/lossless";
2024-12-23 23:36:38 -06:00
import {Lossy, firstValueFromLosslessViewOne, valueFromCollapsedDelta} from "../src/lossy";
import {PointerTarget} from "../src/types";
2024-12-23 23:30:54 -06:00
describe('Lossy', () => {
describe('se a provided function to resolve entity views', () => {
const lossless = new Lossless();
const lossy = new Lossy(lossless);
beforeAll(() => {
lossless.ingestDelta({
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"
}]
});
});
it('example summary', () => {
type Role = {
actor: PointerTarget,
film: PointerTarget,
role: PointerTarget
2024-12-23 23:30:54 -06:00
};
type Summary = {
roles: Role[];
};
const resolver = (losslessView: LosslessViewMany): Summary => {
const roles: Role[] = [];
for (const [id, ent] of Object.entries(losslessView)) {
if (ent.referencedAs.includes("role")) {
const {delta, value: actor} = firstValueFromLosslessViewOne(ent, "actor") ?? {};
if (!delta) continue; // TODO: panic
if (!actor) continue; // TODO: panic
2024-12-23 23:36:38 -06:00
const film = valueFromCollapsedDelta(delta, "film");
2024-12-23 23:30:54 -06:00
if (!film) continue; // TODO: panic
roles.push({
role: id,
actor,
film
});
}
}
return {roles};
}
const result = lossy.resolve(resolver);
expect(result).toEqual({
roles: [{
film: "the_matrix",
role: "neo",
actor: "keanu"
}]
});
});
});
});