Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor: keep the consent helpers private and test the recheck through its cron handler (#8332)

[._.]/ Adam Eivy02555590de81
diff --git a/server/services/privacyRecheckScheduler.js b/server/services/privacyRecheckScheduler.js
index 95d927a5ef..1230ccbfeb 100644
--- a/server/services/privacyRecheckScheduler.js
+++ b/server/services/privacyRecheckScheduler.js
@@ -71,9 +71,9 @@ const SCHEDULED_PURPOSES = [
* is still refused. One subject's or purpose's failure must not abort the
* others, and this runs OUTSIDE the request lifecycle, so the per-pass
* try/catch is the sanctioned exception to the no-try/catch rule.
- * Exported for tests; the cron handler calls it after the enabled check.
+ * The cron handler calls it after the enabled check.
*/
-export async function runScheduledRecheck() {
+async function runScheduledRecheck() {
const subjects = await listSubjects();
for (const { scope, label, run } of SCHEDULED_PURPOSES) {
const granted = subjects.filter((s) => (s.activeScopes ?? []).includes(scope));
diff --git a/server/services/privacyRecheckScheduler.test.js b/server/services/privacyRecheckScheduler.test.js
index 2c339a81f8..d52c9e890d 100644
--- a/server/services/privacyRecheckScheduler.test.js
+++ b/server/services/privacyRecheckScheduler.test.js
@@ -10,7 +10,8 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
-const { listSubjectsMock, runScanPassMock, runOptOutPassMock } = vi.hoisted(() => ({
+const { listSubjectsMock, runScanPassMock, runOptOutPassMock, scheduleMock } = vi.hoisted(() => ({
+ scheduleMock: vi.fn(),
listSubjectsMock: vi.fn(),
runScanPassMock: vi.fn(async () => ({})),
runOptOutPassMock: vi.fn(async () => ({})),
@@ -19,11 +20,18 @@ const { listSubjectsMock, runScanPassMock, runOptOutPassMock } = vi.hoisted(() =
vi.mock('./privacySubjects.js', () => ({ listSubjects: listSubjectsMock }));
vi.mock('./privacyScan.js', () => ({ runScanPass: runScanPassMock }));
vi.mock('./privacyOptOut.js', () => ({ runOptOutPass: runOptOutPassMock }));
-vi.mock('./eventScheduler.js', () => ({ schedule: vi.fn(), cancel: vi.fn(), parseCronToNextRun: vi.fn() }));
-vi.mock('./settings.js', () => ({ getSettings: vi.fn(async () => ({})) }));
+vi.mock('./eventScheduler.js', () => ({ schedule: scheduleMock, cancel: vi.fn(), parseCronToNextRun: vi.fn() }));
+vi.mock('./settings.js', () => ({ getSettings: vi.fn(async () => ({ privacy: { recheck: { enabled: true } } })) }));
vi.mock('./userTimezone.js', () => ({ getUserTimezone: vi.fn(async () => 'UTC') }));
-const { runScheduledRecheck } = await import('./privacyRecheckScheduler.js');
+const { startPrivacyRecheckScheduler } = await import('./privacyRecheckScheduler.js');
+
+// Register the cron, then fire its handler the way the event scheduler would.
+const runScheduledRecheck = async () => {
+ await startPrivacyRecheckScheduler();
+ const { handler } = scheduleMock.mock.calls.at(-1)[0];
+ await handler();
+};
const ranFor = (mock) => mock.mock.calls.map(([arg]) => arg.subjectId);
diff --git a/server/services/privacySubjects.db.test.js b/server/services/privacySubjects.db.test.js
index 52bb74aa4f..65edfe9516 100644
--- a/server/services/privacySubjects.db.test.js
+++ b/server/services/privacySubjects.db.test.js
@@ -97,10 +97,9 @@ describe.skipIf(!runDb)('privacy household subjects DB round-trip', () => {
expect(consents[0]).toMatchObject({
subjectId: subject.id, scope: 'pii_vault', method: 'signed_form', note: 'form filed',
});
- expect(await subjects.hasActiveConsent(subject.id, 'pii_vault')).toBe(true);
// Creation grants local-vault use only — never a broker purpose (#8332).
- expect(await subjects.hasActiveConsent(subject.id, 'broker_scan')).toBe(false);
- expect(await subjects.hasActiveConsent(subject.id, 'broker_optout')).toBe(false);
+ const row = (await subjects.listSubjects()).find((s) => s.id === subject.id);
+ expect(row.activeScopes).toEqual(['pii_vault']);
});
it('reports consent and record counts on the list', async () => {
diff --git a/server/services/privacySubjects.js b/server/services/privacySubjects.js
index de1c3f70bb..e3e72c3e62 100644
--- a/server/services/privacySubjects.js
+++ b/server/services/privacySubjects.js
@@ -236,9 +236,10 @@ function assertKnownScope(scope) {
* Does this subject hold an ACTIVE (unrevoked) grant for exactly `scope`? A
* grant for a different purpose never counts — `pii_vault` (local storage)
* does not imply `broker_scan`, and `broker_scan` does not imply
- * `broker_optout`. Callers that need a hard stop use assertSubjectConsent.
+ * `broker_optout`. Module-private: callers use assertSubjectConsent (the hard
+ * stop) or listSubjects' `activeScopes` (the read-only view).
*/
-export async function hasActiveConsent(subjectId, scope) {
+async function hasActiveConsent(subjectId, scope) {
assertKnownScope(scope);
const resolved = resolveSubjectId(subjectId);
const { rows } = await query(
diff --git a/server/services/privacySubjects.test.js b/server/services/privacySubjects.test.js
index 30fc9ded03..985731106a 100644
--- a/server/services/privacySubjects.test.js
+++ b/server/services/privacySubjects.test.js
@@ -25,7 +25,7 @@ vi.mock('../lib/db.js', () => ({ query: queryMock, withTransaction: withTransact
const {
resolveSubjectId, createSubject, updateSubject, deleteSubject,
- listSubjects, assertSubject, hasActiveConsent, assertSubjectConsent, recordConsent,
+ listSubjects, assertSubject, assertSubjectConsent, recordConsent,
revokeConsent,
} = await import('./privacySubjects.js');
const { PRIVACY_SELF_SUBJECT_ID } = await import('../lib/privacyValidation.js');
@@ -203,7 +203,7 @@ describe('consent gate — purpose-scoped (#8332)', () => {
it('a pii_vault-only subject is refused BOTH broker purposes', async () => {
useConsentRows([{ subjectId: 's2', scope: 'pii_vault' }]);
- expect(await hasActiveConsent('s2', 'pii_vault')).toBe(true);
+ await expect(assertSubjectConsent('s2', { scope: 'pii_vault' })).resolves.toMatchObject({ id: 's2' });
for (const scope of ['broker_scan', 'broker_optout']) {
await expect(assertSubjectConsent('s2', { scope, action: 'x' }))
.rejects.toMatchObject({ status: 403, code: 'SUBJECT_CONSENT_REQUIRED' });
@@ -226,7 +226,6 @@ describe('consent gate — purpose-scoped (#8332)', () => {
it('refuses to run an unscoped gate — there is no "any consent" mode', async () => {
useConsentRows([{ subjectId: 's2', scope: 'pii_vault' }]);
await expect(assertSubjectConsent('s2', { action: 'scan' })).rejects.toMatchObject({ code: 'CONSENT_SCOPE_INVALID' });
- await expect(hasActiveConsent('s2')).rejects.toMatchObject({ code: 'CONSENT_SCOPE_INVALID' });
expect(queryMock).not.toHaveBeenCalled();
});
diff --git a/server/services/privacyVault.db.test.js b/server/services/privacyVault.db.test.js
index dd36686e9e..54ba1d12b0 100644
--- a/server/services/privacyVault.db.test.js
+++ b/server/services/privacyVault.db.test.js
@@ -81,9 +81,9 @@ describe.skipIf(!runDb)('privacy vault DB round-trip', () => {
});
it('the seeded self subject always has consent, so a record adds no duplicate (#3658)', async () => {
- // The boot DDL seeds a consent row for `self`, so the install owner is never
- // refused by the engine guard and an ordinary create appends nothing.
- expect(await subjects.hasActiveConsent(selfSubjectId, 'pii_vault')).toBe(true);
+ // The boot DDL seeds a local-vault (`pii_vault`) consent row for `self`, so
+ // an ordinary create appends nothing. Broker purposes are never seeded (#8332).
+ await expect(subjects.assertSubjectConsent(selfSubjectId, { scope: 'pii_vault' })).resolves.toMatchObject({ id: selfSubjectId });
const before = await query(
`SELECT COUNT(*)::int AS n FROM privacy_consents WHERE subject_id = $1`, [selfSubjectId],
);