Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/37766-opening-lead.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@objectstack/plugin-approvals': patch
---

fix(approvals): the opening refusal no longer says the object closes on its approval field when its only gaps are the host's or an unreadable flow

Clause-②: yes (widening)
Metadata-protocol: none

Widened: the plugin's message catalog gains the sentence `approvalOpeningRefusedClosedStatesLead` in all 11 locale packs.

An approval's first request is refused when an object it can act on declares `closedStates` and the host runs no job service, or when a flow it runs cannot be read. Those gaps are raised whatever field the object closes on. The refusal's lead used to say "the object closes on its approval field" in every case, which is false for an object that closes on another field. Where every gap is one of those two, the lead now says only what is true: the approval can act on an object that declares `closedStates`. The sentences per gap are unchanged.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('[ADR-0162 item 7] the check when an approval opens', () => {
try { await kernel?.shutdown(); } catch { /* noop */ }
});

async function boot(opts: { sweeps?: boolean; invoiceDef?: Record<string, unknown> } = {}) {
async function boot(opts: { sweeps?: boolean; invoiceDef?: Record<string, unknown>; warns?: string[] } = {}) {
kernel = new ObjectKernel({ logger: { level: 'silent' } } as any);
await kernel.use(new ObjectQLPlugin());
await kernel.use(new AutomationServicePlugin());
Expand All @@ -118,7 +118,9 @@ describe('[ADR-0162 item 7] the check when an approval opens', () => {
await ql.insert('sys_member', { id: `mem_${id}`, user_id: id, organization_id: ORG, role: 'member' }, { context: SYS });
}
const quiet = { debug: () => {}, info: () => {}, warn: () => {}, error: () => {} };
svc = new ApprovalService({ engine: ql, logger: quiet as any });
const sink = opts.warns;
const serviceLogger = sink ? { ...quiet, warn: (message: string) => { sink.push(message); } } : quiet;
svc = new ApprovalService({ engine: ql, logger: serviceLogger as any });
if (opts.sweeps !== false) svc.noteSweepClockScheduled();
svc.attachAutomation(automation as any);
registerApprovalNode(automation as any, svc, quiet as any);
Expand Down Expand Up @@ -229,4 +231,30 @@ describe('[ADR-0162 item 7] the check when an approval opens', () => {
await seed('crm_memo', 'm_host');
expect((await start('two_step', 'crm_memo', 'm_host')).status).toBe('paused');
});

describe('the log line at a refusal says the object closes on its approval field only where a gap is about it', () => {
const FIELD_CLAIM = 'closes on its approval field';
const refusalWarns = (warns: string[]) => warns.filter((w) => w.includes('was refused at its first request'));

it('does not say it where the host\'s gap is the only one', async () => {
const warns: string[] = [];
await boot({ sweeps: false, warns });
await seed('crm_invoice', 'i_host_log');
expect((await start('two_step', 'crm_invoice', 'i_host_log')).status).toBe('failed');
const logged = refusalWarns(warns);
expect(logged).toHaveLength(1);
expect(logged[0]).not.toContain(FIELD_CLAIM);
expect(logged[0]).toContain('declares closedStates');
});

it('says it where a gap is about the field (control)', async () => {
const warns: string[] = [];
await boot({ warns });
await seed('crm_invoice', 'i_wait_log');
expect((await start('wait_between', 'crm_invoice', 'i_wait_log')).status).toBe('failed');
const logged = refusalWarns(warns);
expect(logged).toHaveLength(1);
expect(logged[0]).toContain(FIELD_CLAIM);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* [ADR-0162 Execution item 7] The lead of the refusal at an approval's first
* request says the object closes on its approval field only where a gap is
* about that field. The host's gap (`no_job_service`) and a flow that could
* not be read (`unreadable_flow`) are raised on an object that declares
* `closedStates` on any field, so where they are the only gaps the lead says
* what is true in every case: the approval can act on an object that declares
* `closedStates`.
*
* The expected sentences are read from the catalog, not written out here: what
* these tests hold is WHICH lead is chosen, and in which pack; the wording is
* the catalog's, and `approval-notification-keyed-text.integration.test.ts` holds every locale
* to the same ids and holes.
*/

import { describe, expect, it } from 'vitest';
import { interpolate } from './approval-notification-text.js';
import type { OpeningGap } from './approval-opening-check.js';
import { openingRefusalText } from './approval-opening-refusal.js';
import { approvalMessages } from './translations/messages.js';

const subject = { object: 'crm_deal', record: 'd1' };
type Locale = keyof typeof approvalMessages;
const fieldLead = (locale: Locale) => interpolate(approvalMessages[locale].approvalOpeningRefusedLead, subject);
const neutralLead = (locale: Locale) => interpolate(approvalMessages[locale].approvalOpeningRefusedClosedStatesLead, subject);
const sentence = (gap: OpeningGap, id: 'approvalOpeningNoJobService' | 'approvalOpeningUnreadableFlow') => {
const { kind: _kind, ...params } = gap;
return interpolate(approvalMessages.en[id], params as Record<string, string>);
};

const host: OpeningGap = { kind: 'no_job_service', object: 'crm_deal' };
const unreadable: OpeningGap = { kind: 'unreadable_flow', flow: 'deal_sub' };
const pause: OpeningGap = { kind: 'pause_between_steps', flow: 'deal_flow', node: 'hold', type: 'wait', object: 'crm_deal' };

describe('the opening refusal: its lead', () => {
it('the two leads are two sentences, in every pack', () => {
for (const locale of Object.keys(approvalMessages) as Locale[]) {
expect(neutralLead(locale), locale).not.toBe(fieldLead(locale));
}
});

it('does not say the object closes on its approval field where the host\'s gap is the only one', () => {
const text = openingRefusalText([host], subject, 'en');
expect(text).toBe(`${neutralLead('en')} ${sentence(host, 'approvalOpeningNoJobService')}`);
});

it('does not say it where a flow that could not be read is the only gap', () => {
const text = openingRefusalText([unreadable], subject, 'en');
expect(text).toBe(`${neutralLead('en')} ${sentence(unreadable, 'approvalOpeningUnreadableFlow')}`);
});

it('does not say it where the host\'s gap and an unreadable flow are the only gaps', () => {
const text = openingRefusalText([unreadable, host], subject, 'en');
expect(text.startsWith(`${neutralLead('en')} `)).toBe(true);
expect(text).not.toContain(fieldLead('en'));
});

it('says it where a gap is about the field, alone or beside the host\'s', () => {
expect(openingRefusalText([pause], subject, 'en').startsWith(`${fieldLead('en')} `)).toBe(true);
expect(openingRefusalText([pause, host], subject, 'en').startsWith(`${fieldLead('en')} `)).toBe(true);
expect(openingRefusalText([host, pause], subject, 'en').startsWith(`${fieldLead('en')} `)).toBe(true);
});

it('words the lead that says nothing about the field in the run\'s locale', () => {
const text = openingRefusalText([host], subject, 'zh-CN');
expect(text.startsWith(`${neutralLead('zh-CN')} `)).toBe(true);
expect(text).not.toContain(fieldLead('zh-CN'));
expect(neutralLead('zh-CN')).not.toBe(neutralLead('en'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,34 @@ export function openingRefusalPack(locale: string | undefined): Pack {
return same ? packs[same]! : approvalMessages.en;
}

/**
* The gaps that say nothing about the approval field: the host's, and a flow
* that could not be read. Where every gap is one of these, the lead does not
* say the object closes on its approval field, which may be false: an object
* can declare `closedStates` on another field.
*/
const NOT_ABOUT_THE_FIELD: ReadonlySet<OpeningGap['kind']> = new Set(['no_job_service', 'unreadable_flow']);

/**
* Whether any of `gaps` is about the approval field, so that "the object
* closes on its approval field" is true of the refusal. The one rule for every
* text that says so: the refusal's lead here, and the service's log line.
*/
export function gapsAreAboutTheField(gaps: readonly OpeningGap[]): boolean {
return gaps.some((gap) => !NOT_ABOUT_THE_FIELD.has(gap.kind));
}

/** The refusal text for `gaps`, on the record `subject`, in `locale`. */
export function openingRefusalText(
gaps: readonly OpeningGap[],
subject: { object: string; record: string },
locale?: string,
): string {
const pack = openingRefusalPack(locale);
const lead = interpolate(pack.approvalOpeningRefusedLead, { object: subject.object, record: subject.record });
const lead = interpolate(
gapsAreAboutTheField(gaps) ? pack.approvalOpeningRefusedLead : pack.approvalOpeningRefusedClosedStatesLead,
{ object: subject.object, record: subject.record },
);
const sentences = gaps.map((gap) => {
const { kind, ...params } = gap;
return interpolate(pack[GAP_SENTENCE[kind]], params as Record<string, string>);
Expand Down
6 changes: 4 additions & 2 deletions packages/plugins/plugin-approvals/src/approval-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ import { consolePageLink, type ConsolePageComposer } from './public-base-url.js'
import { APPROVAL_EMAIL_CHANNELS, EMAILED_APPROVAL_TOPICS } from './approval-email-templates.js';
import { enMessages } from './translations/messages.js';
import { closedValuesOf, judgeApprovalOpening, type OpeningFlow } from './approval-opening-check.js';
import { openingRefusalText } from './approval-opening-refusal.js';
import { gapsAreAboutTheField, openingRefusalText } from './approval-opening-refusal.js';
import {
approvalMessageKey,
approvalNotificationText,
Expand Down Expand Up @@ -5252,7 +5252,9 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
});
if (judgement.gaps.length > 0) {
const locale = input.locale ?? (context as { locale?: string } | undefined)?.locale;
this.logger?.warn?.('[approvals] an approval was refused at its first request: the record\'s object closes on its approval field and the approval cannot show it in progress (ADR-0162 D5)', {
this.logger?.warn?.(gapsAreAboutTheField(judgement.gaps)
? '[approvals] an approval was refused at its first request: the record\'s object closes on its approval field and the approval cannot show it in progress (ADR-0162 D5)'
: '[approvals] an approval was refused at its first request: it can act on an object that declares closedStates, and this host runs no job service or a flow it runs could not be read (ADR-0162 D5)', {
object: input.object, record: input.recordId, run: input.runId, rootRun: rootRun.runId,
gaps: judgement.gaps.map((g) => g.kind),
});
Expand Down
Loading
Loading