forked from WikiDeck/wikideck
72 lines
4.1 KiB
TypeScript
72 lines
4.1 KiB
TypeScript
import test from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import {
|
||
canonicalProfile, classIdsOf, profileFromSources, wikidataEntityParams, wikidataLabelParams, type WikidataEntity,
|
||
} from '../shared/duel/profile.ts';
|
||
import { classify } from '../shared/duel/classify.ts';
|
||
import { derive } from '../shared/duel/derive.ts';
|
||
|
||
const item = (id: string) => ({ rank: 'normal', mainsnak: { snaktype: 'value', datavalue: { type: 'wikibase-entityid', value: { id } } } });
|
||
const time = (t: string) => ({ rank: 'normal', mainsnak: { snaktype: 'value', datavalue: { type: 'time', value: { time: t } } } });
|
||
|
||
const EWELL: WikidataEntity = {
|
||
id: 'Q1351340',
|
||
claims: {
|
||
P31: [item('Q5')],
|
||
P27: [item('Q30')],
|
||
P569: [time('+1909-04-29T00:00:00Z')],
|
||
P570: [time('+1994-09-12T00:00:00Z')],
|
||
P19: [item('Q1018580')],
|
||
P106: [item('Q33999')],
|
||
P17: [{ rank: 'deprecated', mainsnak: { snaktype: 'value', datavalue: { value: { id: 'Q999' } } } }],
|
||
},
|
||
};
|
||
const ENRICH = { description: 'American actor (1909–1994)', wikidataId: 'Q1351340', categories: ['1994 deaths', 'American male actors'], revisionId: 42 };
|
||
|
||
test('profileFromSources reads P31, countries, years, death and cited items; ignores deprecated claims', () => {
|
||
const p = profileFromSources({ enrichment: ENRICH, entity: EWELL, labels: { Q5: 'human' }, links: [9, 3, 9] });
|
||
assert.deepEqual(p, {
|
||
wikidataId: 'Q1351340', description: 'American actor (1909–1994)', categories: ['1994 deaths', 'American male actors'],
|
||
instanceOf: [{ id: 'Q5', label: 'human' }], fetchedRevisionId: 42,
|
||
countries: ['Q30'], years: [1909, 1994], died: true, cites: ['Q1018580', 'Q30'], links: [3, 9],
|
||
});
|
||
assert.equal(classify(p).type, 'Figure');
|
||
});
|
||
|
||
test('BCE years are negative; malformed and novalue claims are skipped without throwing', () => {
|
||
const entity: WikidataEntity = { claims: {
|
||
P571: [time('-0753-04-21T00:00:00Z'), { mainsnak: { snaktype: 'novalue' } }, { mainsnak: { snaktype: 'value', datavalue: { value: 'junk' } } }],
|
||
P31: [item('not-a-qid'), item('Q515')],
|
||
} };
|
||
const p = profileFromSources({ enrichment: { ...ENRICH, categories: [] }, entity });
|
||
assert.deepEqual(p.years, [-753]);
|
||
assert.deepEqual(p.instanceOf, [{ id: 'Q515', label: 'Q515' }]);
|
||
assert.equal(classify(p).type, 'Location', 'Q-id anchors classify even without labels');
|
||
assert.equal(profileFromSources({ enrichment: ENRICH, entity: null }).instanceOf.length, 0);
|
||
});
|
||
|
||
test('without a Wikidata item the profile still classifies from categories and description', () => {
|
||
const p = profileFromSources({ enrichment: { description: 'Village in Poland', wikidataId: null, categories: ['Villages in Gmina X'], revisionId: 1 } });
|
||
assert.equal(classify(p).type, 'Location');
|
||
});
|
||
|
||
test('request parameters: claims-only entities, English labels, ≤ 50 sorted ids', () => {
|
||
assert.deepEqual(wikidataEntityParams(['Q5', 'Q1']), { action: 'wbgetentities', ids: 'Q1|Q5', props: 'claims', format: 'json', formatversion: '2' });
|
||
assert.equal(wikidataLabelParams(['Q5']).props, 'labels');
|
||
assert.throws(() => wikidataEntityParams([]), RangeError);
|
||
assert.throws(() => wikidataLabelParams(Array.from({ length: 51 }, (_, i) => `Q${i + 1}`)), RangeError);
|
||
assert.deepEqual(classIdsOf({ claims: { P31: [item('Q5')], P279: [item('Q2'), item('Q5')] } }), ['Q2', 'Q5']);
|
||
});
|
||
|
||
test('canonicalProfile is key-order independent, and the frozen profile derives the same card twice', () => {
|
||
const p = profileFromSources({ enrichment: ENRICH, entity: EWELL, labels: { Q5: 'human' } });
|
||
const shuffled = Object.fromEntries(Object.entries(p).reverse()) as typeof p;
|
||
assert.equal(canonicalProfile(shuffled), canonicalProfile(p));
|
||
const chain = { tokenId: 'ab'.repeat(32), article: { pageId: 1, title: 'Tom Ewell', extract: 'Tom Ewell was an American actor. More.', revisionId: 42 },
|
||
statsSnapshot: { pageBytes: 9000, languageCount: 30, stats: { attention: 60, depth: 70, reach: 50 } } };
|
||
const thawed = JSON.parse(canonicalProfile(p));
|
||
assert.deepEqual(derive(chain, thawed), derive(chain, p));
|
||
assert.equal(derive(chain, p).flavor, 'Tom Ewell was an American actor.');
|
||
assert.ok(derive(chain, p).keywords.includes('Legacy'));
|
||
});
|