mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
151 lines
7.3 KiB
JavaScript
151 lines
7.3 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const vm = require('node:vm');
|
|
const ts = require('typescript');
|
|
const source = file => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
|
function load(file, modules, globals = {}) {
|
|
const context = { exports: {}, require: name => {
|
|
assert.ok(name in modules, `Unexpected import ${name}`);
|
|
return modules[name];
|
|
}, ...globals };
|
|
vm.runInNewContext(ts.transpileModule(source(file), {
|
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
|
}).outputText, context);
|
|
return context.exports;
|
|
}
|
|
function fixture(settings = new Map()) {
|
|
const listeners = new Set();
|
|
const storage = {
|
|
getStringSetting: (key, fallback) => settings.get(key) ?? fallback,
|
|
setStringSetting: (key, value) => { settings.set(key, value); for (const fn of listeners) fn(key); },
|
|
onSettingsStoreChanged: fn => { listeners.add(fn); return () => listeners.delete(fn); },
|
|
};
|
|
const prefs = load('app/native/calendar-selection.ts', { './settings-store': storage });
|
|
let permitted = true;
|
|
const calls = [];
|
|
const calendars = [
|
|
{ id: '1', name: 'Work', accountName: 'one@example.com', accountType: 'google' },
|
|
{ id: '2', name: 'Work', accountName: 'two@example.com', accountType: 'google' },
|
|
];
|
|
const events = [
|
|
{ id: 1, calendarId: '1', startMs: 10000, endMs: 20000, title: 'Excluded early' },
|
|
{ id: 9, calendarId: '2', startMs: 30000, endMs: 40000, title: 'Recurrence one' },
|
|
{ id: 9, calendarId: '2', startMs: 50000, endMs: 60000, title: 'Recurrence two' },
|
|
];
|
|
const modules = {
|
|
'@nativescript/core': { Utils: { android: { getApplicationContext: () => ({}) } } },
|
|
'../g2/android-permissions': { hasCalendarPermission: () => permitted },
|
|
'./calendar-permissions': { hasCalendarPermission: () => permitted },
|
|
'./frame-timings': { spanCurrent: (_name, fn) => fn() },
|
|
'./calendar-selection': prefs,
|
|
'./settings-store': storage,
|
|
};
|
|
const globals = { global: { isAndroid: true }, com: { faceclaw: { app: { FaceclawCalendarProvider: {
|
|
getCalendarsJson: () => JSON.stringify(calendars),
|
|
getUpcomingEventsJson: (_ctx, max, window, selection) => {
|
|
calls.push({ max, window, selection });
|
|
const ids = JSON.parse(selection);
|
|
return JSON.stringify(events.filter(event => ids === null || ids.includes(event.calendarId)).slice(0, max));
|
|
},
|
|
} } } } };
|
|
const api = load('app/native/calendar.ts', modules, globals);
|
|
const sources = load('app/native/calendar-sources.ts', modules, globals);
|
|
return { api, prefs, sources, modules, globals, settings, calls, calendars,
|
|
permissions: value => { permitted = value; } };
|
|
}
|
|
|
|
test('calendar selection distinguishes all and none, survives restart and rejects invalid data', () => {
|
|
const f = fixture();
|
|
assert.equal(f.prefs.readCalendarSelection(), null);
|
|
f.prefs.setCalendarSelection([]);
|
|
assert.deepEqual(Array.from(fixture(f.settings).prefs.readCalendarSelection()), []);
|
|
f.prefs.setCalendarSelection(['2', '2', '1']);
|
|
assert.deepEqual(Array.from(fixture(f.settings).prefs.readCalendarSelection()), ['2', '1']);
|
|
f.prefs.setCalendarSelection(null);
|
|
assert.equal(fixture(f.settings).prefs.readCalendarSelection(), null);
|
|
assert.throws(() => f.prefs.setCalendarSelection(['1 OR 1=1']));
|
|
for (const value of ['oops', '{}', '[false]', '["bad"]']) {
|
|
f.settings.set(f.prefs.CALENDAR_SELECTION_KEY, value);
|
|
assert.equal(f.prefs.readCalendarSelection(), null);
|
|
}
|
|
});
|
|
|
|
test('shared reader forwards IDs before limiting, preserves recurrence occurrences and invalidates all cached query variants', async () => {
|
|
const f = fixture();
|
|
assert.equal(f.api.readUpcomingEvents(1)[0].title, 'Excluded early');
|
|
f.api.readUpcomingEvents(1);
|
|
assert.equal(f.calls.length, 1);
|
|
let notifications = 0;
|
|
const off = f.api.onCalendarChanged(() => notifications++);
|
|
f.prefs.setCalendarSelection(['2']);
|
|
const rows = f.api.readUpcomingEvents(2);
|
|
assert.deepEqual(Array.from(rows, event => event.id), [9, 9]);
|
|
assert.equal(f.calls.at(-1).selection, '["2"]');
|
|
assert.equal(notifications, 1);
|
|
assert.equal((await f.api.readUpcomingEventsAsync(1))[0].title, 'Recurrence one');
|
|
off();
|
|
f.prefs.setCalendarSelection([]);
|
|
assert.equal(notifications, 1);
|
|
assert.equal(f.api.readUpcomingEvents(1).length, 0, 'selection cache key works even without subscriptions');
|
|
assert.equal(fixture(f.settings).api.readUpcomingEvents().length, 0);
|
|
f.prefs.setCalendarSelection(['999']);
|
|
assert.equal(f.api.readUpcomingEvents().length, 0, 'deleted calendar does not revert to all');
|
|
f.permissions(false);
|
|
assert.equal(f.api.readUpcomingEvents().length, 0);
|
|
assert.equal(f.sources.readAvailableCalendars().length, 0);
|
|
});
|
|
|
|
test('calendar menu distinguishes equal names by account and ID; all, none, deleted and individual selections share preferences', () => {
|
|
const f = fixture();
|
|
const { CalendarFilterLayer } = load('app/apps/calendar/calendar-filter.ts', {
|
|
'../../graphics/ui-fonts': { getDefaultSmallFont: () => ({}) },
|
|
'../../graphics/textwrap': { truncateText: (_font, text) => text },
|
|
'../../native/calendar-selection': f.prefs,
|
|
'../../native/calendar-sources': f.sources,
|
|
'../../native/calendar-permissions': { hasCalendarPermission: () => true },
|
|
'../../ui/menu': {
|
|
drawToggleMenuItem: () => {},
|
|
MenuLayer: class {
|
|
constructor(_title, items) { this.items = items; this.selectedIndex = 0; }
|
|
selectItem(index) { this.selectedIndex = index; return this; }
|
|
setItems(items, index) { this.items = items; this.selectedIndex = index; }
|
|
paint() { return this.items; }
|
|
},
|
|
},
|
|
}, f.globals);
|
|
const layer = new CalendarFilterLayer();
|
|
const ctx = { stack: { getBaseSize: () => ({ width: 576 }) } };
|
|
const rows = () => layer.paint(ctx, () => {});
|
|
assert.ok(rows()[2].label.includes('one@example.com'));
|
|
assert.ok(rows()[3].label.includes('two@example.com'));
|
|
rows()[2].onSelect();
|
|
assert.deepEqual(Array.from(f.prefs.readCalendarSelection()), ['2']);
|
|
rows()[1].onSelect();
|
|
assert.equal(f.api.readUpcomingEvents().length, 0);
|
|
rows()[3].onSelect();
|
|
assert.deepEqual(Array.from(f.prefs.readCalendarSelection()), ['2']);
|
|
f.calendars.pop();
|
|
const missing = rows().find(row => row.label === 'Unavailable calendar #2');
|
|
assert.ok(missing);
|
|
missing.onSelect();
|
|
assert.equal(f.prefs.readCalendarSelection().length, 0);
|
|
rows()[0].onSelect();
|
|
assert.equal(f.prefs.readCalendarSelection(), null);
|
|
});
|
|
|
|
test('Android provider restricts Instances with bound calendar IDs before cutoff and limit', () => {
|
|
const kotlin = source('App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawCalendarProvider.kt');
|
|
assert.match(kotlin, /Instances\.CALENDAR_ID/);
|
|
assert.match(kotlin, /if \(ids != null && ids.length\(\) == 0\) return "\[\]"/);
|
|
assert.match(kotlin, /PROJECTION,\s+selection,\s+args,/);
|
|
assert.ok(kotlin.indexOf('selection,\n args,') < kotlin.indexOf('events.take(limit)'));
|
|
assert.match(kotlin, /CalendarContract\.Calendars\._ID/);
|
|
assert.match(kotlin, /CalendarContract\.Calendars\.ACCOUNT_NAME/);
|
|
for (const file of ['app/apps/calendar/calendar.ts', 'app/apps/glanceboard/widgets/calendar-widget.ts']) {
|
|
assert.match(source(file), /readUpcomingEvents/);
|
|
assert.match(source(file), /native\/calendar"/);
|
|
}
|
|
});
|