fix(docsync): make the admin switch pause a binding, and retry a failed update

The check that was meant to stand a binding down when its provider is
switched off asked AddonsService.isAddonEnabled(link.provider_id). The
addons table has no rows for paperless, papra and the other document
providers, so it answered "off" for every binding, and every run came
back disabled before it listed anything. The unit test stubbed that
answer and the e2e never runs a sync, so nothing caught it.

isSwitchedOff() now asks the two things an admin actually switches: the
Documents addon, and document_providers.enabled for the binding's
provider. dueLinks leaves such bindings out in SQL, since one that
never runs never gets a last_sync_at and twenty of them would take
every slot of a tick. A run, the webhook on arrival and when its timer
fires, "Sync now" and the MCP sync tool all refuse before anything is
written, so a paused binding keeps its state and its documents' attempt
counters and resumes where it stopped. The REST route answers 409
provider_disabled, the MCP tool reports the same code, status() carries
providerOff per link, and both shells show one line saying the binding
is paused because its provider is switched off, in all 23 languages.

A failed pull_update also left the row stuck. upsertItem wrote the
listing's version, size and time next to the old bytes, so the next run
saw no upstream change, planned touch, which leaves error alone, and
the document stayed in error on stale bytes for good, "Sync now"
included. A failed transfer now leaves the pairing describing the copy
TREK holds; a new row keeps what pairs it but no version, so it is
fetched again.
This commit is contained in:
Maurice
2026-09-19 15:12:47 +02:00
parent 9de8490176
commit 233f46575c
39 changed files with 603 additions and 83 deletions
@@ -16,7 +16,6 @@ vi.mock('../../../../src/db/database', () => ({
import { DocSyncWebhookController } from '../../../../src/nest/doc-sync/doc-sync-webhook.controller';
import type { DocSyncConfigService, LinkRow } from '../../../../src/nest/doc-sync/doc-sync-config.service';
import type { DocSyncService } from '../../../../src/nest/doc-sync/doc-sync.service';
import type { AddonsService } from '../../../../src/nest/addons/addons.service';
import type { DatabaseService } from '../../../../src/nest/database/database.service';
/**
@@ -72,7 +71,6 @@ const config = {
};
const settings = new Map<string, string>();
const addons = { isAddonEnabled: vi.fn(() => true) };
const db = {
get: vi.fn((_sql: string, key?: unknown) => {
const value = settings.get(String(key));
@@ -82,12 +80,14 @@ const db = {
const sync = {
syncLink: vi.fn(async (_link: LinkRow) => ({ state: 'ok', pulled: 0, pushed: 0, conflicts: 0, missing: 0 })),
// The Documents addon and the binding's provider, as one answer. What goes
// into it is the service's business and tested there.
isSwitchedOff: vi.fn((_link: LinkRow) => false),
};
const controller = new DocSyncWebhookController(
config as unknown as DocSyncConfigService,
sync as unknown as DocSyncService,
addons as unknown as AddonsService,
db as unknown as DatabaseService,
);
@@ -119,7 +119,7 @@ beforeEach(() => {
vi.clearAllMocks();
vi.useFakeTimers();
settings.clear();
addons.isAddonEnabled.mockReturnValue(true);
sync.isSwitchedOff.mockReturnValue(false);
config.getLinkByToken.mockImplementation((token: string) => (token === 'tok-live' ? link() : undefined));
config.getLink.mockImplementation((id: number) => (id === 4 ? link() : undefined));
config.webhookSecret.mockReturnValue(SECRET);
@@ -339,15 +339,17 @@ describe('a burst of nudges', () => {
/**
* The switches an admin expects to mean "off".
*
* The scheduler obeys both the Documents addon and the app_settings kill
* switch; the webhook obeyed neither, so switching document sync off stopped
* the poll while every provider holding a webhook carried on driving full runs.
* The scheduler obeys the Documents addon, the binding's provider and the
* app_settings kill switch; the webhook obeyed none of them, so switching
* document sync off stopped the poll while every provider holding a webhook
* carried on driving full runs.
*/
describe('the admin switches', () => {
it('does nothing while the Documents addon is off', () => {
addons.isAddonEnabled.mockReturnValue(false);
it('does nothing while the addon or the provider of the binding is switched off', () => {
sync.isSwitchedOff.mockReturnValue(true);
expect(controller.nudge('tok-live', makeReq({ 'x-trek-docsync-secret': SECRET }))).toEqual({ received: true });
settle();
expect(sync.isSwitchedOff.mock.calls[0][0]).toMatchObject({ id: 4, provider_id: 'papra' });
expect(sync.syncLink).not.toHaveBeenCalled();
});
@@ -358,9 +360,9 @@ describe('the admin switches', () => {
expect(sync.syncLink).not.toHaveBeenCalled();
});
it('drops a scheduled run when the addon goes off inside the window', () => {
it('drops a scheduled run when the provider goes off inside the window', () => {
controller.nudge('tok-live', makeReq({ 'x-trek-docsync-secret': SECRET }));
addons.isAddonEnabled.mockReturnValue(false);
sync.isSwitchedOff.mockReturnValue(true);
settle();
expect(sync.syncLink).not.toHaveBeenCalled();
});