2024-12-31 11:35:09 -06:00
|
|
|
import {Delta, PointerTarget} from "../src/delta.js";
|
|
|
|
import {Lossless, LosslessViewMany} from "../src/lossless.js";
|
|
|
|
import {Lossy, lastValueFromLosslessViewOne, valueFromCollapsedDelta } from "../src/lossy.js";
|
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(() => {
|
2024-12-27 13:43:43 -06:00
|
|
|
lossless.ingestDelta(new Delta({
|
2024-12-23 23:30:54 -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-27 13:43:43 -06:00
|
|
|
}));
|
2024-12-23 23:30:54 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
it('example summary', () => {
|
|
|
|
type Role = {
|
2024-12-26 15:59:03 -06:00
|
|
|
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")) {
|
2024-12-29 17:50:20 -06:00
|
|
|
const {delta, value: actor} = lastValueFromLosslessViewOne(ent, "actor") ?? {};
|
2024-12-23 23:30:54 -06:00
|
|
|
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};
|
|
|
|
}
|
|
|
|
|
2024-12-27 13:43:43 -06:00
|
|
|
const result = lossy.resolve<Summary>(resolver);
|
2024-12-23 23:30:54 -06:00
|
|
|
expect(result).toEqual({
|
|
|
|
roles: [{
|
|
|
|
film: "the_matrix",
|
|
|
|
role: "neo",
|
|
|
|
actor: "keanu"
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|