palaryn 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +716 -0
- package/dist/sdk/typescript/src/client.d.ts +71 -0
- package/dist/sdk/typescript/src/client.d.ts.map +1 -0
- package/dist/sdk/typescript/src/client.js +176 -0
- package/dist/sdk/typescript/src/client.js.map +1 -0
- package/dist/sdk/typescript/src/errors.d.ts +50 -0
- package/dist/sdk/typescript/src/errors.d.ts.map +1 -0
- package/dist/sdk/typescript/src/errors.js +103 -0
- package/dist/sdk/typescript/src/errors.js.map +1 -0
- package/dist/sdk/typescript/src/index.d.ts +4 -0
- package/dist/sdk/typescript/src/index.d.ts.map +1 -0
- package/dist/sdk/typescript/src/index.js +15 -0
- package/dist/sdk/typescript/src/index.js.map +1 -0
- package/dist/sdk/typescript/src/types.d.ts +101 -0
- package/dist/sdk/typescript/src/types.d.ts.map +1 -0
- package/dist/sdk/typescript/src/types.js +6 -0
- package/dist/sdk/typescript/src/types.js.map +1 -0
- package/dist/src/admin/index.d.ts +2 -0
- package/dist/src/admin/index.d.ts.map +1 -0
- package/dist/src/admin/index.js +6 -0
- package/dist/src/admin/index.js.map +1 -0
- package/dist/src/admin/routes.d.ts +5 -0
- package/dist/src/admin/routes.d.ts.map +1 -0
- package/dist/src/admin/routes.js +471 -0
- package/dist/src/admin/routes.js.map +1 -0
- package/dist/src/admin/templates.d.ts +51 -0
- package/dist/src/admin/templates.d.ts.map +1 -0
- package/dist/src/admin/templates.js +500 -0
- package/dist/src/admin/templates.js.map +1 -0
- package/dist/src/anomaly/detector.d.ts +141 -0
- package/dist/src/anomaly/detector.d.ts.map +1 -0
- package/dist/src/anomaly/detector.js +554 -0
- package/dist/src/anomaly/detector.js.map +1 -0
- package/dist/src/anomaly/index.d.ts +2 -0
- package/dist/src/anomaly/index.d.ts.map +1 -0
- package/dist/src/anomaly/index.js +7 -0
- package/dist/src/anomaly/index.js.map +1 -0
- package/dist/src/approval/manager.d.ts +147 -0
- package/dist/src/approval/manager.d.ts.map +1 -0
- package/dist/src/approval/manager.js +511 -0
- package/dist/src/approval/manager.js.map +1 -0
- package/dist/src/approval/webhook.d.ts +36 -0
- package/dist/src/approval/webhook.d.ts.map +1 -0
- package/dist/src/approval/webhook.js +135 -0
- package/dist/src/approval/webhook.js.map +1 -0
- package/dist/src/audit/logger.d.ts +70 -0
- package/dist/src/audit/logger.d.ts.map +1 -0
- package/dist/src/audit/logger.js +440 -0
- package/dist/src/audit/logger.js.map +1 -0
- package/dist/src/auth/index.d.ts +6 -0
- package/dist/src/auth/index.d.ts.map +1 -0
- package/dist/src/auth/index.js +22 -0
- package/dist/src/auth/index.js.map +1 -0
- package/dist/src/auth/password.d.ts +3 -0
- package/dist/src/auth/password.d.ts.map +1 -0
- package/dist/src/auth/password.js +25 -0
- package/dist/src/auth/password.js.map +1 -0
- package/dist/src/auth/pkce.d.ts +13 -0
- package/dist/src/auth/pkce.d.ts.map +1 -0
- package/dist/src/auth/pkce.js +58 -0
- package/dist/src/auth/pkce.js.map +1 -0
- package/dist/src/auth/providers.d.ts +28 -0
- package/dist/src/auth/providers.d.ts.map +1 -0
- package/dist/src/auth/providers.js +198 -0
- package/dist/src/auth/providers.js.map +1 -0
- package/dist/src/auth/routes.d.ts +14 -0
- package/dist/src/auth/routes.d.ts.map +1 -0
- package/dist/src/auth/routes.js +431 -0
- package/dist/src/auth/routes.js.map +1 -0
- package/dist/src/auth/session.d.ts +24 -0
- package/dist/src/auth/session.d.ts.map +1 -0
- package/dist/src/auth/session.js +105 -0
- package/dist/src/auth/session.js.map +1 -0
- package/dist/src/billing/index.d.ts +7 -0
- package/dist/src/billing/index.d.ts.map +1 -0
- package/dist/src/billing/index.js +14 -0
- package/dist/src/billing/index.js.map +1 -0
- package/dist/src/billing/plan-enforcer.d.ts +44 -0
- package/dist/src/billing/plan-enforcer.d.ts.map +1 -0
- package/dist/src/billing/plan-enforcer.js +110 -0
- package/dist/src/billing/plan-enforcer.js.map +1 -0
- package/dist/src/billing/routes.d.ts +15 -0
- package/dist/src/billing/routes.d.ts.map +1 -0
- package/dist/src/billing/routes.js +193 -0
- package/dist/src/billing/routes.js.map +1 -0
- package/dist/src/billing/stripe-client.d.ts +14 -0
- package/dist/src/billing/stripe-client.d.ts.map +1 -0
- package/dist/src/billing/stripe-client.js +51 -0
- package/dist/src/billing/stripe-client.js.map +1 -0
- package/dist/src/billing/webhook-handler.d.ts +19 -0
- package/dist/src/billing/webhook-handler.d.ts.map +1 -0
- package/dist/src/billing/webhook-handler.js +169 -0
- package/dist/src/billing/webhook-handler.js.map +1 -0
- package/dist/src/billing/webhook-routes.d.ts +5 -0
- package/dist/src/billing/webhook-routes.d.ts.map +1 -0
- package/dist/src/billing/webhook-routes.js +30 -0
- package/dist/src/billing/webhook-routes.js.map +1 -0
- package/dist/src/budget/manager.d.ts +95 -0
- package/dist/src/budget/manager.d.ts.map +1 -0
- package/dist/src/budget/manager.js +547 -0
- package/dist/src/budget/manager.js.map +1 -0
- package/dist/src/budget/usage-extractor.d.ts +38 -0
- package/dist/src/budget/usage-extractor.d.ts.map +1 -0
- package/dist/src/budget/usage-extractor.js +165 -0
- package/dist/src/budget/usage-extractor.js.map +1 -0
- package/dist/src/cli.d.ts +3 -0
- package/dist/src/cli.d.ts.map +1 -0
- package/dist/src/cli.js +115 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/config/defaults.d.ts +3 -0
- package/dist/src/config/defaults.d.ts.map +1 -0
- package/dist/src/config/defaults.js +243 -0
- package/dist/src/config/defaults.js.map +1 -0
- package/dist/src/config/validate.d.ts +15 -0
- package/dist/src/config/validate.d.ts.map +1 -0
- package/dist/src/config/validate.js +105 -0
- package/dist/src/config/validate.js.map +1 -0
- package/dist/src/dlp/composite-scanner.d.ts +47 -0
- package/dist/src/dlp/composite-scanner.d.ts.map +1 -0
- package/dist/src/dlp/composite-scanner.js +186 -0
- package/dist/src/dlp/composite-scanner.js.map +1 -0
- package/dist/src/dlp/index.d.ts +10 -0
- package/dist/src/dlp/index.d.ts.map +1 -0
- package/dist/src/dlp/index.js +26 -0
- package/dist/src/dlp/index.js.map +1 -0
- package/dist/src/dlp/interfaces.d.ts +33 -0
- package/dist/src/dlp/interfaces.d.ts.map +1 -0
- package/dist/src/dlp/interfaces.js +3 -0
- package/dist/src/dlp/interfaces.js.map +1 -0
- package/dist/src/dlp/patterns.d.ts +9 -0
- package/dist/src/dlp/patterns.d.ts.map +1 -0
- package/dist/src/dlp/patterns.js +25 -0
- package/dist/src/dlp/patterns.js.map +1 -0
- package/dist/src/dlp/prompt-injection-backend.d.ts +68 -0
- package/dist/src/dlp/prompt-injection-backend.d.ts.map +1 -0
- package/dist/src/dlp/prompt-injection-backend.js +148 -0
- package/dist/src/dlp/prompt-injection-backend.js.map +1 -0
- package/dist/src/dlp/prompt-injection-patterns.d.ts +32 -0
- package/dist/src/dlp/prompt-injection-patterns.d.ts.map +1 -0
- package/dist/src/dlp/prompt-injection-patterns.js +290 -0
- package/dist/src/dlp/prompt-injection-patterns.js.map +1 -0
- package/dist/src/dlp/regex-backend.d.ts +32 -0
- package/dist/src/dlp/regex-backend.d.ts.map +1 -0
- package/dist/src/dlp/regex-backend.js +153 -0
- package/dist/src/dlp/regex-backend.js.map +1 -0
- package/dist/src/dlp/scanner.d.ts +122 -0
- package/dist/src/dlp/scanner.d.ts.map +1 -0
- package/dist/src/dlp/scanner.js +444 -0
- package/dist/src/dlp/scanner.js.map +1 -0
- package/dist/src/dlp/text-normalizer.d.ts +41 -0
- package/dist/src/dlp/text-normalizer.d.ts.map +1 -0
- package/dist/src/dlp/text-normalizer.js +203 -0
- package/dist/src/dlp/text-normalizer.js.map +1 -0
- package/dist/src/dlp/trufflehog-backend.d.ts +64 -0
- package/dist/src/dlp/trufflehog-backend.d.ts.map +1 -0
- package/dist/src/dlp/trufflehog-backend.js +151 -0
- package/dist/src/dlp/trufflehog-backend.js.map +1 -0
- package/dist/src/executor/http-executor.d.ts +25 -0
- package/dist/src/executor/http-executor.d.ts.map +1 -0
- package/dist/src/executor/http-executor.js +333 -0
- package/dist/src/executor/http-executor.js.map +1 -0
- package/dist/src/executor/index.d.ts +6 -0
- package/dist/src/executor/index.d.ts.map +1 -0
- package/dist/src/executor/index.js +12 -0
- package/dist/src/executor/index.js.map +1 -0
- package/dist/src/executor/interfaces.d.ts +11 -0
- package/dist/src/executor/interfaces.d.ts.map +1 -0
- package/dist/src/executor/interfaces.js +3 -0
- package/dist/src/executor/interfaces.js.map +1 -0
- package/dist/src/executor/noop-executor.d.ts +13 -0
- package/dist/src/executor/noop-executor.d.ts.map +1 -0
- package/dist/src/executor/noop-executor.js +21 -0
- package/dist/src/executor/noop-executor.js.map +1 -0
- package/dist/src/executor/registry.d.ts +30 -0
- package/dist/src/executor/registry.d.ts.map +1 -0
- package/dist/src/executor/registry.js +62 -0
- package/dist/src/executor/registry.js.map +1 -0
- package/dist/src/executor/slack-executor.d.ts +24 -0
- package/dist/src/executor/slack-executor.d.ts.map +1 -0
- package/dist/src/executor/slack-executor.js +147 -0
- package/dist/src/executor/slack-executor.js.map +1 -0
- package/dist/src/index.d.ts +25 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +74 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/mcp/auth-verifier.d.ts +23 -0
- package/dist/src/mcp/auth-verifier.d.ts.map +1 -0
- package/dist/src/mcp/auth-verifier.js +162 -0
- package/dist/src/mcp/auth-verifier.js.map +1 -0
- package/dist/src/mcp/bridge.d.ts +132 -0
- package/dist/src/mcp/bridge.d.ts.map +1 -0
- package/dist/src/mcp/bridge.js +734 -0
- package/dist/src/mcp/bridge.js.map +1 -0
- package/dist/src/mcp/http-transport.d.ts +32 -0
- package/dist/src/mcp/http-transport.d.ts.map +1 -0
- package/dist/src/mcp/http-transport.js +538 -0
- package/dist/src/mcp/http-transport.js.map +1 -0
- package/dist/src/mcp/index.d.ts +10 -0
- package/dist/src/mcp/index.d.ts.map +1 -0
- package/dist/src/mcp/index.js +17 -0
- package/dist/src/mcp/index.js.map +1 -0
- package/dist/src/mcp/oauth-pages.d.ts +23 -0
- package/dist/src/mcp/oauth-pages.d.ts.map +1 -0
- package/dist/src/mcp/oauth-pages.js +121 -0
- package/dist/src/mcp/oauth-pages.js.map +1 -0
- package/dist/src/mcp/oauth-postgres-stores.d.ts +55 -0
- package/dist/src/mcp/oauth-postgres-stores.d.ts.map +1 -0
- package/dist/src/mcp/oauth-postgres-stores.js +226 -0
- package/dist/src/mcp/oauth-postgres-stores.js.map +1 -0
- package/dist/src/mcp/oauth-provider.d.ts +95 -0
- package/dist/src/mcp/oauth-provider.d.ts.map +1 -0
- package/dist/src/mcp/oauth-provider.js +360 -0
- package/dist/src/mcp/oauth-provider.js.map +1 -0
- package/dist/src/mcp/oauth-stores.d.ts +62 -0
- package/dist/src/mcp/oauth-stores.d.ts.map +1 -0
- package/dist/src/mcp/oauth-stores.js +154 -0
- package/dist/src/mcp/oauth-stores.js.map +1 -0
- package/dist/src/mcp/server.d.ts +18 -0
- package/dist/src/mcp/server.d.ts.map +1 -0
- package/dist/src/mcp/server.js +51 -0
- package/dist/src/mcp/server.js.map +1 -0
- package/dist/src/metrics/collector.d.ts +106 -0
- package/dist/src/metrics/collector.d.ts.map +1 -0
- package/dist/src/metrics/collector.js +311 -0
- package/dist/src/metrics/collector.js.map +1 -0
- package/dist/src/metrics/index.d.ts +2 -0
- package/dist/src/metrics/index.d.ts.map +1 -0
- package/dist/src/metrics/index.js +6 -0
- package/dist/src/metrics/index.js.map +1 -0
- package/dist/src/middleware/auth.d.ts +77 -0
- package/dist/src/middleware/auth.d.ts.map +1 -0
- package/dist/src/middleware/auth.js +720 -0
- package/dist/src/middleware/auth.js.map +1 -0
- package/dist/src/middleware/session.d.ts +18 -0
- package/dist/src/middleware/session.d.ts.map +1 -0
- package/dist/src/middleware/session.js +67 -0
- package/dist/src/middleware/session.js.map +1 -0
- package/dist/src/middleware/validate.d.ts +3 -0
- package/dist/src/middleware/validate.d.ts.map +1 -0
- package/dist/src/middleware/validate.js +85 -0
- package/dist/src/middleware/validate.js.map +1 -0
- package/dist/src/policy/engine.d.ts +107 -0
- package/dist/src/policy/engine.d.ts.map +1 -0
- package/dist/src/policy/engine.js +646 -0
- package/dist/src/policy/engine.js.map +1 -0
- package/dist/src/policy/index.d.ts +3 -0
- package/dist/src/policy/index.d.ts.map +1 -0
- package/dist/src/policy/index.js +8 -0
- package/dist/src/policy/index.js.map +1 -0
- package/dist/src/policy/opa-engine.d.ts +176 -0
- package/dist/src/policy/opa-engine.d.ts.map +1 -0
- package/dist/src/policy/opa-engine.js +790 -0
- package/dist/src/policy/opa-engine.js.map +1 -0
- package/dist/src/proxy/forward-proxy.d.ts +30 -0
- package/dist/src/proxy/forward-proxy.d.ts.map +1 -0
- package/dist/src/proxy/forward-proxy.js +580 -0
- package/dist/src/proxy/forward-proxy.js.map +1 -0
- package/dist/src/proxy/index.d.ts +2 -0
- package/dist/src/proxy/index.d.ts.map +1 -0
- package/dist/src/proxy/index.js +8 -0
- package/dist/src/proxy/index.js.map +1 -0
- package/dist/src/ratelimit/limiter.d.ts +45 -0
- package/dist/src/ratelimit/limiter.d.ts.map +1 -0
- package/dist/src/ratelimit/limiter.js +158 -0
- package/dist/src/ratelimit/limiter.js.map +1 -0
- package/dist/src/replay/engine.d.ts +40 -0
- package/dist/src/replay/engine.d.ts.map +1 -0
- package/dist/src/replay/engine.js +106 -0
- package/dist/src/replay/engine.js.map +1 -0
- package/dist/src/replay/index.d.ts +2 -0
- package/dist/src/replay/index.d.ts.map +1 -0
- package/dist/src/replay/index.js +6 -0
- package/dist/src/replay/index.js.map +1 -0
- package/dist/src/saas/index.d.ts +2 -0
- package/dist/src/saas/index.d.ts.map +1 -0
- package/dist/src/saas/index.js +18 -0
- package/dist/src/saas/index.js.map +1 -0
- package/dist/src/saas/routes.d.ts +18 -0
- package/dist/src/saas/routes.d.ts.map +1 -0
- package/dist/src/saas/routes.js +1566 -0
- package/dist/src/saas/routes.js.map +1 -0
- package/dist/src/server/app.d.ts +44 -0
- package/dist/src/server/app.d.ts.map +1 -0
- package/dist/src/server/app.js +854 -0
- package/dist/src/server/app.js.map +1 -0
- package/dist/src/server/errors.d.ts +32 -0
- package/dist/src/server/errors.d.ts.map +1 -0
- package/dist/src/server/errors.js +39 -0
- package/dist/src/server/errors.js.map +1 -0
- package/dist/src/server/gateway.d.ts +165 -0
- package/dist/src/server/gateway.d.ts.map +1 -0
- package/dist/src/server/gateway.js +964 -0
- package/dist/src/server/gateway.js.map +1 -0
- package/dist/src/server/index.d.ts +2 -0
- package/dist/src/server/index.d.ts.map +1 -0
- package/dist/src/server/index.js +295 -0
- package/dist/src/server/index.js.map +1 -0
- package/dist/src/server/logger.d.ts +33 -0
- package/dist/src/server/logger.d.ts.map +1 -0
- package/dist/src/server/logger.js +230 -0
- package/dist/src/server/logger.js.map +1 -0
- package/dist/src/server/stream-proxy.d.ts +32 -0
- package/dist/src/server/stream-proxy.d.ts.map +1 -0
- package/dist/src/server/stream-proxy.js +184 -0
- package/dist/src/server/stream-proxy.js.map +1 -0
- package/dist/src/storage/file-persistence.d.ts +48 -0
- package/dist/src/storage/file-persistence.d.ts.map +1 -0
- package/dist/src/storage/file-persistence.js +280 -0
- package/dist/src/storage/file-persistence.js.map +1 -0
- package/dist/src/storage/index.d.ts +5 -0
- package/dist/src/storage/index.d.ts.map +1 -0
- package/dist/src/storage/index.js +21 -0
- package/dist/src/storage/index.js.map +1 -0
- package/dist/src/storage/interfaces.d.ts +237 -0
- package/dist/src/storage/interfaces.d.ts.map +1 -0
- package/dist/src/storage/interfaces.js +3 -0
- package/dist/src/storage/interfaces.js.map +1 -0
- package/dist/src/storage/memory.d.ts +162 -0
- package/dist/src/storage/memory.d.ts.map +1 -0
- package/dist/src/storage/memory.js +603 -0
- package/dist/src/storage/memory.js.map +1 -0
- package/dist/src/storage/postgres.d.ts +267 -0
- package/dist/src/storage/postgres.d.ts.map +1 -0
- package/dist/src/storage/postgres.js +1555 -0
- package/dist/src/storage/postgres.js.map +1 -0
- package/dist/src/storage/redis.d.ts +202 -0
- package/dist/src/storage/redis.d.ts.map +1 -0
- package/dist/src/storage/redis.js +629 -0
- package/dist/src/storage/redis.js.map +1 -0
- package/dist/src/tracing/index.d.ts +2 -0
- package/dist/src/tracing/index.d.ts.map +1 -0
- package/dist/src/tracing/index.js +6 -0
- package/dist/src/tracing/index.js.map +1 -0
- package/dist/src/tracing/provider.d.ts +43 -0
- package/dist/src/tracing/provider.d.ts.map +1 -0
- package/dist/src/tracing/provider.js +74 -0
- package/dist/src/tracing/provider.js.map +1 -0
- package/dist/src/trust/calculator.d.ts +54 -0
- package/dist/src/trust/calculator.d.ts.map +1 -0
- package/dist/src/trust/calculator.js +102 -0
- package/dist/src/trust/calculator.js.map +1 -0
- package/dist/src/trust/index.d.ts +2 -0
- package/dist/src/trust/index.d.ts.map +1 -0
- package/dist/src/trust/index.js +7 -0
- package/dist/src/trust/index.js.map +1 -0
- package/dist/src/types/budget.d.ts +30 -0
- package/dist/src/types/budget.d.ts.map +1 -0
- package/dist/src/types/budget.js +3 -0
- package/dist/src/types/budget.js.map +1 -0
- package/dist/src/types/config.d.ts +176 -0
- package/dist/src/types/config.d.ts.map +1 -0
- package/dist/src/types/config.js +3 -0
- package/dist/src/types/config.js.map +1 -0
- package/dist/src/types/events.d.ts +24 -0
- package/dist/src/types/events.d.ts.map +1 -0
- package/dist/src/types/events.js +3 -0
- package/dist/src/types/events.js.map +1 -0
- package/dist/src/types/index.d.ts +8 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/index.js +24 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/types/policy.d.ts +60 -0
- package/dist/src/types/policy.d.ts.map +1 -0
- package/dist/src/types/policy.js +3 -0
- package/dist/src/types/policy.js.map +1 -0
- package/dist/src/types/stripe-config.d.ts +12 -0
- package/dist/src/types/stripe-config.d.ts.map +1 -0
- package/dist/src/types/stripe-config.js +3 -0
- package/dist/src/types/stripe-config.js.map +1 -0
- package/dist/src/types/subscription.d.ts +24 -0
- package/dist/src/types/subscription.d.ts.map +1 -0
- package/dist/src/types/subscription.js +38 -0
- package/dist/src/types/subscription.js.map +1 -0
- package/dist/src/types/tool-call.d.ts +42 -0
- package/dist/src/types/tool-call.d.ts.map +1 -0
- package/dist/src/types/tool-call.js +3 -0
- package/dist/src/types/tool-call.js.map +1 -0
- package/dist/src/types/tool-result.d.ts +58 -0
- package/dist/src/types/tool-result.d.ts.map +1 -0
- package/dist/src/types/tool-result.js +3 -0
- package/dist/src/types/tool-result.js.map +1 -0
- package/dist/src/types/user.d.ts +101 -0
- package/dist/src/types/user.d.ts.map +1 -0
- package/dist/src/types/user.js +6 -0
- package/dist/src/types/user.js.map +1 -0
- package/dist/tests/integration/api.test.d.ts +2 -0
- package/dist/tests/integration/api.test.d.ts.map +1 -0
- package/dist/tests/integration/api.test.js +1199 -0
- package/dist/tests/integration/api.test.js.map +1 -0
- package/dist/tests/integration/proxy.test.d.ts +2 -0
- package/dist/tests/integration/proxy.test.d.ts.map +1 -0
- package/dist/tests/integration/proxy.test.js +251 -0
- package/dist/tests/integration/proxy.test.js.map +1 -0
- package/dist/tests/integration/storage.test.d.ts +16 -0
- package/dist/tests/integration/storage.test.d.ts.map +1 -0
- package/dist/tests/integration/storage.test.js +826 -0
- package/dist/tests/integration/storage.test.js.map +1 -0
- package/dist/tests/unit/admin.test.d.ts +2 -0
- package/dist/tests/unit/admin.test.d.ts.map +1 -0
- package/dist/tests/unit/admin.test.js +698 -0
- package/dist/tests/unit/admin.test.js.map +1 -0
- package/dist/tests/unit/anomaly-detector.test.d.ts +2 -0
- package/dist/tests/unit/anomaly-detector.test.d.ts.map +1 -0
- package/dist/tests/unit/anomaly-detector.test.js +903 -0
- package/dist/tests/unit/anomaly-detector.test.js.map +1 -0
- package/dist/tests/unit/approval-manager.test.d.ts +2 -0
- package/dist/tests/unit/approval-manager.test.d.ts.map +1 -0
- package/dist/tests/unit/approval-manager.test.js +528 -0
- package/dist/tests/unit/approval-manager.test.js.map +1 -0
- package/dist/tests/unit/approval-webhook.test.d.ts +2 -0
- package/dist/tests/unit/approval-webhook.test.d.ts.map +1 -0
- package/dist/tests/unit/approval-webhook.test.js +355 -0
- package/dist/tests/unit/approval-webhook.test.js.map +1 -0
- package/dist/tests/unit/audit-logger.test.d.ts +2 -0
- package/dist/tests/unit/audit-logger.test.d.ts.map +1 -0
- package/dist/tests/unit/audit-logger.test.js +635 -0
- package/dist/tests/unit/audit-logger.test.js.map +1 -0
- package/dist/tests/unit/auth-routes.test.d.ts +2 -0
- package/dist/tests/unit/auth-routes.test.d.ts.map +1 -0
- package/dist/tests/unit/auth-routes.test.js +281 -0
- package/dist/tests/unit/auth-routes.test.js.map +1 -0
- package/dist/tests/unit/auth.test.d.ts +2 -0
- package/dist/tests/unit/auth.test.d.ts.map +1 -0
- package/dist/tests/unit/auth.test.js +1382 -0
- package/dist/tests/unit/auth.test.js.map +1 -0
- package/dist/tests/unit/billing.test.d.ts +2 -0
- package/dist/tests/unit/billing.test.d.ts.map +1 -0
- package/dist/tests/unit/billing.test.js +579 -0
- package/dist/tests/unit/billing.test.js.map +1 -0
- package/dist/tests/unit/budget-manager.test.d.ts +2 -0
- package/dist/tests/unit/budget-manager.test.d.ts.map +1 -0
- package/dist/tests/unit/budget-manager.test.js +778 -0
- package/dist/tests/unit/budget-manager.test.js.map +1 -0
- package/dist/tests/unit/budget-race.test.d.ts +2 -0
- package/dist/tests/unit/budget-race.test.d.ts.map +1 -0
- package/dist/tests/unit/budget-race.test.js +58 -0
- package/dist/tests/unit/budget-race.test.js.map +1 -0
- package/dist/tests/unit/cli.test.d.ts +2 -0
- package/dist/tests/unit/cli.test.d.ts.map +1 -0
- package/dist/tests/unit/cli.test.js +93 -0
- package/dist/tests/unit/cli.test.js.map +1 -0
- package/dist/tests/unit/concurrency.test.d.ts +2 -0
- package/dist/tests/unit/concurrency.test.d.ts.map +1 -0
- package/dist/tests/unit/concurrency.test.js +1270 -0
- package/dist/tests/unit/concurrency.test.js.map +1 -0
- package/dist/tests/unit/config-validate.test.d.ts +2 -0
- package/dist/tests/unit/config-validate.test.d.ts.map +1 -0
- package/dist/tests/unit/config-validate.test.js +230 -0
- package/dist/tests/unit/config-validate.test.js.map +1 -0
- package/dist/tests/unit/defaults.test.d.ts +2 -0
- package/dist/tests/unit/defaults.test.d.ts.map +1 -0
- package/dist/tests/unit/defaults.test.js +364 -0
- package/dist/tests/unit/defaults.test.js.map +1 -0
- package/dist/tests/unit/dlp-backends.test.d.ts +2 -0
- package/dist/tests/unit/dlp-backends.test.d.ts.map +1 -0
- package/dist/tests/unit/dlp-backends.test.js +563 -0
- package/dist/tests/unit/dlp-backends.test.js.map +1 -0
- package/dist/tests/unit/dlp-scanner.test.d.ts +2 -0
- package/dist/tests/unit/dlp-scanner.test.d.ts.map +1 -0
- package/dist/tests/unit/dlp-scanner.test.js +739 -0
- package/dist/tests/unit/dlp-scanner.test.js.map +1 -0
- package/dist/tests/unit/error-responses.test.d.ts +2 -0
- package/dist/tests/unit/error-responses.test.d.ts.map +1 -0
- package/dist/tests/unit/error-responses.test.js +101 -0
- package/dist/tests/unit/error-responses.test.js.map +1 -0
- package/dist/tests/unit/executor-registry.test.d.ts +2 -0
- package/dist/tests/unit/executor-registry.test.d.ts.map +1 -0
- package/dist/tests/unit/executor-registry.test.js +390 -0
- package/dist/tests/unit/executor-registry.test.js.map +1 -0
- package/dist/tests/unit/forward-proxy.test.d.ts +2 -0
- package/dist/tests/unit/forward-proxy.test.d.ts.map +1 -0
- package/dist/tests/unit/forward-proxy.test.js +621 -0
- package/dist/tests/unit/forward-proxy.test.js.map +1 -0
- package/dist/tests/unit/gateway-features.test.d.ts +2 -0
- package/dist/tests/unit/gateway-features.test.d.ts.map +1 -0
- package/dist/tests/unit/gateway-features.test.js +753 -0
- package/dist/tests/unit/gateway-features.test.js.map +1 -0
- package/dist/tests/unit/http-executor.test.d.ts +2 -0
- package/dist/tests/unit/http-executor.test.d.ts.map +1 -0
- package/dist/tests/unit/http-executor.test.js +310 -0
- package/dist/tests/unit/http-executor.test.js.map +1 -0
- package/dist/tests/unit/mcp-bridge.test.d.ts +2 -0
- package/dist/tests/unit/mcp-bridge.test.d.ts.map +1 -0
- package/dist/tests/unit/mcp-bridge.test.js +1136 -0
- package/dist/tests/unit/mcp-bridge.test.js.map +1 -0
- package/dist/tests/unit/mcp-http-transport.test.d.ts +2 -0
- package/dist/tests/unit/mcp-http-transport.test.d.ts.map +1 -0
- package/dist/tests/unit/mcp-http-transport.test.js +899 -0
- package/dist/tests/unit/mcp-http-transport.test.js.map +1 -0
- package/dist/tests/unit/mcp-oauth.test.d.ts +2 -0
- package/dist/tests/unit/mcp-oauth.test.d.ts.map +1 -0
- package/dist/tests/unit/mcp-oauth.test.js +759 -0
- package/dist/tests/unit/mcp-oauth.test.js.map +1 -0
- package/dist/tests/unit/mcp-server.test.d.ts +15 -0
- package/dist/tests/unit/mcp-server.test.d.ts.map +1 -0
- package/dist/tests/unit/mcp-server.test.js +158 -0
- package/dist/tests/unit/mcp-server.test.js.map +1 -0
- package/dist/tests/unit/metrics.test.d.ts +2 -0
- package/dist/tests/unit/metrics.test.d.ts.map +1 -0
- package/dist/tests/unit/metrics.test.js +208 -0
- package/dist/tests/unit/metrics.test.js.map +1 -0
- package/dist/tests/unit/oauth.test.d.ts +2 -0
- package/dist/tests/unit/oauth.test.d.ts.map +1 -0
- package/dist/tests/unit/oauth.test.js +281 -0
- package/dist/tests/unit/oauth.test.js.map +1 -0
- package/dist/tests/unit/opa-circuit-breaker.test.d.ts +2 -0
- package/dist/tests/unit/opa-circuit-breaker.test.d.ts.map +1 -0
- package/dist/tests/unit/opa-circuit-breaker.test.js +297 -0
- package/dist/tests/unit/opa-circuit-breaker.test.js.map +1 -0
- package/dist/tests/unit/opa-engine.test.d.ts +2 -0
- package/dist/tests/unit/opa-engine.test.d.ts.map +1 -0
- package/dist/tests/unit/opa-engine.test.js +1813 -0
- package/dist/tests/unit/opa-engine.test.js.map +1 -0
- package/dist/tests/unit/pipeline-timing.test.d.ts +2 -0
- package/dist/tests/unit/pipeline-timing.test.d.ts.map +1 -0
- package/dist/tests/unit/pipeline-timing.test.js +528 -0
- package/dist/tests/unit/pipeline-timing.test.js.map +1 -0
- package/dist/tests/unit/policy-engine.test.d.ts +2 -0
- package/dist/tests/unit/policy-engine.test.d.ts.map +1 -0
- package/dist/tests/unit/policy-engine.test.js +1345 -0
- package/dist/tests/unit/policy-engine.test.js.map +1 -0
- package/dist/tests/unit/policy-store.test.d.ts +2 -0
- package/dist/tests/unit/policy-store.test.d.ts.map +1 -0
- package/dist/tests/unit/policy-store.test.js +60 -0
- package/dist/tests/unit/policy-store.test.js.map +1 -0
- package/dist/tests/unit/postgres-storage.test.d.ts +2 -0
- package/dist/tests/unit/postgres-storage.test.d.ts.map +1 -0
- package/dist/tests/unit/postgres-storage.test.js +614 -0
- package/dist/tests/unit/postgres-storage.test.js.map +1 -0
- package/dist/tests/unit/prompt-injection-backend.test.d.ts +2 -0
- package/dist/tests/unit/prompt-injection-backend.test.d.ts.map +1 -0
- package/dist/tests/unit/prompt-injection-backend.test.js +621 -0
- package/dist/tests/unit/prompt-injection-backend.test.js.map +1 -0
- package/dist/tests/unit/proxy-hardening.test.d.ts +2 -0
- package/dist/tests/unit/proxy-hardening.test.d.ts.map +1 -0
- package/dist/tests/unit/proxy-hardening.test.js +166 -0
- package/dist/tests/unit/proxy-hardening.test.js.map +1 -0
- package/dist/tests/unit/rate-limiter.test.d.ts +2 -0
- package/dist/tests/unit/rate-limiter.test.d.ts.map +1 -0
- package/dist/tests/unit/rate-limiter.test.js +443 -0
- package/dist/tests/unit/rate-limiter.test.js.map +1 -0
- package/dist/tests/unit/redis-storage.test.d.ts +2 -0
- package/dist/tests/unit/redis-storage.test.d.ts.map +1 -0
- package/dist/tests/unit/redis-storage.test.js +766 -0
- package/dist/tests/unit/redis-storage.test.js.map +1 -0
- package/dist/tests/unit/replay-engine.test.d.ts +2 -0
- package/dist/tests/unit/replay-engine.test.d.ts.map +1 -0
- package/dist/tests/unit/replay-engine.test.js +371 -0
- package/dist/tests/unit/replay-engine.test.js.map +1 -0
- package/dist/tests/unit/saas-routes.test.d.ts +2 -0
- package/dist/tests/unit/saas-routes.test.d.ts.map +1 -0
- package/dist/tests/unit/saas-routes.test.js +1399 -0
- package/dist/tests/unit/saas-routes.test.js.map +1 -0
- package/dist/tests/unit/session.test.d.ts +2 -0
- package/dist/tests/unit/session.test.d.ts.map +1 -0
- package/dist/tests/unit/session.test.js +532 -0
- package/dist/tests/unit/session.test.js.map +1 -0
- package/dist/tests/unit/slack-executor.test.d.ts +2 -0
- package/dist/tests/unit/slack-executor.test.d.ts.map +1 -0
- package/dist/tests/unit/slack-executor.test.js +209 -0
- package/dist/tests/unit/slack-executor.test.js.map +1 -0
- package/dist/tests/unit/storage-hardening.test.d.ts +2 -0
- package/dist/tests/unit/storage-hardening.test.d.ts.map +1 -0
- package/dist/tests/unit/storage-hardening.test.js +165 -0
- package/dist/tests/unit/storage-hardening.test.js.map +1 -0
- package/dist/tests/unit/storage.test.d.ts +2 -0
- package/dist/tests/unit/storage.test.d.ts.map +1 -0
- package/dist/tests/unit/storage.test.js +698 -0
- package/dist/tests/unit/storage.test.js.map +1 -0
- package/dist/tests/unit/text-normalizer.test.d.ts +2 -0
- package/dist/tests/unit/text-normalizer.test.d.ts.map +1 -0
- package/dist/tests/unit/text-normalizer.test.js +229 -0
- package/dist/tests/unit/text-normalizer.test.js.map +1 -0
- package/dist/tests/unit/tracing.test.d.ts +2 -0
- package/dist/tests/unit/tracing.test.d.ts.map +1 -0
- package/dist/tests/unit/tracing.test.js +611 -0
- package/dist/tests/unit/tracing.test.js.map +1 -0
- package/dist/tests/unit/trust-calculator.test.d.ts +2 -0
- package/dist/tests/unit/trust-calculator.test.d.ts.map +1 -0
- package/dist/tests/unit/trust-calculator.test.js +497 -0
- package/dist/tests/unit/trust-calculator.test.js.map +1 -0
- package/dist/tests/unit/ts-sdk.test.d.ts +2 -0
- package/dist/tests/unit/ts-sdk.test.d.ts.map +1 -0
- package/dist/tests/unit/ts-sdk.test.js +421 -0
- package/dist/tests/unit/ts-sdk.test.js.map +1 -0
- package/dist/tests/unit/usage-extractor-llm.test.d.ts +2 -0
- package/dist/tests/unit/usage-extractor-llm.test.d.ts.map +1 -0
- package/dist/tests/unit/usage-extractor-llm.test.js +139 -0
- package/dist/tests/unit/usage-extractor-llm.test.js.map +1 -0
- package/dist/tests/unit/usage-extractor.test.d.ts +2 -0
- package/dist/tests/unit/usage-extractor.test.d.ts.map +1 -0
- package/dist/tests/unit/usage-extractor.test.js +271 -0
- package/dist/tests/unit/usage-extractor.test.js.map +1 -0
- package/dist/tests/unit/user-stores.test.d.ts +2 -0
- package/dist/tests/unit/user-stores.test.d.ts.map +1 -0
- package/dist/tests/unit/user-stores.test.js +687 -0
- package/dist/tests/unit/user-stores.test.js.map +1 -0
- package/dist/tests/unit/validate.test.d.ts +2 -0
- package/dist/tests/unit/validate.test.d.ts.map +1 -0
- package/dist/tests/unit/validate.test.js +545 -0
- package/dist/tests/unit/validate.test.js.map +1 -0
- package/package.json +86 -0
- package/policy-packs/README.md +42 -0
- package/policy-packs/default.yaml +46 -0
- package/policy-packs/dev_fast.yaml +54 -0
- package/policy-packs/prod_strict.yaml +83 -0
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const postgres_1 = require("../../src/storage/postgres");
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Helper factories
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
function makeToolResult(overrides = {}) {
|
|
8
|
+
return {
|
|
9
|
+
tool_call_id: 'tc-001',
|
|
10
|
+
task_id: 'task-001',
|
|
11
|
+
status: 'ok',
|
|
12
|
+
policy: { decision: 'allow', reasons: [] },
|
|
13
|
+
dlp: { detected: [], redactions: [], severity: 'low' },
|
|
14
|
+
budget: { estimated_cost_usd: 0, spent_cost_usd_task: 0, remaining_cost_usd_task: 100 },
|
|
15
|
+
timing: { started_at: new Date().toISOString(), duration_ms: 10 },
|
|
16
|
+
...overrides,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function makeApprovalRecord(overrides = {}) {
|
|
20
|
+
return {
|
|
21
|
+
approval_id: 'appr-001',
|
|
22
|
+
tool_call_id: 'tc-001',
|
|
23
|
+
task_id: 'task-001',
|
|
24
|
+
workspace_id: 'ws-001',
|
|
25
|
+
actor_id: 'actor-001',
|
|
26
|
+
tool_name: 'http.get',
|
|
27
|
+
tool_capability: 'read',
|
|
28
|
+
args_summary: '{}',
|
|
29
|
+
scope: 'global',
|
|
30
|
+
reason: 'test',
|
|
31
|
+
token_hash: 'hash-001',
|
|
32
|
+
status: 'pending',
|
|
33
|
+
created_at: new Date().toISOString(),
|
|
34
|
+
expires_at: new Date(Date.now() + 3600000).toISOString(),
|
|
35
|
+
...overrides,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
// Mock pg Pool
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
const mockQuery = jest.fn().mockResolvedValue({ rows: [] });
|
|
42
|
+
const mockPool = { query: mockQuery };
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
jest.clearAllMocks();
|
|
45
|
+
});
|
|
46
|
+
// ===========================================================================
|
|
47
|
+
// PostgresBudgetStore
|
|
48
|
+
// ===========================================================================
|
|
49
|
+
describe('PostgresBudgetStore', () => {
|
|
50
|
+
let store;
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
store = new postgres_1.PostgresBudgetStore(mockPool);
|
|
53
|
+
});
|
|
54
|
+
const makeBudgetState = (overrides = {}) => ({
|
|
55
|
+
task_id: 'task-1',
|
|
56
|
+
workspace_id: 'ws-1',
|
|
57
|
+
actor_id: 'actor-1',
|
|
58
|
+
spent_usd: 0,
|
|
59
|
+
steps: 0,
|
|
60
|
+
started_at: '2025-01-01T00:00:00.000Z',
|
|
61
|
+
...overrides,
|
|
62
|
+
});
|
|
63
|
+
test('getTaskState returns undefined for missing tasks', () => {
|
|
64
|
+
expect(store.getTaskState('nonexistent')).toBeUndefined();
|
|
65
|
+
});
|
|
66
|
+
test('setTaskState saves and retrieves task state from cache', () => {
|
|
67
|
+
const state = makeBudgetState();
|
|
68
|
+
store.setTaskState('task-1', state);
|
|
69
|
+
const retrieved = store.getTaskState('task-1');
|
|
70
|
+
expect(retrieved).toEqual(state);
|
|
71
|
+
});
|
|
72
|
+
test('getCounter returns 0 for missing keys', () => {
|
|
73
|
+
expect(store.getCounter('missing-key')).toBe(0);
|
|
74
|
+
});
|
|
75
|
+
test('incrementCounter accumulates values', () => {
|
|
76
|
+
store.incrementCounter('spend:actor-1', 1.5);
|
|
77
|
+
expect(store.getCounter('spend:actor-1')).toBe(1.5);
|
|
78
|
+
store.incrementCounter('spend:actor-1', 2.5);
|
|
79
|
+
expect(store.getCounter('spend:actor-1')).toBe(4.0);
|
|
80
|
+
store.incrementCounter('spend:actor-1', 0.5);
|
|
81
|
+
expect(store.getCounter('spend:actor-1')).toBe(4.5);
|
|
82
|
+
});
|
|
83
|
+
test('getRetryCount returns 0 for missing tool call IDs', () => {
|
|
84
|
+
expect(store.getRetryCount('tc-unknown')).toBe(0);
|
|
85
|
+
});
|
|
86
|
+
test('incrementRetryCount increments and returns updated count', () => {
|
|
87
|
+
expect(store.incrementRetryCount('tc-1')).toBe(1);
|
|
88
|
+
expect(store.incrementRetryCount('tc-1')).toBe(2);
|
|
89
|
+
expect(store.incrementRetryCount('tc-1')).toBe(3);
|
|
90
|
+
expect(store.getRetryCount('tc-1')).toBe(3);
|
|
91
|
+
});
|
|
92
|
+
test('reset clears all state', () => {
|
|
93
|
+
const state = makeBudgetState();
|
|
94
|
+
store.setTaskState('task-1', state);
|
|
95
|
+
store.incrementCounter('key-1', 10);
|
|
96
|
+
store.incrementRetryCount('tc-1');
|
|
97
|
+
store.reset();
|
|
98
|
+
expect(store.getTaskState('task-1')).toBeUndefined();
|
|
99
|
+
expect(store.getCounter('key-1')).toBe(0);
|
|
100
|
+
expect(store.getRetryCount('tc-1')).toBe(0);
|
|
101
|
+
});
|
|
102
|
+
test('reset fires TRUNCATE query', () => {
|
|
103
|
+
store.reset();
|
|
104
|
+
expect(mockQuery).toHaveBeenCalledWith('TRUNCATE budget_task_states, budget_counters, budget_retry_counts', undefined);
|
|
105
|
+
});
|
|
106
|
+
test('setTaskState fires an async INSERT/UPDATE query', () => {
|
|
107
|
+
const state = makeBudgetState({ task_id: 'task-42', spent_usd: 3.14, steps: 7 });
|
|
108
|
+
store.setTaskState('task-42', state);
|
|
109
|
+
expect(mockQuery).toHaveBeenCalledTimes(1);
|
|
110
|
+
const [sql, params] = mockQuery.mock.calls[0];
|
|
111
|
+
expect(sql).toContain('INSERT INTO budget_task_states');
|
|
112
|
+
expect(sql).toContain('ON CONFLICT (task_id) DO UPDATE');
|
|
113
|
+
expect(params).toEqual([
|
|
114
|
+
state.task_id,
|
|
115
|
+
state.workspace_id,
|
|
116
|
+
state.actor_id,
|
|
117
|
+
state.spent_usd,
|
|
118
|
+
state.steps,
|
|
119
|
+
state.started_at,
|
|
120
|
+
]);
|
|
121
|
+
});
|
|
122
|
+
test('incrementCounter fires an async INSERT/UPDATE query', () => {
|
|
123
|
+
store.incrementCounter('spend:actor-1', 2.0);
|
|
124
|
+
expect(mockQuery).toHaveBeenCalledTimes(1);
|
|
125
|
+
const [sql, params] = mockQuery.mock.calls[0];
|
|
126
|
+
expect(sql).toContain('INSERT INTO budget_counters');
|
|
127
|
+
expect(sql).toContain('ON CONFLICT (key) DO UPDATE');
|
|
128
|
+
expect(params).toEqual(['spend:actor-1', 2.0]);
|
|
129
|
+
});
|
|
130
|
+
test('incrementRetryCount fires an async INSERT/UPDATE query', () => {
|
|
131
|
+
store.incrementRetryCount('tc-99');
|
|
132
|
+
expect(mockQuery).toHaveBeenCalledTimes(1);
|
|
133
|
+
const [sql, params] = mockQuery.mock.calls[0];
|
|
134
|
+
expect(sql).toContain('INSERT INTO budget_retry_counts');
|
|
135
|
+
expect(sql).toContain('ON CONFLICT (tool_call_id) DO UPDATE');
|
|
136
|
+
expect(params).toEqual(['tc-99', 1]);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
// ===========================================================================
|
|
140
|
+
// PostgresAuditStore
|
|
141
|
+
// ===========================================================================
|
|
142
|
+
describe('PostgresAuditStore', () => {
|
|
143
|
+
let store;
|
|
144
|
+
beforeEach(() => {
|
|
145
|
+
store = new postgres_1.PostgresAuditStore(mockPool);
|
|
146
|
+
});
|
|
147
|
+
const makeEvent = (overrides = {}) => ({
|
|
148
|
+
event_id: 'evt-1',
|
|
149
|
+
event_type: 'TOOL_CALL_RECEIVED',
|
|
150
|
+
timestamp: '2025-01-01T00:00:00.000Z',
|
|
151
|
+
tool_call_id: 'tc-1',
|
|
152
|
+
task_id: 'task-1',
|
|
153
|
+
workspace_id: 'ws-1',
|
|
154
|
+
actor_id: 'actor-1',
|
|
155
|
+
tool_name: 'http.get',
|
|
156
|
+
metadata: {},
|
|
157
|
+
...overrides,
|
|
158
|
+
});
|
|
159
|
+
test('append adds events to cache and fires async INSERT', () => {
|
|
160
|
+
const event = makeEvent();
|
|
161
|
+
store.append(event);
|
|
162
|
+
// Verify it's in the cache
|
|
163
|
+
expect(store.getAll()).toHaveLength(1);
|
|
164
|
+
expect(store.getAll()[0]).toEqual(event);
|
|
165
|
+
// Verify the INSERT was fired
|
|
166
|
+
expect(mockQuery).toHaveBeenCalledTimes(1);
|
|
167
|
+
const [sql, params] = mockQuery.mock.calls[0];
|
|
168
|
+
expect(sql).toContain('INSERT INTO audit_events');
|
|
169
|
+
expect(sql).toContain('ON CONFLICT (event_id) DO NOTHING');
|
|
170
|
+
expect(params).toEqual([
|
|
171
|
+
event.event_id,
|
|
172
|
+
event.event_type,
|
|
173
|
+
event.timestamp,
|
|
174
|
+
event.tool_call_id,
|
|
175
|
+
event.task_id,
|
|
176
|
+
event.workspace_id,
|
|
177
|
+
event.actor_id,
|
|
178
|
+
event.tool_name,
|
|
179
|
+
JSON.stringify(event.metadata),
|
|
180
|
+
]);
|
|
181
|
+
});
|
|
182
|
+
test('getByTaskId filters correctly', () => {
|
|
183
|
+
store.append(makeEvent({ event_id: 'evt-1', task_id: 'task-A', timestamp: '2025-01-01T00:00:02Z' }));
|
|
184
|
+
store.append(makeEvent({ event_id: 'evt-2', task_id: 'task-B', timestamp: '2025-01-01T00:00:01Z' }));
|
|
185
|
+
store.append(makeEvent({ event_id: 'evt-3', task_id: 'task-A', timestamp: '2025-01-01T00:00:00Z' }));
|
|
186
|
+
const results = store.getByTaskId('task-A');
|
|
187
|
+
expect(results).toHaveLength(2);
|
|
188
|
+
// Should be sorted by timestamp ascending
|
|
189
|
+
expect(results[0].event_id).toBe('evt-3');
|
|
190
|
+
expect(results[1].event_id).toBe('evt-1');
|
|
191
|
+
});
|
|
192
|
+
test('getByToolCallId filters correctly', () => {
|
|
193
|
+
store.append(makeEvent({ event_id: 'evt-1', tool_call_id: 'tc-X' }));
|
|
194
|
+
store.append(makeEvent({ event_id: 'evt-2', tool_call_id: 'tc-Y' }));
|
|
195
|
+
store.append(makeEvent({ event_id: 'evt-3', tool_call_id: 'tc-X' }));
|
|
196
|
+
const results = store.getByToolCallId('tc-X');
|
|
197
|
+
expect(results).toHaveLength(2);
|
|
198
|
+
expect(results.every((e) => e.tool_call_id === 'tc-X')).toBe(true);
|
|
199
|
+
});
|
|
200
|
+
test('getByEventType filters correctly', () => {
|
|
201
|
+
store.append(makeEvent({ event_id: 'evt-1', event_type: 'TOOL_CALL_RECEIVED' }));
|
|
202
|
+
store.append(makeEvent({ event_id: 'evt-2', event_type: 'POLICY_DECIDED' }));
|
|
203
|
+
store.append(makeEvent({ event_id: 'evt-3', event_type: 'TOOL_CALL_RECEIVED' }));
|
|
204
|
+
const results = store.getByEventType('TOOL_CALL_RECEIVED');
|
|
205
|
+
expect(results).toHaveLength(2);
|
|
206
|
+
expect(results.every((e) => e.event_type === 'TOOL_CALL_RECEIVED')).toBe(true);
|
|
207
|
+
});
|
|
208
|
+
test('getAll returns all events', () => {
|
|
209
|
+
store.append(makeEvent({ event_id: 'evt-1' }));
|
|
210
|
+
store.append(makeEvent({ event_id: 'evt-2' }));
|
|
211
|
+
store.append(makeEvent({ event_id: 'evt-3' }));
|
|
212
|
+
const all = store.getAll();
|
|
213
|
+
expect(all).toHaveLength(3);
|
|
214
|
+
});
|
|
215
|
+
test('getAll returns a copy (not the internal array)', () => {
|
|
216
|
+
store.append(makeEvent({ event_id: 'evt-1' }));
|
|
217
|
+
const all = store.getAll();
|
|
218
|
+
all.push(makeEvent({ event_id: 'evt-pushed' }));
|
|
219
|
+
expect(store.getAll()).toHaveLength(1);
|
|
220
|
+
});
|
|
221
|
+
test('clear empties cache and truncates table', () => {
|
|
222
|
+
store.append(makeEvent({ event_id: 'evt-1' }));
|
|
223
|
+
store.append(makeEvent({ event_id: 'evt-2' }));
|
|
224
|
+
mockQuery.mockClear();
|
|
225
|
+
store.clear();
|
|
226
|
+
expect(store.getAll()).toHaveLength(0);
|
|
227
|
+
expect(mockQuery).toHaveBeenCalledWith('TRUNCATE audit_events', undefined);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
// ===========================================================================
|
|
231
|
+
// PostgresApprovalStore
|
|
232
|
+
// ===========================================================================
|
|
233
|
+
describe('PostgresApprovalStore', () => {
|
|
234
|
+
let store;
|
|
235
|
+
beforeEach(() => {
|
|
236
|
+
store = new postgres_1.PostgresApprovalStore(mockPool);
|
|
237
|
+
});
|
|
238
|
+
test('save and getById', () => {
|
|
239
|
+
const approval = makeApprovalRecord({ approval_id: 'apr-1', workspace_id: 'ws-1', tool_call_id: 'tc-1' });
|
|
240
|
+
store.save('apr-1', approval);
|
|
241
|
+
expect(store.getById('apr-1')).toEqual(approval);
|
|
242
|
+
expect(store.getById('apr-missing')).toBeUndefined();
|
|
243
|
+
});
|
|
244
|
+
test('save fires async INSERT/UPDATE query', () => {
|
|
245
|
+
const approval = makeApprovalRecord({ approval_id: 'apr-1', workspace_id: 'ws-1', tool_call_id: 'tc-1' });
|
|
246
|
+
store.save('apr-1', approval);
|
|
247
|
+
expect(mockQuery).toHaveBeenCalledTimes(1);
|
|
248
|
+
const [sql, params] = mockQuery.mock.calls[0];
|
|
249
|
+
expect(sql).toContain('INSERT INTO approvals');
|
|
250
|
+
expect(sql).toContain('ON CONFLICT (approval_id) DO UPDATE');
|
|
251
|
+
expect(params[0]).toBe('apr-1');
|
|
252
|
+
expect(params[1]).toBe(JSON.stringify(approval));
|
|
253
|
+
expect(params[2]).toBe('pending');
|
|
254
|
+
expect(params[3]).toBe('ws-1');
|
|
255
|
+
expect(params[4]).toBe('tc-1');
|
|
256
|
+
});
|
|
257
|
+
test('indexToken and getByToken', () => {
|
|
258
|
+
const approval = makeApprovalRecord({ approval_id: 'apr-1', workspace_id: 'ws-1', tool_call_id: 'tc-1' });
|
|
259
|
+
store.save('apr-1', approval);
|
|
260
|
+
store.indexToken('tok-abc', 'apr-1');
|
|
261
|
+
expect(store.getByToken('tok-abc')).toEqual(approval);
|
|
262
|
+
expect(store.getByToken('tok-missing')).toBeUndefined();
|
|
263
|
+
});
|
|
264
|
+
test('indexToken fires async INSERT/UPDATE query', () => {
|
|
265
|
+
store.indexToken('tok-abc', 'apr-1');
|
|
266
|
+
// The last call should be for the token insert
|
|
267
|
+
const calls = mockQuery.mock.calls;
|
|
268
|
+
const lastCall = calls[calls.length - 1];
|
|
269
|
+
expect(lastCall[0]).toContain('INSERT INTO approval_tokens');
|
|
270
|
+
expect(lastCall[0]).toContain('ON CONFLICT (token) DO UPDATE');
|
|
271
|
+
expect(lastCall[1]).toEqual(['tok-abc', 'apr-1']);
|
|
272
|
+
});
|
|
273
|
+
test('getByToolCallId searches approvals', () => {
|
|
274
|
+
store.save('apr-1', makeApprovalRecord({ approval_id: 'apr-1', tool_call_id: 'tc-A', workspace_id: 'ws-1' }));
|
|
275
|
+
store.save('apr-2', makeApprovalRecord({ approval_id: 'apr-2', tool_call_id: 'tc-B', workspace_id: 'ws-1' }));
|
|
276
|
+
const found = store.getByToolCallId('tc-A');
|
|
277
|
+
expect(found).toBeDefined();
|
|
278
|
+
expect(found.tool_call_id).toBe('tc-A');
|
|
279
|
+
expect(found.workspace_id).toBe('ws-1');
|
|
280
|
+
expect(store.getByToolCallId('tc-C')).toBeUndefined();
|
|
281
|
+
});
|
|
282
|
+
test('findPending returns pending items only', () => {
|
|
283
|
+
store.save('apr-1', makeApprovalRecord({ approval_id: 'apr-1', status: 'pending', workspace_id: 'ws-1', tool_call_id: 'tc-1' }));
|
|
284
|
+
store.save('apr-2', makeApprovalRecord({ approval_id: 'apr-2', status: 'approved', workspace_id: 'ws-1', tool_call_id: 'tc-2' }));
|
|
285
|
+
store.save('apr-3', makeApprovalRecord({ approval_id: 'apr-3', status: 'pending', workspace_id: 'ws-2', tool_call_id: 'tc-3' }));
|
|
286
|
+
const pending = store.findPending();
|
|
287
|
+
expect(pending).toHaveLength(2);
|
|
288
|
+
expect(pending.every((a) => a.status === 'pending')).toBe(true);
|
|
289
|
+
});
|
|
290
|
+
test('findPending filters by workspace', () => {
|
|
291
|
+
store.save('apr-1', makeApprovalRecord({ approval_id: 'apr-1', status: 'pending', workspace_id: 'ws-1', tool_call_id: 'tc-1' }));
|
|
292
|
+
store.save('apr-2', makeApprovalRecord({ approval_id: 'apr-2', status: 'pending', workspace_id: 'ws-2', tool_call_id: 'tc-2' }));
|
|
293
|
+
store.save('apr-3', makeApprovalRecord({ approval_id: 'apr-3', status: 'pending', workspace_id: 'ws-1', tool_call_id: 'tc-3' }));
|
|
294
|
+
const ws1Pending = store.findPending('ws-1');
|
|
295
|
+
expect(ws1Pending).toHaveLength(2);
|
|
296
|
+
expect(ws1Pending.every((a) => a.workspace_id === 'ws-1')).toBe(true);
|
|
297
|
+
const ws2Pending = store.findPending('ws-2');
|
|
298
|
+
expect(ws2Pending).toHaveLength(1);
|
|
299
|
+
expect(ws2Pending[0].workspace_id).toBe('ws-2');
|
|
300
|
+
});
|
|
301
|
+
test('clear empties all data', () => {
|
|
302
|
+
store.save('apr-1', makeApprovalRecord({ approval_id: 'apr-1', status: 'pending', workspace_id: 'ws-1', tool_call_id: 'tc-1' }));
|
|
303
|
+
store.indexToken('tok-abc', 'apr-1');
|
|
304
|
+
mockQuery.mockClear();
|
|
305
|
+
store.clear();
|
|
306
|
+
expect(store.getById('apr-1')).toBeUndefined();
|
|
307
|
+
expect(store.getByToken('tok-abc')).toBeUndefined();
|
|
308
|
+
expect(store.findPending()).toHaveLength(0);
|
|
309
|
+
expect(mockQuery).toHaveBeenCalledWith('TRUNCATE approvals, approval_tokens', undefined);
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
// ===========================================================================
|
|
313
|
+
// PostgresIdempotencyStore
|
|
314
|
+
// ===========================================================================
|
|
315
|
+
describe('PostgresIdempotencyStore', () => {
|
|
316
|
+
let store;
|
|
317
|
+
beforeEach(() => {
|
|
318
|
+
jest.useFakeTimers();
|
|
319
|
+
jest.setSystemTime(new Date('2025-01-01T00:00:00.000Z'));
|
|
320
|
+
store = new postgres_1.PostgresIdempotencyStore(mockPool);
|
|
321
|
+
});
|
|
322
|
+
afterEach(() => {
|
|
323
|
+
jest.useRealTimers();
|
|
324
|
+
});
|
|
325
|
+
test('set and get', async () => {
|
|
326
|
+
const result = makeToolResult({ tool_call_id: 'tc-1' });
|
|
327
|
+
store.set('tc-1', result, 60000);
|
|
328
|
+
expect(await store.get('tc-1')).toEqual(result);
|
|
329
|
+
});
|
|
330
|
+
test('set fires async INSERT query', () => {
|
|
331
|
+
const result = makeToolResult({ tool_call_id: 'tc-1' });
|
|
332
|
+
store.set('tc-1', result, 60000);
|
|
333
|
+
expect(mockQuery).toHaveBeenCalledTimes(1);
|
|
334
|
+
const [sql, params] = mockQuery.mock.calls[0];
|
|
335
|
+
expect(sql).toContain('INSERT INTO idempotency_cache');
|
|
336
|
+
expect(sql).toContain('ON CONFLICT (tool_call_id) DO UPDATE');
|
|
337
|
+
expect(params[0]).toBe('tc-1');
|
|
338
|
+
expect(params[1]).toBe(JSON.stringify(result));
|
|
339
|
+
// Third param is the expiry ISO string
|
|
340
|
+
expect(typeof params[2]).toBe('string');
|
|
341
|
+
});
|
|
342
|
+
test('get returns undefined for missing entries', async () => {
|
|
343
|
+
expect(await store.get('nonexistent')).toBeUndefined();
|
|
344
|
+
});
|
|
345
|
+
test('get returns undefined for expired entries', async () => {
|
|
346
|
+
const result = makeToolResult({ tool_call_id: 'tc-1' });
|
|
347
|
+
store.set('tc-1', result, 5000); // 5 second TTL
|
|
348
|
+
// Still valid
|
|
349
|
+
jest.advanceTimersByTime(4999);
|
|
350
|
+
expect(await store.get('tc-1')).toEqual(result);
|
|
351
|
+
// Now expired
|
|
352
|
+
jest.advanceTimersByTime(2);
|
|
353
|
+
expect(await store.get('tc-1')).toBeUndefined();
|
|
354
|
+
});
|
|
355
|
+
test('expired entry triggers DELETE query', async () => {
|
|
356
|
+
store.set('tc-1', makeToolResult({ tool_call_id: 'tc-1' }), 5000);
|
|
357
|
+
mockQuery.mockClear();
|
|
358
|
+
jest.advanceTimersByTime(5001);
|
|
359
|
+
await store.get('tc-1');
|
|
360
|
+
// Should fire a DELETE for the expired entry
|
|
361
|
+
expect(mockQuery).toHaveBeenCalledTimes(1);
|
|
362
|
+
const [sql, params] = mockQuery.mock.calls[0];
|
|
363
|
+
expect(sql).toContain('DELETE FROM idempotency_cache');
|
|
364
|
+
expect(params).toEqual(['tc-1']);
|
|
365
|
+
});
|
|
366
|
+
test('has returns true for valid entries', async () => {
|
|
367
|
+
store.set('tc-1', makeToolResult({ tool_call_id: 'tc-1' }), 60000);
|
|
368
|
+
expect(await store.has('tc-1')).toBe(true);
|
|
369
|
+
});
|
|
370
|
+
test('has returns false for missing entries', async () => {
|
|
371
|
+
expect(await store.has('nonexistent')).toBe(false);
|
|
372
|
+
});
|
|
373
|
+
test('has returns false for expired entries', async () => {
|
|
374
|
+
store.set('tc-1', makeToolResult({ tool_call_id: 'tc-1' }), 5000);
|
|
375
|
+
jest.advanceTimersByTime(5001);
|
|
376
|
+
expect(await store.has('tc-1')).toBe(false);
|
|
377
|
+
});
|
|
378
|
+
test('clear empties cache and fires TRUNCATE', async () => {
|
|
379
|
+
store.set('tc-1', makeToolResult({ tool_call_id: 'tc-1' }), 60000);
|
|
380
|
+
store.set('tc-2', makeToolResult({ tool_call_id: 'tc-2' }), 60000);
|
|
381
|
+
mockQuery.mockClear();
|
|
382
|
+
store.clear();
|
|
383
|
+
expect(await store.get('tc-1')).toBeUndefined();
|
|
384
|
+
expect(await store.get('tc-2')).toBeUndefined();
|
|
385
|
+
expect(mockQuery).toHaveBeenCalledWith('TRUNCATE idempotency_cache', undefined);
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
// ===========================================================================
|
|
389
|
+
// PostgresRateLimitStore
|
|
390
|
+
// ===========================================================================
|
|
391
|
+
describe('PostgresRateLimitStore', () => {
|
|
392
|
+
let store;
|
|
393
|
+
beforeEach(() => {
|
|
394
|
+
jest.useFakeTimers();
|
|
395
|
+
jest.setSystemTime(new Date('2025-01-01T00:00:00.000Z'));
|
|
396
|
+
store = new postgres_1.PostgresRateLimitStore(mockPool);
|
|
397
|
+
});
|
|
398
|
+
afterEach(() => {
|
|
399
|
+
jest.useRealTimers();
|
|
400
|
+
});
|
|
401
|
+
test('hit records and counts correctly', () => {
|
|
402
|
+
const r1 = store.hit('actor:agent-1', 60000, 10);
|
|
403
|
+
expect(r1.allowed).toBe(true);
|
|
404
|
+
expect(r1.current).toBe(1);
|
|
405
|
+
expect(r1.limit).toBe(10);
|
|
406
|
+
const r2 = store.hit('actor:agent-1', 60000, 10);
|
|
407
|
+
expect(r2.allowed).toBe(true);
|
|
408
|
+
expect(r2.current).toBe(2);
|
|
409
|
+
});
|
|
410
|
+
test('hit fires async INSERT to persist hit', () => {
|
|
411
|
+
store.hit('actor:agent-1', 60000, 10);
|
|
412
|
+
// First hit also triggers a DELETE for old entries (cleanup for first hit in window)
|
|
413
|
+
const insertCalls = mockQuery.mock.calls.filter(([sql]) => sql.includes('INSERT INTO rate_limit_hits'));
|
|
414
|
+
expect(insertCalls.length).toBeGreaterThanOrEqual(1);
|
|
415
|
+
expect(insertCalls[0][1][0]).toBe('actor:agent-1');
|
|
416
|
+
});
|
|
417
|
+
test('respects maxRequests limit', () => {
|
|
418
|
+
const windowMs = 60000;
|
|
419
|
+
const maxRequests = 3;
|
|
420
|
+
const r1 = store.hit('actor:agent-1', windowMs, maxRequests);
|
|
421
|
+
expect(r1.allowed).toBe(true);
|
|
422
|
+
expect(r1.current).toBe(1);
|
|
423
|
+
const r2 = store.hit('actor:agent-1', windowMs, maxRequests);
|
|
424
|
+
expect(r2.allowed).toBe(true);
|
|
425
|
+
expect(r2.current).toBe(2);
|
|
426
|
+
const r3 = store.hit('actor:agent-1', windowMs, maxRequests);
|
|
427
|
+
expect(r3.allowed).toBe(true);
|
|
428
|
+
expect(r3.current).toBe(3);
|
|
429
|
+
// 4th request exceeds limit
|
|
430
|
+
const r4 = store.hit('actor:agent-1', windowMs, maxRequests);
|
|
431
|
+
expect(r4.allowed).toBe(false);
|
|
432
|
+
expect(r4.current).toBe(4);
|
|
433
|
+
expect(r4.limit).toBe(maxRequests);
|
|
434
|
+
});
|
|
435
|
+
test('window expires and allows new requests', () => {
|
|
436
|
+
const windowMs = 10000;
|
|
437
|
+
const maxRequests = 2;
|
|
438
|
+
store.hit('actor:agent-1', windowMs, maxRequests);
|
|
439
|
+
store.hit('actor:agent-1', windowMs, maxRequests);
|
|
440
|
+
// At limit
|
|
441
|
+
const r3 = store.hit('actor:agent-1', windowMs, maxRequests);
|
|
442
|
+
expect(r3.allowed).toBe(false);
|
|
443
|
+
// Advance past the window
|
|
444
|
+
jest.advanceTimersByTime(10001);
|
|
445
|
+
// Now the old hits should be pruned
|
|
446
|
+
const r4 = store.hit('actor:agent-1', windowMs, maxRequests);
|
|
447
|
+
expect(r4.allowed).toBe(true);
|
|
448
|
+
expect(r4.current).toBe(1);
|
|
449
|
+
});
|
|
450
|
+
test('different keys are independent', () => {
|
|
451
|
+
store.hit('actor:agent-1', 60000, 1);
|
|
452
|
+
const r1 = store.hit('actor:agent-1', 60000, 1);
|
|
453
|
+
expect(r1.allowed).toBe(false);
|
|
454
|
+
// Different key should be allowed
|
|
455
|
+
const r2 = store.hit('actor:agent-2', 60000, 1);
|
|
456
|
+
expect(r2.allowed).toBe(true);
|
|
457
|
+
});
|
|
458
|
+
test('reset clears all state and fires TRUNCATE', () => {
|
|
459
|
+
store.hit('actor:agent-1', 60000, 10);
|
|
460
|
+
store.hit('actor:agent-2', 60000, 10);
|
|
461
|
+
mockQuery.mockClear();
|
|
462
|
+
store.reset();
|
|
463
|
+
// After reset, counters should start fresh
|
|
464
|
+
const result = store.hit('actor:agent-1', 60000, 10);
|
|
465
|
+
expect(result.current).toBe(1);
|
|
466
|
+
// Verify TRUNCATE was called (first call after mockClear)
|
|
467
|
+
const truncateCalls = mockQuery.mock.calls.filter(([sql]) => sql.includes('TRUNCATE rate_limit_hits'));
|
|
468
|
+
expect(truncateCalls).toHaveLength(1);
|
|
469
|
+
});
|
|
470
|
+
test('hit returns correct resetAt timestamp', () => {
|
|
471
|
+
const now = Date.now();
|
|
472
|
+
const windowMs = 60000;
|
|
473
|
+
const result = store.hit('actor:agent-1', windowMs, 10);
|
|
474
|
+
// resetAt should be the first timestamp in window + windowMs
|
|
475
|
+
expect(result.resetAt).toBe(now + windowMs);
|
|
476
|
+
});
|
|
477
|
+
});
|
|
478
|
+
// ===========================================================================
|
|
479
|
+
// PostgresSubscriptionStore
|
|
480
|
+
// ===========================================================================
|
|
481
|
+
function makeSubscription(overrides = {}) {
|
|
482
|
+
return {
|
|
483
|
+
id: 'sub-001',
|
|
484
|
+
workspace_id: 'ws-001',
|
|
485
|
+
stripe_customer_id: 'cus_test123',
|
|
486
|
+
stripe_subscription_id: 'sub_test456',
|
|
487
|
+
plan: 'pro',
|
|
488
|
+
status: 'active',
|
|
489
|
+
current_period_start: '2026-01-01T00:00:00.000Z',
|
|
490
|
+
current_period_end: '2026-02-01T00:00:00.000Z',
|
|
491
|
+
cancel_at_period_end: false,
|
|
492
|
+
created_at: '2026-01-01T00:00:00.000Z',
|
|
493
|
+
updated_at: '2026-01-01T00:00:00.000Z',
|
|
494
|
+
...overrides,
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
describe('PostgresSubscriptionStore', () => {
|
|
498
|
+
let store;
|
|
499
|
+
beforeEach(() => {
|
|
500
|
+
store = new postgres_1.PostgresSubscriptionStore(mockPool);
|
|
501
|
+
});
|
|
502
|
+
test('create stores in cache and fires async INSERT', () => {
|
|
503
|
+
const sub = makeSubscription();
|
|
504
|
+
store.create(sub);
|
|
505
|
+
expect(store.getById('sub-001')).toMatchObject({ id: 'sub-001', plan: 'pro' });
|
|
506
|
+
expect(mockQuery).toHaveBeenCalledWith(expect.stringContaining('INSERT INTO subscriptions'), expect.arrayContaining(['sub-001', 'ws-001', 'cus_test123']));
|
|
507
|
+
});
|
|
508
|
+
test('getById returns undefined for missing entries', () => {
|
|
509
|
+
expect(store.getById('nonexistent')).toBeUndefined();
|
|
510
|
+
});
|
|
511
|
+
test('getByWorkspace returns subscription for workspace', () => {
|
|
512
|
+
store.create(makeSubscription());
|
|
513
|
+
expect(store.getByWorkspace('ws-001')).toMatchObject({ workspace_id: 'ws-001' });
|
|
514
|
+
expect(store.getByWorkspace('ws-other')).toBeUndefined();
|
|
515
|
+
});
|
|
516
|
+
test('getByStripeCustomerId returns subscription', () => {
|
|
517
|
+
store.create(makeSubscription());
|
|
518
|
+
expect(store.getByStripeCustomerId('cus_test123')).toMatchObject({ stripe_customer_id: 'cus_test123' });
|
|
519
|
+
expect(store.getByStripeCustomerId('cus_other')).toBeUndefined();
|
|
520
|
+
});
|
|
521
|
+
test('getByStripeSubscriptionId returns subscription', () => {
|
|
522
|
+
store.create(makeSubscription());
|
|
523
|
+
expect(store.getByStripeSubscriptionId('sub_test456')).toMatchObject({ stripe_subscription_id: 'sub_test456' });
|
|
524
|
+
expect(store.getByStripeSubscriptionId('sub_other')).toBeUndefined();
|
|
525
|
+
});
|
|
526
|
+
test('update modifies cache and fires async UPDATE', () => {
|
|
527
|
+
store.create(makeSubscription());
|
|
528
|
+
const result = store.update('sub-001', { plan: 'business', updated_at: '2026-02-01T00:00:00.000Z' });
|
|
529
|
+
expect(result?.plan).toBe('business');
|
|
530
|
+
expect(store.getById('sub-001')?.plan).toBe('business');
|
|
531
|
+
expect(mockQuery).toHaveBeenCalledWith(expect.stringContaining('UPDATE subscriptions SET'), expect.arrayContaining(['business']));
|
|
532
|
+
});
|
|
533
|
+
test('update returns undefined for missing entries', () => {
|
|
534
|
+
expect(store.update('nonexistent', { plan: 'free' })).toBeUndefined();
|
|
535
|
+
});
|
|
536
|
+
test('update rejects disallowed columns', () => {
|
|
537
|
+
store.create(makeSubscription());
|
|
538
|
+
// 'id' is not in ALLOWED_SUBSCRIPTION_COLUMNS — should be silently stripped
|
|
539
|
+
const result = store.update('sub-001', { id: 'hacked' });
|
|
540
|
+
expect(result?.id).toBe('sub-001'); // id unchanged
|
|
541
|
+
});
|
|
542
|
+
test('delete removes from cache and fires async DELETE', () => {
|
|
543
|
+
store.create(makeSubscription());
|
|
544
|
+
expect(store.delete('sub-001')).toBe(true);
|
|
545
|
+
expect(store.getById('sub-001')).toBeUndefined();
|
|
546
|
+
expect(mockQuery).toHaveBeenCalledWith(expect.stringContaining('DELETE FROM subscriptions'), ['sub-001']);
|
|
547
|
+
});
|
|
548
|
+
test('delete returns false for missing entries', () => {
|
|
549
|
+
expect(store.delete('nonexistent')).toBe(false);
|
|
550
|
+
});
|
|
551
|
+
test('list returns all subscriptions', () => {
|
|
552
|
+
store.create(makeSubscription({ id: 'sub-001' }));
|
|
553
|
+
store.create(makeSubscription({ id: 'sub-002', workspace_id: 'ws-002' }));
|
|
554
|
+
expect(store.list()).toHaveLength(2);
|
|
555
|
+
});
|
|
556
|
+
test('hydrate loads from database', async () => {
|
|
557
|
+
mockQuery.mockResolvedValueOnce({
|
|
558
|
+
rows: [
|
|
559
|
+
{
|
|
560
|
+
id: 'sub-pg-001', workspace_id: 'ws-pg', stripe_customer_id: 'cus_pg',
|
|
561
|
+
stripe_subscription_id: 'sub_pg', plan: 'business', status: 'active',
|
|
562
|
+
current_period_start: '2026-01-01', current_period_end: '2026-02-01',
|
|
563
|
+
cancel_at_period_end: false, created_at: '2026-01-01', updated_at: '2026-01-01',
|
|
564
|
+
},
|
|
565
|
+
],
|
|
566
|
+
});
|
|
567
|
+
await store.hydrate();
|
|
568
|
+
expect(store.getById('sub-pg-001')).toMatchObject({ plan: 'business' });
|
|
569
|
+
});
|
|
570
|
+
test('createTables creates table and indexes', async () => {
|
|
571
|
+
await postgres_1.PostgresSubscriptionStore.createTables(mockPool);
|
|
572
|
+
const allSql = mockQuery.mock.calls.map((c) => c[0]);
|
|
573
|
+
expect(allSql.some(s => s.includes('CREATE TABLE IF NOT EXISTS subscriptions'))).toBe(true);
|
|
574
|
+
expect(allSql.some(s => s.includes('idx_subscriptions_workspace'))).toBe(true);
|
|
575
|
+
expect(allSql.some(s => s.includes('idx_subscriptions_stripe_customer'))).toBe(true);
|
|
576
|
+
expect(allSql.some(s => s.includes('idx_subscriptions_stripe_subscription'))).toBe(true);
|
|
577
|
+
});
|
|
578
|
+
});
|
|
579
|
+
// ===========================================================================
|
|
580
|
+
// initPostgresStorage
|
|
581
|
+
// ===========================================================================
|
|
582
|
+
describe('initPostgresStorage', () => {
|
|
583
|
+
test('calls CREATE TABLE IF NOT EXISTS for all tables', async () => {
|
|
584
|
+
// Each store's createTables uses pool.query for DDL
|
|
585
|
+
await (0, postgres_1.initPostgresStorage)(mockPool);
|
|
586
|
+
const allSql = mockQuery.mock.calls.map(([sql]) => sql);
|
|
587
|
+
// PostgresBudgetStore creates 3 tables
|
|
588
|
+
expect(allSql.some((s) => s.includes('CREATE TABLE IF NOT EXISTS budget_task_states'))).toBe(true);
|
|
589
|
+
expect(allSql.some((s) => s.includes('CREATE TABLE IF NOT EXISTS budget_counters'))).toBe(true);
|
|
590
|
+
expect(allSql.some((s) => s.includes('CREATE TABLE IF NOT EXISTS budget_retry_counts'))).toBe(true);
|
|
591
|
+
// PostgresAuditStore creates 1 table
|
|
592
|
+
expect(allSql.some((s) => s.includes('CREATE TABLE IF NOT EXISTS audit_events'))).toBe(true);
|
|
593
|
+
// PostgresApprovalStore creates 2 tables
|
|
594
|
+
expect(allSql.some((s) => s.includes('CREATE TABLE IF NOT EXISTS approvals'))).toBe(true);
|
|
595
|
+
expect(allSql.some((s) => s.includes('CREATE TABLE IF NOT EXISTS approval_tokens'))).toBe(true);
|
|
596
|
+
// PostgresIdempotencyStore creates 1 table
|
|
597
|
+
expect(allSql.some((s) => s.includes('CREATE TABLE IF NOT EXISTS idempotency_cache'))).toBe(true);
|
|
598
|
+
// PostgresRateLimitStore creates 1 table + 1 index
|
|
599
|
+
expect(allSql.some((s) => s.includes('CREATE TABLE IF NOT EXISTS rate_limit_hits'))).toBe(true);
|
|
600
|
+
expect(allSql.some((s) => s.includes('CREATE INDEX IF NOT EXISTS idx_rate_limit_hits_key_ts'))).toBe(true);
|
|
601
|
+
});
|
|
602
|
+
test('calls pool.query the expected number of times', async () => {
|
|
603
|
+
await (0, postgres_1.initPostgresStorage)(mockPool);
|
|
604
|
+
// Budget: 3 tables, Audit: 1 table, Approval: 2 tables, Idempotency: 1 table,
|
|
605
|
+
// RateLimit: 1 table + 1 index = 9 core queries
|
|
606
|
+
// SaaS: Users: 1 + 1 migration, OAuthAccounts: 1, Workspaces: 1, WorkspaceMembers: 1,
|
|
607
|
+
// Sessions: 1 + 1 index, UserApiKeys: 1 + 2 indexes + 1 migration (tags) = 11 SaaS queries
|
|
608
|
+
// Subscriptions: 1 table + 3 indexes = 4 queries
|
|
609
|
+
// Per-workspace config: RateLimitConfig: 1 table, BudgetConfig: 1 table, PolicyStore: 1 table = 3 queries
|
|
610
|
+
// Total: 27
|
|
611
|
+
expect(mockQuery).toHaveBeenCalledTimes(27);
|
|
612
|
+
});
|
|
613
|
+
});
|
|
614
|
+
//# sourceMappingURL=postgres-storage.test.js.map
|