fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Å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:
@@ -0,0 +1,36 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { SANDBOX_AGENT_NAME, SANDBOX_PROFILE_SUMMARY } from '../ensure-agent'
|
||||
|
||||
/**
|
||||
* The sandbox assistant is called "Assistenten", not a first name: nobody in
|
||||
* the sandbox chose a persona, so a first name implies a relationship the
|
||||
* visitor never opted into.
|
||||
*
|
||||
* The pairing is the part worth pinning. profile_summary is the agent's own
|
||||
* self-description inside the system prompt, so if it drifts from the display
|
||||
* name the header says one thing and the assistant introduces itself as
|
||||
* another in its first sentence: a mismatch nothing else in the stack checks.
|
||||
*/
|
||||
describe('sandbox agent persona', () => {
|
||||
it('is named Assistenten', () => {
|
||||
expect(SANDBOX_AGENT_NAME).toBe('Assistenten')
|
||||
})
|
||||
|
||||
it('introduces itself by the same name the UI shows', () => {
|
||||
expect(SANDBOX_PROFILE_SUMMARY).toContain(`Du är ${SANDBOX_AGENT_NAME}`)
|
||||
})
|
||||
|
||||
it('does not still call itself Anna anywhere in the persona', () => {
|
||||
expect(SANDBOX_AGENT_NAME).not.toContain('Anna')
|
||||
expect(SANDBOX_PROFILE_SUMMARY).not.toContain('Anna')
|
||||
})
|
||||
|
||||
it('keeps the demo company description the persona depends on', () => {
|
||||
// The sandbox data is a Stockholm IT consultancy on the cash method with
|
||||
// quarterly VAT. The summary is what makes the assistant's answers match
|
||||
// the seeded books, so a rename must not quietly drop it.
|
||||
expect(SANDBOX_PROFILE_SUMMARY).toContain('enskild firma')
|
||||
expect(SANDBOX_PROFILE_SUMMARY).toContain('kontantmetoden')
|
||||
expect(SANDBOX_PROFILE_SUMMARY).toContain('momsregistrerat')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,59 @@
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { isSandboxCompany, sandboxBlockedResponse, guardSandbox } from '../guard'
|
||||
|
||||
function mockSupabase(isSandboxValue: boolean | null) {
|
||||
const maybeSingle = vi.fn().mockResolvedValue({
|
||||
data: isSandboxValue === null ? null : { is_sandbox: isSandboxValue },
|
||||
})
|
||||
const eq = vi.fn(() => ({ maybeSingle }))
|
||||
const select = vi.fn(() => ({ eq }))
|
||||
const from = vi.fn(() => ({ select }))
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return { from } as any
|
||||
}
|
||||
|
||||
describe('guardSandbox', () => {
|
||||
it('returns null for non-sandbox companies so the route proceeds', async () => {
|
||||
const supabase = mockSupabase(false)
|
||||
const result = await guardSandbox(supabase, '00000000-0000-0000-0000-000000000001')
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null when no company_settings row exists', async () => {
|
||||
const supabase = mockSupabase(null)
|
||||
const result = await guardSandbox(supabase, '00000000-0000-0000-0000-000000000001')
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it('returns the 403 NextResponse for sandbox companies', async () => {
|
||||
const supabase = mockSupabase(true)
|
||||
const result = await guardSandbox(supabase, '00000000-0000-0000-0000-000000000001')
|
||||
expect(result).not.toBeNull()
|
||||
expect(result!.status).toBe(403)
|
||||
const body = await result!.json()
|
||||
expect(body.sandbox_blocked).toBe(true)
|
||||
expect(body.error).toMatch(/sandlådan/i)
|
||||
expect(body.error_en).toMatch(/sandbox/i)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isSandboxCompany', () => {
|
||||
it('returns false when is_sandbox is missing', async () => {
|
||||
const supabase = mockSupabase(null)
|
||||
expect(await isSandboxCompany(supabase, 'cid')).toBe(false)
|
||||
})
|
||||
|
||||
it('returns true only when is_sandbox is exactly true', async () => {
|
||||
expect(await isSandboxCompany(mockSupabase(true), 'cid')).toBe(true)
|
||||
expect(await isSandboxCompany(mockSupabase(false), 'cid')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('sandboxBlockedResponse', () => {
|
||||
it('returns a 403 with sandbox_blocked envelope', async () => {
|
||||
const res = sandboxBlockedResponse()
|
||||
expect(res.status).toBe(403)
|
||||
const body = await res.json()
|
||||
expect(body.sandbox_blocked).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,80 @@
|
||||
import type { SupabaseClient } from '@supabase/supabase-js'
|
||||
import { createLogger } from '@/lib/logger'
|
||||
|
||||
const log = createLogger('sandbox:ensure-agent')
|
||||
|
||||
/**
|
||||
* The sandbox assistant is called "Assistenten", not a first name.
|
||||
*
|
||||
* A named persona is right once someone has been through onboarding and chosen
|
||||
* it: it is their assistant, and they named it. In the sandbox nobody chose
|
||||
* anything, so a first name reads as a character the product invented and
|
||||
* implies a relationship the visitor has not opted into. "Assistenten" says
|
||||
* what it is.
|
||||
*
|
||||
* The summary is the agent's own self-description in the system prompt, so it
|
||||
* has to agree with the display name: otherwise the header says one thing and
|
||||
* the assistant introduces itself as another in its first sentence.
|
||||
*/
|
||||
export const SANDBOX_AGENT_NAME = 'Assistenten'
|
||||
|
||||
export const SANDBOX_PROFILE_SUMMARY =
|
||||
'Du är Assistenten, en revisorsassistent för en svensk enskild firma som tillhandahåller IT-konsulttjänster i Stockholm. Företaget är momsregistrerat (kvartalsvis), använder kontantmetoden och fakturerar både svenska och utländska kunder.'
|
||||
|
||||
/**
|
||||
* Backfill a verified agent_profile for sandbox companies. Single source of
|
||||
* truth for the sandbox assistant's persona (name, avatar, atoms, summary):
|
||||
* the seed route, dashboard layout, dashboard page, and chat layout all call
|
||||
* through here so the profile data lives in exactly one place.
|
||||
*
|
||||
* `verified_by_user_id` is intentionally NULL: the row is synthetic seed
|
||||
* data, not a real user-driven verification. Attributing it to the calling
|
||||
* user would pollute the audit trail (and conflate consent on the GDPR
|
||||
* Art. 25(2) privacy-by-default surface).
|
||||
*
|
||||
* Best-effort: any error is logged and swallowed so the caller continues.
|
||||
* Worst case the user sees the pre-seed UI on this request; the next
|
||||
* request retries.
|
||||
*
|
||||
* Idempotent: the UNIQUE constraint on company_id makes the insert a no-op
|
||||
* once a profile exists.
|
||||
*/
|
||||
export async function ensureSandboxAgentProfile(
|
||||
supabase: SupabaseClient,
|
||||
companyId: string,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const { data: existing } = await supabase
|
||||
.from('agent_profiles')
|
||||
.select('id')
|
||||
.eq('company_id', companyId)
|
||||
.maybeSingle()
|
||||
if (existing) return
|
||||
|
||||
const { error } = await supabase.from('agent_profiles').insert({
|
||||
company_id: companyId,
|
||||
display_name: SANDBOX_AGENT_NAME,
|
||||
avatar_id: 'notionists-3',
|
||||
horizontal_atoms: [
|
||||
'horizontal/swedish-vat',
|
||||
'horizontal/swedish-accounting-compliance',
|
||||
],
|
||||
vertical_atoms: ['vertical/consulting'],
|
||||
modifier_atoms: [],
|
||||
profile_summary: SANDBOX_PROFILE_SUMMARY,
|
||||
source_signals: { is_sandbox: true },
|
||||
field_overrides: {},
|
||||
composer_model: 'sandbox-demo',
|
||||
composer_version: 1,
|
||||
composed_at: new Date().toISOString(),
|
||||
verified_at: new Date().toISOString(),
|
||||
verified_by_user_id: null,
|
||||
intake_completed_at: new Date().toISOString(),
|
||||
})
|
||||
if (error) {
|
||||
log.warn('failed to backfill sandbox agent_profile', { error, companyId })
|
||||
}
|
||||
} catch (err) {
|
||||
log.warn('unexpected error backfilling sandbox agent_profile', { error: err, companyId })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import type { SupabaseClient } from '@supabase/supabase-js'
|
||||
import { NextResponse } from 'next/server'
|
||||
|
||||
/**
|
||||
* Sandbox guard: returns true if the given company is a sandbox company
|
||||
* (`company_settings.is_sandbox = true`). Used to short-circuit API routes
|
||||
* that would otherwise call paid external services (Anthropic Bedrock, the
|
||||
* Resend email API, Riksbanken FX, VIES, Skatteverket, Enable Banking, TIC).
|
||||
*
|
||||
* The sandbox is intentionally read-only against external systems: it must
|
||||
* never send a real email, charge a token, or speak to a tax authority on
|
||||
* behalf of an anonymous demo user. RLS and the `is_sandbox` flag on
|
||||
* company_settings are the single source of truth: we check it here on
|
||||
* every gated entry point so the demo can't accidentally outgrow its sandbox.
|
||||
*/
|
||||
export async function isSandboxCompany(
|
||||
supabase: SupabaseClient,
|
||||
companyId: string,
|
||||
): Promise<boolean> {
|
||||
const { data } = await supabase
|
||||
.from('company_settings')
|
||||
.select('is_sandbox')
|
||||
.eq('company_id', companyId)
|
||||
.maybeSingle()
|
||||
return data?.is_sandbox === true
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard 403 response for sandbox-blocked endpoints. The bilingual envelope
|
||||
* matches the rest of the app's error shape: the UI picks the right field
|
||||
* via the active locale.
|
||||
*/
|
||||
export function sandboxBlockedResponse(): NextResponse {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Inte tillgängligt i sandlådan. Skapa ett konto för att använda AI-assistenten och externa tjänster.',
|
||||
error_en: 'Not available in the sandbox. Create an account to use the AI assistant and external services.',
|
||||
sandbox_blocked: true,
|
||||
},
|
||||
{ status: 403 },
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience wrapper: check + return the 403 in one call. Returns the
|
||||
* NextResponse to return from the route, or `null` when the company is not
|
||||
* a sandbox and the route should proceed.
|
||||
*
|
||||
* const blocked = await guardSandbox(supabase, companyId)
|
||||
* if (blocked) return blocked
|
||||
*/
|
||||
export async function guardSandbox(
|
||||
supabase: SupabaseClient,
|
||||
companyId: string,
|
||||
): Promise<NextResponse | null> {
|
||||
if (await isSandboxCompany(supabase, companyId)) {
|
||||
return sandboxBlockedResponse()
|
||||
}
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user