Mail relay: state block without an import-map entry, e2e on the reference site, sharper failure reports

- the state block is rendered by IntegrationStatusBanner (no new admin component: generate:importmap gives no diff)
- reference site: e2e/mail-relay.spec.ts with a fixture and setup script; e2e:built runs it with both bundlers
- a failed enrolment is reported as what it was; the service's own refusal after a fresh enrolment
- a message keeps its key after any failure that is not final
- a local site is shown as refused in the panel before any e-mail

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 21:14:47 +01:00
co-authored by Claude Fable 5.2
parent dd3792cbc7
commit 2cceceffd0
29 changed files with 1461 additions and 65 deletions
+54
View File
@@ -13,6 +13,7 @@ import { relayAdapter } from '../src/modules/email/relay/adapter.js'
import { withMailKind } from '../src/modules/email/relay/kinds.js'
import { bareAddresses, buildRelayMessage, cleanSubject } from '../src/modules/email/relay/message.js'
import { isPublicHost, relayHost, resolveRelayOptions } from '../src/modules/email/relay/options.js'
import { mailPanelState } from '../src/modules/email/relay/panel.js'
import { clearUnconfirmedRelayMessages, sendThroughRelay } from '../src/modules/email/relay/send.js'
import { disconnectRelay, enrolRelay, getRelayStatus, refreshRelayStatus, rotateRelayToken, setRelayToken } from '../src/modules/email/relay/session.js'
import { holdNonce, readRelayCandidate, readRelayStore, updateRelayStore } from '../src/modules/email/relay/store.js'
@@ -251,6 +252,17 @@ describe('when the service asks the site to enrol again', () => {
expect((await readRelayStore(payload)).token).not.toBe(old)
})
test('a service that keeps refusing the token: one enrolment for the message, not a loop', async () => {
const { fake, options, payload } = relaySite({ fake: { allow: [HOST] } })
await sendThroughRelay(payload, options, MESSAGE)
fake.intercept = (request) =>
request.url.endsWith('/v1/send') ? Response.json({ code: 'invalid-token', message: 'x', ok: false, retryable: false }, { status: 401 }) : undefined
const before = fake.requests.length
const result = await sendThroughRelay(payload, options, { ...MESSAGE, subject: 'Refused twice' })
expect(result).toMatchObject({ cause: 'invalid-token', code: 'relay-refused', sent: false })
expect(fake.requests.slice(before).filter((r) => r.path === '/v1/enrol')).toHaveLength(1)
})
test('a site enrols at most once per message, and a refused enrolment is not asked again for a while', async () => {
const { fake, options, payload } = relaySite({ fake: { allow: [HOST], callSite: async () => new Response(null, { status: 404 }) } })
const first = await sendThroughRelay(payload, options, MESSAGE)
@@ -266,6 +278,18 @@ describe('when the service asks the site to enrol again', () => {
expect(fake.requests.map((r) => r.path)).toEqual(['/v1/enrol', '/v1/enrol'])
})
test('the service cannot be reached at all: one enrolment is tried, nothing is sent, and the status says "unreachable"', async () => {
const { fake, options, payload } = relaySite({ fake: { allow: [HOST] } })
fake.intercept = () => 'drop'
const result = await sendThroughRelay(payload, options, MESSAGE)
// the message never left the site, so it is "not sent" (confirmed), not "may have been sent"
expect(result).toMatchObject({ cause: 'unavailable', code: 'relay-unreachable', confirmed: true, sent: false })
expect(fake.outbox).toHaveLength(0)
// not a refusal: the next message tries again at once
fake.intercept = undefined
expect((await sendThroughRelay(payload, options, MESSAGE)).sent).toBe(true)
})
test('the answer to /v1/enrol is lost: the delivered token is tried next and becomes the token once the service accepts it', async () => {
const { fake, options, payload } = relaySite({ fake: { allow: [HOST] } })
fake.intercept = async (request) => {
@@ -352,6 +376,16 @@ describe('one message: one key, one encoding, three attempts', () => {
const sends = capture(fake, [page(), page(), page()])
expect(await sendThroughRelay(payload, options, MESSAGE)).toMatchObject({ cause: 'unavailable', code: 'relay-unreachable', confirmed: true, sent: false })
expect(sends).toHaveLength(3)
// an error page says nothing about what the service did: the same message sent again keeps its key
const again = capture(fake, [])
expect((await sendThroughRelay(payload, options, MESSAGE)).sent).toBe(true)
expect(again[0].key).toBe(sends[0].key)
// a final refusal does not: the next try is a new message
const refused = capture(fake, [failing(429, { code: 'quota-exceeded', limit: 'hour', retryable: false })])
await sendThroughRelay(payload, options, { ...MESSAGE, subject: 'Refused' })
const after = capture(fake, [])
await sendThroughRelay(payload, options, { ...MESSAGE, subject: 'Refused' })
expect(after[0].key).not.toBe(refused[0].key)
// also a 200 whose body is not the contract's
const odd = capture(fake, [new Response('', { status: 200 }), Response.json(['ok']), Response.json({ ok: true }, { status: 201 })])
expect((await sendThroughRelay(payload, options, { ...MESSAGE, subject: 'Odd' })).sent).toBe(false)
@@ -562,6 +596,9 @@ describe('the token over time', () => {
expect(refusedByService.status).toBe(401)
const before = fake.requests.length
expect(await sendThroughRelay(payload, options, MESSAGE)).toMatchObject({ code: 'relay-disconnected', sent: false })
// nor does anything else enrol the site by itself (a script, a status read): only "Connect again" does
expect(await enrolRelay(payload, options)).toMatchObject({ ok: false, status: 'relay-disconnected' })
await refreshRelayStatus(payload, options)
expect(fake.requests.length).toBe(before)
expect((await enrolRelay(payload, options, { force: true })).ok).toBe(true)
expect((await sendThroughRelay(payload, options, MESSAGE)).sent).toBe(true)
@@ -588,6 +625,23 @@ describe('the token over time', () => {
})
})
describe('what the panel is told', () => {
test('a site that runs locally is shown as refused before any e-mail; a public one as "not connected yet"; SMTP and "off" say so', async () => {
const local = relayPayload({ serverURL: 'http://localhost:3000' })
expect(await mailPanelState(local, {})).toMatchObject({ relay: { connected: false, refusal: { code: 'host-not-allowed' } }, replyAddress: null, transport: 'relay' })
const nowhere = relayPayload({ serverURL: '' })
expect((await mailPanelState(nowhere, {})).relay?.refusal?.code).toBe('client-no-host')
const fresh = relayPayload({ integrations: { smtpFromAddress: ' [email protected] ' } })
const state = await mailPanelState(fresh, {})
expect(state).toMatchObject({ confirmUrl: 'https://example.com/api/iapl/mail-relay/confirm', relay: { connected: false, host: HOST, senderAddress: '[email protected]' }, replyAddress: '[email protected]', transport: 'relay' })
expect(state.relay?.refusal).toBeUndefined()
const smtp = relayPayload({ integrations: { smtpHost: 'smtp.example.com', smtpPassword: 'x', smtpUser: 'u' } })
expect((await mailPanelState(smtp, {})).transport).toBe('smtp')
expect(await mailPanelState(fresh, false)).toMatchObject({ relay: null, transport: 'none' })
expect(JSON.stringify([state])).not.toContain('ims_')
})
})
describe('the adapter on its own, and the deprecated Graph line', () => {
test('relayAdapter() is a whole e-mail adapter', async () => {
const { fake, payload } = relaySite({ fake: { allow: [HOST] }, integrations: { smtpFromName: 'Kancelaria Example' } })