86 lines
3.8 KiB
JavaScript
86 lines
3.8 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { readFileSync } from 'node:fs';
|
|
import vm from 'node:vm';
|
|
|
|
const memories = [
|
|
{ id: 'old', date: '2024-10-16', title: 'Old daily entry', text: 'Field notes' },
|
|
{ id: 'moment', date: '2026-10-16', kind: 'moment', title: 'Survey moment', tags: ['Lab "A"'], taskId: 'task' },
|
|
{ id: 'daily', date: '2026-10-16', kind: 'daily', title: 'Survey summary', tags: ['Lab "A"'], taskId: 'task' },
|
|
{ id: 'later', date: '2026-10-17', kind: 'moment', title: 'Survey walk', tags: ['Lab "A"'], taskId: 'task' }
|
|
];
|
|
const context = vm.createContext({
|
|
mems: () => memories,
|
|
linkedTasks: () => [{ id: 'task', title: 'Photography project' }],
|
|
museum: () => [{ memoryId: 'moment' }, { memoryId: 'later' }, { memoryId: 'old' }],
|
|
locale: () => 'en',
|
|
t: (key, values) => values ? `${values.count}/${values.total}` : key,
|
|
esc: value => value,
|
|
escAttr: value => value.replaceAll('"', '"'),
|
|
titleOf: memory => memory.title,
|
|
weather: () => 'sunny',
|
|
LifeI18n: { date: value => value }
|
|
});
|
|
context.window = context;
|
|
const source = readFileSync(new URL('../public/i18n-renderers.js', import.meta.url), 'utf8');
|
|
const start = source.indexOf(' const timelineFilterDefaults');
|
|
const end = source.indexOf(' window.museumApp', start);
|
|
assert.ok(start >= 0 && end > start);
|
|
vm.runInContext(source.slice(start, end), context);
|
|
|
|
const listeners = {};
|
|
const status = {}, results = {};
|
|
const fields = Object.fromEntries(['query', 'tag', 'kind', 'from', 'to', 'saved'].map(name => [name, {
|
|
name, setAttribute(key, value) { this[key] = value; }
|
|
}]));
|
|
const clear = { addEventListener: (_, listener) => { clear.click = listener; } };
|
|
const form = {
|
|
elements: fields,
|
|
querySelector: () => clear,
|
|
addEventListener: (event, listener) => { listeners[event] = listener; }
|
|
};
|
|
let renders = 0, searchFocused = false;
|
|
const container = {
|
|
set innerHTML(value) { renders++; assert.match(value, /value="Lab "A""/); },
|
|
querySelector: selector => ({ '.timeline-tools': form, '.timeline-status': status, '.timeline-results': results }[selector])
|
|
};
|
|
fields.query.focus = () => { searchFocused = true; };
|
|
const edit = (name, value, event = 'input', isComposing = false) => {
|
|
const target = fields[name];
|
|
target[name === 'saved' ? 'checked' : 'value'] = value;
|
|
listeners[event]({ target, isComposing });
|
|
};
|
|
|
|
context.timeline(container);
|
|
assert.equal(status.textContent, '4/4');
|
|
assert.equal(clear.disabled, true);
|
|
edit('kind', 'daily', 'change');
|
|
assert.equal(status.textContent, '2/4');
|
|
assert.match(results.innerHTML, /Old daily entry/, 'records without kind are daily summaries');
|
|
edit('kind', 'moment', 'change');
|
|
edit('tag', 'Lab "A"', 'change');
|
|
edit('saved', true, 'change');
|
|
edit('from', '2026-10-16', 'change');
|
|
edit('to', '2026-10-16', 'change');
|
|
edit('query', ' PHOTOGRAPHY ');
|
|
assert.equal(status.textContent, '1/4');
|
|
assert.match(results.innerHTML, /Survey moment/);
|
|
assert.doesNotMatch(results.innerHTML, /Survey summary|Survey walk/);
|
|
edit('query', '进行中', 'input', true);
|
|
assert.equal(status.textContent, '1/4', 'IME input waits for composition to end');
|
|
edit('query', '进行中', 'compositionend');
|
|
assert.equal(status.textContent, '0/4');
|
|
assert.match(results.innerHTML, /timeline.noMatches/);
|
|
assert.equal(renders, 1, 'filter changes preserve the form and its input focus');
|
|
edit('from', '2026-10-17', 'change');
|
|
assert.equal(status.textContent, 'timeline.invalidRange');
|
|
assert.equal(fields.from['aria-invalid'], 'true');
|
|
assert.equal(results.innerHTML, '');
|
|
clear.click();
|
|
assert.equal(fields.from['aria-invalid'], 'false');
|
|
assert.equal(fields.query.value, '');
|
|
assert.equal(fields.saved.checked, false);
|
|
assert.equal(status.textContent, '4/4');
|
|
assert.equal(searchFocused, true);
|
|
assert.equal(clear.disabled, true);
|
|
console.log('Life OS timeline combinations, legacy records, inclusive dates, IME and reset valid');
|