refactor(plugins): async DB-touching methods; transactions through UnitOfWork

This commit is contained in:
jubnl
2026-09-22 00:00:53 +02:00
parent e413308227
commit e28dc6de28
68 changed files with 743 additions and 706 deletions
@@ -53,7 +53,7 @@ beforeEach(() => {
});
describe('plugin settings are isolated from core and from each other', () => {
it('PSET-001 — a plugin declaring "webhook_url" cannot touch the CORE settings row', () => {
it('PSET-001 — a plugin declaring "webhook_url" cannot touch the CORE settings row', async () => {
testDb.prepare("INSERT INTO settings (user_id, key, value) VALUES (?, 'webhook_url', 'https://core.example.com/real')").run(uid);
declareField('evil', 'webhook_url');
setUserConfig('evil', { webhook_url: 'https://attacker.example.com' });
@@ -62,43 +62,43 @@ describe('plugin settings are isolated from core and from each other', () => {
// its own blob, in its own table. The namespacing is structural, not by key naming.
const core = testDb.prepare("SELECT value FROM settings WHERE user_id = ? AND key = 'webhook_url'").get(uid) as { value: string };
expect(core.value).toBe('https://core.example.com/real');
expect(userSettings().readAll('evil', uid)).toEqual({ webhook_url: 'https://attacker.example.com' });
expect(await userSettings().readAll('evil', uid)).toEqual({ webhook_url: 'https://attacker.example.com' });
});
it('PSET-002 — plugin A cannot read plugin B’s config, even with the same key name', () => {
it('PSET-002 — plugin A cannot read plugin B’s config, even with the same key name', async () => {
declareField('a', 'token', { secret: true });
declareField('b', 'token', { secret: true });
setUserConfig('b', { token: 'B-SECRET' });
expect(userSettings().readAll('a', uid)).toEqual({});
expect(userSettings().readOne('a', uid, 'token')).toBeUndefined();
expect(userSettings().readOne('b', uid, 'token')).toBe('B-SECRET');
expect(await userSettings().readAll('a', uid)).toEqual({});
expect(await userSettings().readOne('a', uid, 'token')).toBeUndefined();
expect(await userSettings().readOne('b', uid, 'token')).toBe('B-SECRET');
});
it('PSET-003 — a plugin only ever sees its own DECLARED keys', () => {
it('PSET-003 — a plugin only ever sees its own DECLARED keys', async () => {
declareField('p', 'declared');
// An undeclared key that somehow reached the blob is not handed to the plugin.
setUserConfig('p', { declared: 'yes', sneaked: 'no' });
expect(userSettings().readAll('p', uid)).toEqual({ declared: 'yes' });
expect(await userSettings().readAll('p', uid)).toEqual({ declared: 'yes' });
});
});
describe('settings keys cannot resolve off the prototype chain', () => {
it.each(['__proto__', 'constructor', 'prototype'])(
'PSET-004 — a REQUIRED field named "%s" is NOT reported as configured',
(key) => {
async (key) => {
declareField('evil', key, { required: true });
// The user has configured nothing at all.
expect(userSettings().hasRequired('evil', uid)).toBe(false);
expect(userSettings().readAll('evil', uid)).toEqual({});
expect(await userSettings().hasRequired('evil', uid)).toBe(false);
expect(await userSettings().readAll('evil', uid)).toEqual({});
},
);
it('PSET-005 — a genuinely configured required field still reports configured', () => {
it('PSET-005 — a genuinely configured required field still reports configured', async () => {
declareField('good', 'appToken', { required: true, secret: true });
expect(userSettings().hasRequired('good', uid)).toBe(false);
expect(await userSettings().hasRequired('good', uid)).toBe(false);
setUserConfig('good', { appToken: 'T' });
expect(userSettings().hasRequired('good', uid)).toBe(true);
expect(await userSettings().hasRequired('good', uid)).toBe(true);
});
it('PSET-006 — the manifest rejects such a key at install', () => {
@@ -132,7 +132,7 @@ describe('a plugin channel label is bounded by the host', () => {
id: 'loud', status: 'active', hooks: ['notificationChannel'], granted: new Set(['hook:notification-channel']),
});
const [channel] = rt.notificationChannels();
const [channel] = await rt.notificationChannels();
expect(channel.id).toBe('plugin:loud');
expect(channel.label!.length).toBeLessThanOrEqual(40);
expect(channel.label).not.toMatch(/\p{Extended_Pictographic}/u);