feat(document-workbench): wire customer upload, structuring, credits and readiness
CI (SIAX Cloud) / integration (push) Skipped
CI (SIAX Cloud) / sonar (push) Skipped
masterplan-lock / check (push) Successful in 10s
CI (SIAX Cloud) / sonar (pull_request) Skipped
Security Scan (SIAX Cloud) / security (push) Successful in 13s
masterplan-lock / check (pull_request) Successful in 7s
CI (SIAX Cloud) / security (push) Successful in 26s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 17s
CI (SIAX Cloud) / security (pull_request) Successful in 19s
CI (SIAX Cloud) / quality (push) Successful in 1m9s
CI (SIAX Cloud) / quality (pull_request) Successful in 2m26s
CI (SIAX Cloud) / integration (pull_request) Successful in 2m33s
CI (SIAX Cloud) / integration (push) Skipped
CI (SIAX Cloud) / sonar (push) Skipped
masterplan-lock / check (push) Successful in 10s
CI (SIAX Cloud) / sonar (pull_request) Skipped
Security Scan (SIAX Cloud) / security (push) Successful in 13s
masterplan-lock / check (pull_request) Successful in 7s
CI (SIAX Cloud) / security (push) Successful in 26s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 17s
CI (SIAX Cloud) / security (pull_request) Successful in 19s
CI (SIAX Cloud) / quality (push) Successful in 1m9s
CI (SIAX Cloud) / quality (pull_request) Successful in 2m26s
CI (SIAX Cloud) / integration (pull_request) Successful in 2m33s
- POST /api/0pt/documents/upload: real multipart bytes into DOC0 through the durable attempt journal and the existing coordinator; verified tenant/actor only, project authorization fail-closed. - POST /api/0pt/documents/structure: DOC0 segments -> INF0 -> evidence-bound validation -> optional JSON/CSV export, with credit reserve/settle. - Credits: balance, server-owned price book, Stripe Checkout and idempotent webhook reconciliation via a Stripe re-read of session + payment intent. - /portal/dokument and /portal/krediter: SIAX-styled workspace. - @siax/access: universal, framework-neutral OpenFGA project relationship primitive reusable by DOC0/N0D/other products. - Readiness: real Redis PING probe instead of presence-only; queue is now a live dependency when configured. - Dockerfile: copy every workspace manifest so --frozen-lockfile works. - Format/build hygiene: .prettierignore, .worktrees ignored, valid manifest YAML.
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { Doc0SourceClient, Doc0SourceUnavailableError } from '../../src/document-workbench/doc0-source-client';
|
||||
|
||||
const hash = (value: string) => createHash('sha256').update(value).digest('hex');
|
||||
const receipt = { documentId: 'doc-a', versionId: 'version-a', checksumSha256: 'a'.repeat(64), pipelineStage: 'parsed' };
|
||||
const input = { tenantId: 'tenant-a', projectId: 'project-a', actorId: 'user-a', requestId: 'request-a', receipt };
|
||||
const text = 'Acme 😀';
|
||||
const metadata = { tenantId: input.tenantId, documentId: receipt.documentId, versionId: receipt.versionId,
|
||||
documentTextId: 'text-a', parserRunId: 'run-a', segmentsTruncated: false, segmentCount: 1, charCount: text.length, textSha256: hash(text) };
|
||||
const segment = { tenantId: input.tenantId, documentId: receipt.documentId, versionId: receipt.versionId,
|
||||
documentTextId: 'text-a', parserRunId: 'run-a', segmentId: 'segment-a', sequence: 0, text, charStart: 0,
|
||||
charEnd: text.length, segmentSha256: hash(text), sourceVersionSha256: receipt.checksumSha256 };
|
||||
const payload = { offset: 0, texts: [metadata], segments: [segment] };
|
||||
|
||||
describe('DOC0 extracted source client', () => {
|
||||
it('reads scoped, checksum-bound segment text without caching or redirects', async () => {
|
||||
const fetchImpl = vi.fn(async (url: string | URL | Request, init?: RequestInit) => {
|
||||
expect(String(url)).toContain('/v1/doc0/documents/doc-a/versions/version-a/segments?limit=100&offset=0');
|
||||
expect(new Headers(init?.headers).get('x-tenant-id')).toBe('tenant-a');
|
||||
expect(new Headers(init?.headers).get('x-actor-id')).toBe('user-a');
|
||||
expect(init?.redirect).toBe('error');
|
||||
expect(init?.cache).toBe('no-store');
|
||||
return Response.json(payload);
|
||||
});
|
||||
const client = new Doc0SourceClient({ baseUrl: 'https://doc0.test', serviceToken: 'test', fetchImpl });
|
||||
expect(await client.read(input)).toEqual({ sources: [{ id: 'segment-a', text }], reportedTextSha256: hash(text) });
|
||||
});
|
||||
it('rejects incomplete, forged, cross-tenant or inconsistent text instead of passing it to a model', async () => {
|
||||
const bad = [
|
||||
{ ...payload, texts: [{ ...metadata, segmentsTruncated: true }] },
|
||||
{ ...payload, texts: [{ ...metadata, segmentCount: 2 }] },
|
||||
{ ...payload, texts: [{ ...metadata, tenantId: 'tenant-b' }] },
|
||||
{ ...payload, texts: [metadata, metadata] },
|
||||
{ ...payload, segments: [{ ...segment, sourceVersionSha256: 'b'.repeat(64) }] },
|
||||
{ ...payload, segments: [{ ...segment, text: 'fabricated' }] },
|
||||
{ ...payload, segments: [{ ...segment, tenantId: 'tenant-b' }] },
|
||||
{ ...payload, segments: [{ ...segment, sequence: 1 }] },
|
||||
{ ...payload, segments: [{ ...segment, charEnd: 5 }] },
|
||||
{ ...payload, offset: 1 },
|
||||
];
|
||||
for (const candidate of bad) {
|
||||
const client = new Doc0SourceClient({ baseUrl: 'https://doc0.test', serviceToken: 'test', fetchImpl: async () => Response.json(candidate) });
|
||||
await expect(client.read(input)).rejects.toBeInstanceOf(Doc0SourceUnavailableError);
|
||||
}
|
||||
});
|
||||
it('rejects empty, oversized, unavailable and unparsed sources', async () => {
|
||||
const fetchImpl = vi.fn(async () => Response.json(payload));
|
||||
const client = new Doc0SourceClient({ baseUrl: 'https://doc0.test', serviceToken: 'test', fetchImpl });
|
||||
await expect(client.read({ ...input, receipt: { ...receipt, pipelineStage: 'parse_failed' } })).rejects.toBeInstanceOf(Doc0SourceUnavailableError);
|
||||
expect(fetchImpl).not.toHaveBeenCalled();
|
||||
for (const response of [Response.json({ ...payload, texts: [] }), new Response('x'.repeat(1024 * 1024 + 1)), new Response('', { status: 503 })]) {
|
||||
const bad = new Doc0SourceClient({ baseUrl: 'https://doc0.test', serviceToken: 'test', fetchImpl: async () => response });
|
||||
await expect(bad.read(input)).rejects.toBeInstanceOf(Doc0SourceUnavailableError);
|
||||
}
|
||||
});
|
||||
it('pins the original scope and validated credential during an in-flight read', async () => {
|
||||
const mutable = structuredClone(input);
|
||||
const config = { baseUrl: 'https://doc0.test', serviceToken: 'original', fetchImpl: async (_url: string | URL | Request, init?: RequestInit) => {
|
||||
expect(new Headers(init?.headers).get('authorization')).toBe('Bearer original');
|
||||
mutable.tenantId = 'tenant-b';
|
||||
mutable.receipt.checksumSha256 = 'b'.repeat(64);
|
||||
return Response.json(payload);
|
||||
} };
|
||||
const client = new Doc0SourceClient(config);
|
||||
config.serviceToken = 'changed';
|
||||
expect(await client.read(mutable)).toMatchObject({ sources: [{ id: 'segment-a', text }] });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,49 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
creditsForUsage,
|
||||
findPack,
|
||||
parseCreditPriceBook,
|
||||
readCreditPriceBook,
|
||||
} from '../../src/document-workbench/price-book';
|
||||
|
||||
const valid = {
|
||||
version: '2026-11-01',
|
||||
currency: 'SEK',
|
||||
packs: [
|
||||
{ id: 'starter', credits: 100, amountMinor: 9900 },
|
||||
{ id: 'pro', credits: 2000, amountMinor: 129900 },
|
||||
],
|
||||
metering: { pagesPerCredit: 5, inputTokensPerCredit: 20000, outputTokensPerCredit: 4000 },
|
||||
};
|
||||
|
||||
describe('credit price book', () => {
|
||||
it('parses a valid server-owned price book', () => {
|
||||
const book = parseCreditPriceBook(valid);
|
||||
expect(book.packs).toHaveLength(2);
|
||||
expect(findPack(book, 'pro')?.credits).toBe(2000);
|
||||
});
|
||||
|
||||
it('fails closed when the env price book is absent or invalid', () => {
|
||||
expect(readCreditPriceBook({} as unknown as NodeJS.ProcessEnv)).toBeUndefined();
|
||||
expect(readCreditPriceBook({ OPT_CREDIT_PRICE_BOOK: '{not json' } as unknown as NodeJS.ProcessEnv)).toBeUndefined();
|
||||
expect(
|
||||
readCreditPriceBook({
|
||||
OPT_CREDIT_PRICE_BOOK: JSON.stringify({ ...valid, currency: 'sek' }),
|
||||
} as unknown as NodeJS.ProcessEnv),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('rejects duplicate pack ids, unknown currencies and non-integer money', () => {
|
||||
expect(() => parseCreditPriceBook({ ...valid, packs: [valid.packs[0], valid.packs[0]] })).toThrow();
|
||||
expect(() =>
|
||||
parseCreditPriceBook({ ...valid, packs: [{ id: 'x', credits: 1, amountMinor: 1.5 }] }),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it('rounds every metered component up to its billing block', () => {
|
||||
const book = parseCreditPriceBook(valid);
|
||||
expect(creditsForUsage(book, { pages: 0, inputTokens: 0, outputTokens: 0 })).toBe(0);
|
||||
expect(creditsForUsage(book, { pages: 1, inputTokens: 1, outputTokens: 1 })).toBe(3);
|
||||
expect(creditsForUsage(book, { pages: 6, inputTokens: 40001, outputTokens: 8001 })).toBe(8);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,116 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type Stripe from 'stripe';
|
||||
import {
|
||||
StripeCreditVerificationError,
|
||||
verifyStripeCreditPurchase,
|
||||
type StripeCheckoutReader,
|
||||
} from '../../src/document-workbench/stripe-credit-verification';
|
||||
|
||||
const snapshot = {
|
||||
tenantId: 't_0pt',
|
||||
purchaseId: 'purchase-1',
|
||||
checkoutSessionId: 'cs_test_1',
|
||||
amountMinor: 9900,
|
||||
currency: 'SEK',
|
||||
credits: 100,
|
||||
};
|
||||
|
||||
function stripeReturning(session: unknown): StripeCheckoutReader {
|
||||
return {
|
||||
checkout: {
|
||||
sessions: {
|
||||
retrieve: async () => session as Stripe.Checkout.Session,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function session(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
id: 'cs_test_1',
|
||||
metadata: {
|
||||
kind: '0pt_credit_purchase',
|
||||
tenant_id: 't_0pt',
|
||||
purchase_id: 'purchase-1',
|
||||
},
|
||||
amount_total: 9900,
|
||||
currency: 'sek',
|
||||
payment_status: 'paid',
|
||||
payment_intent: {
|
||||
id: 'pi_123',
|
||||
status: 'succeeded',
|
||||
currency: 'sek',
|
||||
amount_received: 9900,
|
||||
},
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe('Stripe credit verification', () => {
|
||||
it('derives an idempotent verified-payment command from the re-read session', async () => {
|
||||
const command = await verifyStripeCreditPurchase(stripeReturning(session()), snapshot, {
|
||||
tenantId: 't_0pt',
|
||||
});
|
||||
expect(command).toEqual({
|
||||
tenantId: 't_0pt',
|
||||
idempotencyKey: 'credit_purchase:t_0pt:purchase-1',
|
||||
kind: 'verified_payment',
|
||||
reference: 'pi_123',
|
||||
amount: 100,
|
||||
});
|
||||
});
|
||||
|
||||
it('refuses a session that belongs to another tenant or purchase', async () => {
|
||||
await expect(
|
||||
verifyStripeCreditPurchase(
|
||||
stripeReturning(
|
||||
session({ metadata: { kind: '0pt_credit_purchase', tenant_id: 't_b', purchase_id: 'purchase-1' } }),
|
||||
),
|
||||
snapshot,
|
||||
{ tenantId: 't_0pt' },
|
||||
),
|
||||
).rejects.toBeInstanceOf(StripeCreditVerificationError);
|
||||
});
|
||||
|
||||
it('refuses an amount that differs from the server price snapshot', async () => {
|
||||
await expect(
|
||||
verifyStripeCreditPurchase(stripeReturning(session({ amount_total: 100 })), snapshot, {
|
||||
tenantId: 't_0pt',
|
||||
}),
|
||||
).rejects.toMatchObject({ reason: 'mismatch' });
|
||||
});
|
||||
|
||||
it('refuses an unpaid session or a non-succeeded payment intent', async () => {
|
||||
await expect(
|
||||
verifyStripeCreditPurchase(stripeReturning(session({ payment_status: 'unpaid' })), snapshot, {
|
||||
tenantId: 't_0pt',
|
||||
}),
|
||||
).rejects.toMatchObject({ reason: 'not_paid' });
|
||||
await expect(
|
||||
verifyStripeCreditPurchase(
|
||||
stripeReturning(
|
||||
session({
|
||||
payment_intent: { id: 'pi_123', status: 'processing', currency: 'sek', amount_received: 0 },
|
||||
}),
|
||||
),
|
||||
snapshot,
|
||||
{ tenantId: 't_0pt' },
|
||||
),
|
||||
).rejects.toMatchObject({ reason: 'not_paid' });
|
||||
});
|
||||
|
||||
it('refuses a mismatched caller tenant before any Stripe call', async () => {
|
||||
const reader = {
|
||||
checkout: {
|
||||
sessions: {
|
||||
retrieve: async () => {
|
||||
throw new Error('should not be called');
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as StripeCheckoutReader;
|
||||
await expect(verifyStripeCreditPurchase(reader, snapshot, { tenantId: 't_b' })).rejects.toMatchObject({
|
||||
reason: 'mismatch',
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user