fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s

Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
This commit is contained in:
2026-11-10 09:37:22 +01:00
parent 077251ac94
commit e7f0070e84
6135 changed files with 1718997 additions and 4 deletions
+185
View File
@@ -0,0 +1,185 @@
import { describe, expect, it } from 'vitest'
import {
checklistNumbers,
claudeConnectorLink,
mcpServerUrl,
sideDoorServerUrl,
SIDE_DOORS,
claudeStepDone,
completionPatchBody,
vatDeadlineLine,
} from '../checklist'
describe('vatDeadlineLine', () => {
it('returns null when the company is not VAT-registered', () => {
expect(
vatDeadlineLine({ vatRegistered: false, momsPeriod: 'quarterly', nextVatDueDate: '2026-11-12' })
).toBeNull()
expect(
vatDeadlineLine({ vatRegistered: null, momsPeriod: null, nextVatDueDate: null })
).toBeNull()
})
it('flags the silent zero-deadline misconfiguration when moms_period is unset', () => {
expect(
vatDeadlineLine({ vatRegistered: true, momsPeriod: null, nextVatDueDate: null })
).toEqual({ kind: 'missing_period' })
// Even with a stray row, an unset period is still a misconfiguration to surface.
expect(
vatDeadlineLine({ vatRegistered: true, momsPeriod: undefined, nextVatDueDate: '2026-11-12' })
).toEqual({ kind: 'missing_period' })
})
it('returns the due date when registered with a period and an upcoming row', () => {
expect(
vatDeadlineLine({ vatRegistered: true, momsPeriod: 'quarterly', nextVatDueDate: '2026-11-12' })
).toEqual({ kind: 'date', dueDate: '2026-11-12' })
})
it('says nothing when a period is set but no upcoming row surfaced', () => {
expect(
vatDeadlineLine({ vatRegistered: true, momsPeriod: 'yearly', nextVatDueDate: null })
).toBeNull()
})
})
describe('checklistNumbers', () => {
it('numbers all five steps when both extensions are on', () => {
expect(checklistNumbers({ hasSkatteverket: true, hasInbox: true, hasAssistant: true })).toEqual({
count: 5,
skv: 3,
receipts: 4,
assistant: 5,
})
})
it('collapses to four steps without the inbox extension', () => {
expect(checklistNumbers({ hasSkatteverket: true, hasInbox: false, hasAssistant: true })).toEqual({
count: 4,
skv: 3,
receipts: 4,
assistant: 4,
})
})
it('collapses to four steps without the skatteverket extension', () => {
expect(checklistNumbers({ hasSkatteverket: false, hasInbox: true, hasAssistant: true })).toEqual({
count: 4,
skv: 3,
receipts: 3,
assistant: 4,
})
})
it('collapses to three steps with neither extension', () => {
expect(checklistNumbers({ hasSkatteverket: false, hasInbox: false, hasAssistant: true })).toEqual({
count: 3,
skv: 3,
receipts: 3,
assistant: 3,
})
})
// A deployment that cannot run the assistant (#2204) drops the Claude step:
// the title counts one step fewer, the other ordinals stay put.
it('drops the assistant step from the count without moving the other ordinals', () => {
expect(checklistNumbers({ hasSkatteverket: true, hasInbox: true, hasAssistant: false })).toEqual({
count: 4,
skv: 3,
receipts: 4,
assistant: 5,
})
expect(checklistNumbers({ hasSkatteverket: false, hasInbox: false, hasAssistant: false })).toEqual({
count: 2,
skv: 3,
receipts: 3,
assistant: 3,
})
})
})
describe('completionPatchBody', () => {
it('keeps a recorded path out of the body', () => {
expect(completionPatchBody('bank')).toEqual({ completed: true })
expect(completionPatchBody('fresh')).toEqual({ completed: true })
})
it('records migration when no path was chosen, so the route accepts completion', () => {
// Skipped the books question, then imported: step 1 is only done via an
// import in that state. Without a path the route answers 400 and the
// completion effect used to retry it forever.
expect(completionPatchBody(null)).toEqual({ completed: true, path: 'migration' })
})
})
describe('claudeStepDone', () => {
it('is done once an OAuth-minted MCP key row exists', () => {
expect(claudeStepDone({ oauthKeyCount: 1 })).toBe(true)
expect(claudeStepDone({ oauthKeyCount: 3 })).toBe(true)
})
it('stays open without a key row, including a null head count', () => {
expect(claudeStepDone({ oauthKeyCount: 0 })).toBe(false)
expect(claudeStepDone({ oauthKeyCount: null })).toBe(false)
expect(claudeStepDone({ oauthKeyCount: undefined })).toBe(false)
})
})
describe('mcpServerUrl', () => {
it('builds the namespaced URL with the client marker and no auth flag by default', () => {
expect(mcpServerUrl({ origin: 'https://app.testbrand.example', client: 'cursor' })).toBe(
'https://app.testbrand.example/api/extensions/ext/mcp-server/mcp?tool_namespace=accounted&client=cursor',
)
})
it('appends auth=required when eagerAuth is set', () => {
const url = new URL(mcpServerUrl({ origin: 'http://localhost:3000', client: 'grok', eagerAuth: true }))
expect(url.searchParams.get('tool_namespace')).toBe('accounted')
expect(url.searchParams.get('client')).toBe('grok')
expect(url.searchParams.get('auth')).toBe('required')
})
})
describe('sideDoorServerUrl', () => {
it('lists chatgpt then grok', () => {
expect(SIDE_DOORS).toEqual(['chatgpt', 'grok'])
})
it('gives Grok the eager-auth flag: its dialog reads a 200 probe as "no auth" and never starts OAuth', () => {
const url = new URL(sideDoorServerUrl({ origin: 'https://app.testbrand.example', door: 'grok' }))
expect(url.searchParams.get('client')).toBe('grok')
expect(url.searchParams.get('auth')).toBe('required')
})
it('keeps ChatGPT on the lazy URL', () => {
const url = new URL(sideDoorServerUrl({ origin: 'https://app.testbrand.example', door: 'chatgpt' }))
expect(url.searchParams.get('client')).toBe('chatgpt')
expect(url.searchParams.get('auth')).toBeNull()
})
})
describe('claudeConnectorLink', () => {
it('builds the claude.ai deep link with namespace, client marker and eager-auth flag, from the page origin', () => {
const link = claudeConnectorLink({ origin: 'https://app.testbrand.example', appName: 'Testbrand' })
expect(link).toBe(
'https://claude.ai/customize/connectors?modal=add-custom-connector' +
'&connectorName=Testbrand' +
'&connectorUrl=https%3A%2F%2Fapp.testbrand.example%2Fapi%2Fextensions%2Fext%2Fmcp-server%2Fmcp%3Ftool_namespace%3Daccounted%26client%3Dclaude-connector%26auth%3Drequired',
)
})
it('matches the Settings → API & MCP button shape (tool_namespace + client=claude-connector + auth=required)', () => {
const link = claudeConnectorLink({ origin: 'http://localhost:3000', appName: 'Bokföring AB' })
const url = new URL(link)
expect(url.searchParams.get('connectorName')).toBe('Bokföring AB')
const server = new URL(url.searchParams.get('connectorUrl') ?? '')
expect(server.origin).toBe('http://localhost:3000')
expect(server.pathname).toBe('/api/extensions/ext/mcp-server/mcp')
expect(server.searchParams.get('tool_namespace')).toBe('accounted')
expect(server.searchParams.get('client')).toBe('claude-connector')
// Without this flag claude.ai's Add-custom-connector dialog pre-fills
// Authentication "None" (the lazy handshake answers 200) and the sign-in
// never opens.
expect(server.searchParams.get('auth')).toBe('required')
})
})
+137
View File
@@ -0,0 +1,137 @@
import type { InitialSetupPath, MomsPeriod } from '@/types'
/**
* What the Skatteverket checklist step should say about VAT deadlines.
*
* - 'date': the company's next momsdeklaration due date is known; show it.
* - 'missing_period': the company is VAT-registered but moms_period is unset,
* which makes the deadline engine silently generate ZERO VAT deadlines
* (lib/tax/deadline-config.ts conditions all require a concrete period).
* An empty deadlines query in that state means misconfiguration, not
* "no VAT duty", so the UI must prompt for the period instead of showing
* nothing.
* - null: not VAT-registered (no line), or VAT-registered with a period set
* but no upcoming row surfaced (transient or horizon gap; say nothing
* rather than guessing).
*/
export type VatDeadlineLine =
| { kind: 'date'; dueDate: string }
| { kind: 'missing_period' }
| null
export function vatDeadlineLine(input: {
vatRegistered: boolean | null | undefined
momsPeriod: MomsPeriod | null | undefined
nextVatDueDate: string | null | undefined
}): VatDeadlineLine {
if (!input.vatRegistered) return null
if (!input.momsPeriod) return { kind: 'missing_period' }
if (!input.nextVatDueDate) return null
return { kind: 'date', dueDate: input.nextVatDueDate }
}
/**
* Display ordinals for the setup checklist steps. Books and bank are always
* present; Skatteverket and the receipts/inbox step render only when their
* extensions are enabled; the assistant (Claude) step renders only where the
* deployment can run the assistant and, when it does, is last. `count` drives
* the "{count} steg så är bokföringen igång" title.
*/
export function checklistNumbers(gates: {
hasSkatteverket: boolean
hasInbox: boolean
hasAssistant: boolean
}): {
count: number
skv: number
receipts: number
assistant: number
} {
const skv = 3
const receipts = 3 + (gates.hasSkatteverket ? 1 : 0)
const assistant = receipts + (gates.hasInbox ? 1 : 0)
// Without the assistant step the thread ends one step earlier; the other
// ordinals do not move.
const count = gates.hasAssistant ? assistant : assistant - 1
return { count, skv, receipts, assistant }
}
/**
* Body of the PATCH that retires the checklist once every step is done.
* The route refuses `completed: true` without a path ("Välj först hur du
* vill komma igång"). `path` is null when the journey's books question was
* skipped and the books then arrived through /import or MCP; in that state
* step 1 can only be done via an import, so `migration` is the truthful path
* to record. Without it that cohort looped on a 400 (PR #2147 skeptic).
*/
export function completionPatchBody(
path: InitialSetupPath | null,
): { completed: true; path?: InitialSetupPath } {
return path ? { completed: true } : { completed: true, path: 'migration' }
}
/**
* Done-state for the "Anslut till Claude" step. The only thing that means
* "connected" is a live API key minted by the MCP OAuth token route: it
* exists exactly when a client (claude.ai, Claude Desktop, Claude Code)
* completed the first-call sign-in. `oauthKeyCount` is the head count of
* that user's unrevoked rows named by OAUTH_MCP_KEY_NAME (lib/auth/api-keys).
* Before issue #2133 the step ticked on the in-app AI-profile flag, which
* has nothing to do with Claude; the step could show done for a user who
* never connected and stay open for one who had.
*/
export function claudeStepDone(input: { oauthKeyCount: number | null | undefined }): boolean {
return (input.oauthKeyCount ?? 0) > 0
}
/**
* The MCP server URL we hand to a client. `tool_namespace` is load-bearing
* (without it the server hands out legacy `gnubok_` tool names), `client`
* is a telemetry-only distribution marker, and the origin comes from the
* page so self-hosted and white-label domains link to themselves.
*
* `eagerAuth` appends `auth=required` (extensions/general/mcp-server/
* auth-mode.ts). Needed for clients whose Add-connector dialog probes the
* URL without credentials and reads the lazy 200 as "no authentication":
* claude.ai pre-fills "None" and Grok lists every tool without ever opening
* the sign-in. The 401 challenge is the only answer those dialogs read as
* OAuth. ChatGPT developer mode, Claude Code, Cursor and the stdio bridge
* keep the lazy URL.
*/
export function mcpServerUrl(input: { origin: string; client: string; eagerAuth?: boolean }): string {
const eager = input.eagerAuth ? '&auth=required' : ''
return `${input.origin}/api/extensions/ext/mcp-server/mcp?tool_namespace=accounted&client=${input.client}${eager}`
}
/**
* Clients that get a collapsed "Using X?" side door under the checklist's
* Claude step. Each value keys the i18n strings step_claude_<door>_link /
* _steps and the telemetry step name. Order is display order.
*/
export const SIDE_DOORS = ['chatgpt', 'grok'] as const
export type SideDoor = (typeof SIDE_DOORS)[number]
/**
* The URL a side door copies. Grok's connector dialog behaves like
* claude.ai's (a 200 probe means "no auth", so the OAuth flow never starts)
* and needs the eager flag; ChatGPT's developer mode honours the lazy 401
* on the first protected call and keeps the plain URL.
*/
export function sideDoorServerUrl(input: { origin: string; door: SideDoor }): string {
return mcpServerUrl({ origin: input.origin, client: input.door, eagerAuth: input.door === 'grok' })
}
/**
* The claude.ai Add-custom-connector deep link the checklist's Claude step
* opens. Same shape as the Settings → API & MCP button (see mcpServerUrl for
* the query parameters). The link only prefills the dialog; the user reviews
* there.
*/
export function claudeConnectorLink(input: { origin: string; appName: string }): string {
const serverUrl = mcpServerUrl({ origin: input.origin, client: 'claude-connector', eagerAuth: true })
return (
'https://claude.ai/customize/connectors?modal=add-custom-connector' +
`&connectorName=${encodeURIComponent(input.appName)}` +
`&connectorUrl=${encodeURIComponent(serverUrl)}`
)
}