@useatlas/create 0.0.1
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/README.md +231 -0
- package/index.ts +829 -0
- package/package.json +38 -0
- package/templates/docker/.env.example +67 -0
- package/templates/docker/Dockerfile +52 -0
- package/templates/docker/bin/__tests__/benchmark.test.ts +598 -0
- package/templates/docker/bin/__tests__/duckdb-ingest.test.ts +171 -0
- package/templates/docker/bin/__tests__/eval.test.ts +434 -0
- package/templates/docker/bin/__tests__/matview-partition.test.ts +615 -0
- package/templates/docker/bin/__tests__/multi-source.test.ts +113 -0
- package/templates/docker/bin/__tests__/plugin-cli.test.ts +322 -0
- package/templates/docker/bin/__tests__/profiler-heuristics.test.ts +608 -0
- package/templates/docker/bin/__tests__/query.test.ts +240 -0
- package/templates/docker/bin/__tests__/schema-drift.test.ts +542 -0
- package/templates/docker/bin/__tests__/view-yaml-generation.test.ts +146 -0
- package/templates/docker/bin/atlas.ts +5044 -0
- package/templates/docker/bin/benchmark.ts +695 -0
- package/templates/docker/bin/enrich.ts +559 -0
- package/templates/docker/bin/eval.ts +770 -0
- package/templates/docker/bin/smoke.ts +438 -0
- package/templates/docker/data/.gitkeep +0 -0
- package/templates/docker/data/cybersec.sql +1961 -0
- package/templates/docker/data/demo-semantic/catalog.yml +40 -0
- package/templates/docker/data/demo-semantic/entities/accounts.yml +170 -0
- package/templates/docker/data/demo-semantic/entities/companies.yml +207 -0
- package/templates/docker/data/demo-semantic/entities/people.yml +145 -0
- package/templates/docker/data/demo-semantic/glossary.yml +22 -0
- package/templates/docker/data/demo-semantic/metrics/accounts.yml +38 -0
- package/templates/docker/data/demo-semantic/metrics/companies.yml +89 -0
- package/templates/docker/data/demo.sql +373 -0
- package/templates/docker/data/ecommerce.sql +1690 -0
- package/templates/docker/data/init-demo-db.sql +8 -0
- package/templates/docker/docker-compose.yml +34 -0
- package/templates/docker/docs/deploy.md +390 -0
- package/templates/docker/eslint.config.mjs +18 -0
- package/templates/docker/gitignore +5 -0
- package/templates/docker/next.config.ts +9 -0
- package/templates/docker/package.json +59 -0
- package/templates/docker/postcss.config.mjs +8 -0
- package/templates/docker/public/.gitkeep +0 -0
- package/templates/docker/public/favicon.svg +4 -0
- package/templates/docker/railway.json +13 -0
- package/templates/docker/render.yaml +34 -0
- package/templates/docker/semantic/catalog.yml +5 -0
- package/templates/docker/semantic/entities/.gitkeep +0 -0
- package/templates/docker/semantic/glossary.yml +6 -0
- package/templates/docker/semantic/metrics/.gitkeep +0 -0
- package/templates/docker/sidecar/Dockerfile +28 -0
- package/templates/docker/sidecar/railway.json +14 -0
- package/templates/docker/sidecar/server.ts +188 -0
- package/templates/docker/src/api/__tests__/actions.test.ts +683 -0
- package/templates/docker/src/api/__tests__/admin.test.ts +820 -0
- package/templates/docker/src/api/__tests__/auth.test.ts +165 -0
- package/templates/docker/src/api/__tests__/chat.test.ts +376 -0
- package/templates/docker/src/api/__tests__/conversations.test.ts +555 -0
- package/templates/docker/src/api/__tests__/cors.test.ts +135 -0
- package/templates/docker/src/api/__tests__/health-plugin.test.ts +169 -0
- package/templates/docker/src/api/__tests__/health.test.ts +261 -0
- package/templates/docker/src/api/__tests__/query.test.ts +891 -0
- package/templates/docker/src/api/__tests__/scheduled-tasks.test.ts +601 -0
- package/templates/docker/src/api/__tests__/slack.test.ts +847 -0
- package/templates/docker/src/api/index.ts +117 -0
- package/templates/docker/src/api/routes/actions.ts +274 -0
- package/templates/docker/src/api/routes/admin.ts +757 -0
- package/templates/docker/src/api/routes/auth.ts +48 -0
- package/templates/docker/src/api/routes/chat.ts +465 -0
- package/templates/docker/src/api/routes/conversations.ts +266 -0
- package/templates/docker/src/api/routes/health.ts +287 -0
- package/templates/docker/src/api/routes/openapi.ts +390 -0
- package/templates/docker/src/api/routes/query.ts +318 -0
- package/templates/docker/src/api/routes/scheduled-tasks.ts +467 -0
- package/templates/docker/src/api/routes/slack.ts +611 -0
- package/templates/docker/src/api/server.ts +226 -0
- package/templates/docker/src/app/api/[...route]/route.ts +33 -0
- package/templates/docker/src/app/error.tsx +24 -0
- package/templates/docker/src/app/globals.css +126 -0
- package/templates/docker/src/app/layout.tsx +19 -0
- package/templates/docker/src/app/page.tsx +14 -0
- package/templates/docker/src/global.d.ts +1 -0
- package/templates/docker/src/lib/__tests__/agent-cache.test.ts +437 -0
- package/templates/docker/src/lib/__tests__/agent-dialect.test.ts +114 -0
- package/templates/docker/src/lib/__tests__/agent-health-annotations.test.ts +164 -0
- package/templates/docker/src/lib/__tests__/agent-integration.test.ts +514 -0
- package/templates/docker/src/lib/__tests__/config-actions.test.ts +166 -0
- package/templates/docker/src/lib/__tests__/config.test.ts +1063 -0
- package/templates/docker/src/lib/__tests__/conversations.test.ts +589 -0
- package/templates/docker/src/lib/__tests__/errors.test.ts +256 -0
- package/templates/docker/src/lib/__tests__/logger.test.ts +200 -0
- package/templates/docker/src/lib/__tests__/providers.test.ts +99 -0
- package/templates/docker/src/lib/__tests__/rls.test.ts +435 -0
- package/templates/docker/src/lib/__tests__/scheduled-task-types.test.ts +124 -0
- package/templates/docker/src/lib/__tests__/scheduled-tasks.test.ts +550 -0
- package/templates/docker/src/lib/__tests__/semantic-index.test.ts +547 -0
- package/templates/docker/src/lib/__tests__/semantic-multisource.test.ts +544 -0
- package/templates/docker/src/lib/__tests__/semantic.test.ts +363 -0
- package/templates/docker/src/lib/__tests__/startup-actions.test.ts +452 -0
- package/templates/docker/src/lib/__tests__/startup.test.ts +465 -0
- package/templates/docker/src/lib/__tests__/tracing.test.ts +28 -0
- package/templates/docker/src/lib/action-types.ts +95 -0
- package/templates/docker/src/lib/agent-query.ts +178 -0
- package/templates/docker/src/lib/agent.ts +505 -0
- package/templates/docker/src/lib/api-url.ts +2 -0
- package/templates/docker/src/lib/auth/__tests__/audit.test.ts +418 -0
- package/templates/docker/src/lib/auth/__tests__/byot-integration.test.ts +222 -0
- package/templates/docker/src/lib/auth/__tests__/byot.test.ts +366 -0
- package/templates/docker/src/lib/auth/__tests__/detect.test.ts +190 -0
- package/templates/docker/src/lib/auth/__tests__/managed.test.ts +173 -0
- package/templates/docker/src/lib/auth/__tests__/middleware.test.ts +456 -0
- package/templates/docker/src/lib/auth/__tests__/migrate.test.ts +201 -0
- package/templates/docker/src/lib/auth/__tests__/permissions.test.ts +225 -0
- package/templates/docker/src/lib/auth/__tests__/server.test.ts +34 -0
- package/templates/docker/src/lib/auth/__tests__/simple-key.test.ts +176 -0
- package/templates/docker/src/lib/auth/__tests__/types.test.ts +44 -0
- package/templates/docker/src/lib/auth/audit.ts +89 -0
- package/templates/docker/src/lib/auth/byot.ts +158 -0
- package/templates/docker/src/lib/auth/client.ts +35 -0
- package/templates/docker/src/lib/auth/detect.ts +83 -0
- package/templates/docker/src/lib/auth/managed.ts +73 -0
- package/templates/docker/src/lib/auth/middleware.ts +208 -0
- package/templates/docker/src/lib/auth/migrate.ts +111 -0
- package/templates/docker/src/lib/auth/permissions.ts +156 -0
- package/templates/docker/src/lib/auth/server.ts +142 -0
- package/templates/docker/src/lib/auth/simple-key.ts +92 -0
- package/templates/docker/src/lib/auth/types.ts +49 -0
- package/templates/docker/src/lib/config.ts +704 -0
- package/templates/docker/src/lib/conversation-types.ts +29 -0
- package/templates/docker/src/lib/conversations.ts +270 -0
- package/templates/docker/src/lib/db/__tests__/connection.test.ts +69 -0
- package/templates/docker/src/lib/db/__tests__/duckdb.test.ts +141 -0
- package/templates/docker/src/lib/db/__tests__/internal.test.ts +387 -0
- package/templates/docker/src/lib/db/__tests__/registry-health.test.ts +207 -0
- package/templates/docker/src/lib/db/__tests__/registry-pool-limits.test.ts +156 -0
- package/templates/docker/src/lib/db/__tests__/registry.test.ts +595 -0
- package/templates/docker/src/lib/db/__tests__/salesforce.test.ts +339 -0
- package/templates/docker/src/lib/db/__tests__/snowflake.test.ts +217 -0
- package/templates/docker/src/lib/db/__tests__/source-rate-limit.test.ts +130 -0
- package/templates/docker/src/lib/db/connection.ts +753 -0
- package/templates/docker/src/lib/db/duckdb.ts +122 -0
- package/templates/docker/src/lib/db/internal.ts +273 -0
- package/templates/docker/src/lib/db/salesforce.ts +342 -0
- package/templates/docker/src/lib/db/source-rate-limit.ts +191 -0
- package/templates/docker/src/lib/errors.ts +154 -0
- package/templates/docker/src/lib/logger.ts +98 -0
- package/templates/docker/src/lib/plugins/__tests__/hooks-integration.test.ts +202 -0
- package/templates/docker/src/lib/plugins/__tests__/hooks.test.ts +529 -0
- package/templates/docker/src/lib/plugins/__tests__/migrate.test.ts +521 -0
- package/templates/docker/src/lib/plugins/__tests__/registry.test.ts +346 -0
- package/templates/docker/src/lib/plugins/__tests__/tools.test.ts +49 -0
- package/templates/docker/src/lib/plugins/__tests__/wiring.test.ts +585 -0
- package/templates/docker/src/lib/plugins/hooks.ts +162 -0
- package/templates/docker/src/lib/plugins/index.ts +9 -0
- package/templates/docker/src/lib/plugins/migrate.ts +309 -0
- package/templates/docker/src/lib/plugins/registry.ts +231 -0
- package/templates/docker/src/lib/plugins/tools.ts +39 -0
- package/templates/docker/src/lib/plugins/wiring.ts +291 -0
- package/templates/docker/src/lib/providers.ts +102 -0
- package/templates/docker/src/lib/rls.ts +321 -0
- package/templates/docker/src/lib/scheduled-task-types.ts +132 -0
- package/templates/docker/src/lib/scheduled-tasks.ts +475 -0
- package/templates/docker/src/lib/scheduler/__tests__/delivery.test.ts +192 -0
- package/templates/docker/src/lib/scheduler/__tests__/engine.test.ts +248 -0
- package/templates/docker/src/lib/scheduler/__tests__/format-email.test.ts +96 -0
- package/templates/docker/src/lib/scheduler/__tests__/format-slack.test.ts +78 -0
- package/templates/docker/src/lib/scheduler/__tests__/format-webhook.test.ts +78 -0
- package/templates/docker/src/lib/scheduler/delivery.ts +248 -0
- package/templates/docker/src/lib/scheduler/engine.ts +317 -0
- package/templates/docker/src/lib/scheduler/executor.ts +73 -0
- package/templates/docker/src/lib/scheduler/format-email.ts +109 -0
- package/templates/docker/src/lib/scheduler/format-slack.ts +35 -0
- package/templates/docker/src/lib/scheduler/format-webhook.ts +37 -0
- package/templates/docker/src/lib/scheduler/index.ts +7 -0
- package/templates/docker/src/lib/security.ts +11 -0
- package/templates/docker/src/lib/semantic-index.ts +503 -0
- package/templates/docker/src/lib/semantic.ts +387 -0
- package/templates/docker/src/lib/sidecar-types.ts +16 -0
- package/templates/docker/src/lib/slack/__tests__/api.test.ts +160 -0
- package/templates/docker/src/lib/slack/__tests__/format.test.ts +237 -0
- package/templates/docker/src/lib/slack/__tests__/store.test.ts +188 -0
- package/templates/docker/src/lib/slack/__tests__/threads.test.ts +112 -0
- package/templates/docker/src/lib/slack/__tests__/verify.test.ts +111 -0
- package/templates/docker/src/lib/slack/api.ts +102 -0
- package/templates/docker/src/lib/slack/format.ts +209 -0
- package/templates/docker/src/lib/slack/store.ts +107 -0
- package/templates/docker/src/lib/slack/threads.ts +64 -0
- package/templates/docker/src/lib/slack/verify.ts +71 -0
- package/templates/docker/src/lib/startup.ts +730 -0
- package/templates/docker/src/lib/tools/__tests__/action-permissions.test.ts +594 -0
- package/templates/docker/src/lib/tools/__tests__/custom-validation.test.ts +238 -0
- package/templates/docker/src/lib/tools/__tests__/explore-backend.test.ts +267 -0
- package/templates/docker/src/lib/tools/__tests__/explore-nsjail.test.ts +492 -0
- package/templates/docker/src/lib/tools/__tests__/explore-plugin.test.ts +374 -0
- package/templates/docker/src/lib/tools/__tests__/explore-sdk-compat.test.ts +82 -0
- package/templates/docker/src/lib/tools/__tests__/explore-sidecar.test.ts +208 -0
- package/templates/docker/src/lib/tools/__tests__/registry-actions.test.ts +144 -0
- package/templates/docker/src/lib/tools/__tests__/registry.test.ts +235 -0
- package/templates/docker/src/lib/tools/__tests__/salesforce-tool.test.ts +154 -0
- package/templates/docker/src/lib/tools/__tests__/soql-validation.test.ts +303 -0
- package/templates/docker/src/lib/tools/__tests__/sql-audit.test.ts +225 -0
- package/templates/docker/src/lib/tools/__tests__/sql-connection-whitelist.test.ts +98 -0
- package/templates/docker/src/lib/tools/__tests__/sql-duckdb.test.ts +233 -0
- package/templates/docker/src/lib/tools/__tests__/sql-ratelimit.test.ts +225 -0
- package/templates/docker/src/lib/tools/__tests__/sql.test.ts +1012 -0
- package/templates/docker/src/lib/tools/actions/__tests__/audit.test.ts +211 -0
- package/templates/docker/src/lib/tools/actions/__tests__/email.test.ts +378 -0
- package/templates/docker/src/lib/tools/actions/__tests__/handler.test.ts +681 -0
- package/templates/docker/src/lib/tools/actions/__tests__/jira.test.ts +427 -0
- package/templates/docker/src/lib/tools/actions/audit.ts +47 -0
- package/templates/docker/src/lib/tools/actions/email.ts +191 -0
- package/templates/docker/src/lib/tools/actions/handler.ts +591 -0
- package/templates/docker/src/lib/tools/actions/index.ts +23 -0
- package/templates/docker/src/lib/tools/actions/jira.ts +220 -0
- package/templates/docker/src/lib/tools/explore-nsjail.ts +343 -0
- package/templates/docker/src/lib/tools/explore-sandbox.ts +264 -0
- package/templates/docker/src/lib/tools/explore-sidecar.ts +163 -0
- package/templates/docker/src/lib/tools/explore.ts +379 -0
- package/templates/docker/src/lib/tools/registry.ts +221 -0
- package/templates/docker/src/lib/tools/salesforce.ts +138 -0
- package/templates/docker/src/lib/tools/soql-validation.ts +172 -0
- package/templates/docker/src/lib/tools/sql.ts +680 -0
- package/templates/docker/src/lib/tracing.ts +40 -0
- package/templates/docker/src/lib/utils.ts +6 -0
- package/templates/docker/src/test-setup.ts +38 -0
- package/templates/docker/src/types/vercel-sandbox.d.ts +54 -0
- package/templates/docker/src/ui/components/actions/action-approval-card.tsx +295 -0
- package/templates/docker/src/ui/components/actions/action-status-badge.tsx +50 -0
- package/templates/docker/src/ui/components/admin/admin-layout.tsx +26 -0
- package/templates/docker/src/ui/components/admin/admin-sidebar.tsx +96 -0
- package/templates/docker/src/ui/components/admin/empty-state.tsx +24 -0
- package/templates/docker/src/ui/components/admin/entity-detail.tsx +233 -0
- package/templates/docker/src/ui/components/admin/entity-list.tsx +96 -0
- package/templates/docker/src/ui/components/admin/error-banner.tsx +22 -0
- package/templates/docker/src/ui/components/admin/feature-disabled.tsx +44 -0
- package/templates/docker/src/ui/components/admin/health-badge.tsx +30 -0
- package/templates/docker/src/ui/components/admin/loading-state.tsx +14 -0
- package/templates/docker/src/ui/components/admin/stat-card.tsx +32 -0
- package/templates/docker/src/ui/components/atlas-chat.tsx +370 -0
- package/templates/docker/src/ui/components/chart/chart-detection.ts +261 -0
- package/templates/docker/src/ui/components/chart/result-chart.tsx +375 -0
- package/templates/docker/src/ui/components/chat/api-key-bar.tsx +66 -0
- package/templates/docker/src/ui/components/chat/copy-button.tsx +25 -0
- package/templates/docker/src/ui/components/chat/data-table.tsx +102 -0
- package/templates/docker/src/ui/components/chat/error-banner.tsx +32 -0
- package/templates/docker/src/ui/components/chat/explore-card.tsx +41 -0
- package/templates/docker/src/ui/components/chat/loading-card.tsx +10 -0
- package/templates/docker/src/ui/components/chat/managed-auth-card.tsx +116 -0
- package/templates/docker/src/ui/components/chat/markdown.tsx +72 -0
- package/templates/docker/src/ui/components/chat/sql-block.tsx +30 -0
- package/templates/docker/src/ui/components/chat/sql-result-card.tsx +144 -0
- package/templates/docker/src/ui/components/chat/starter-prompts.ts +6 -0
- package/templates/docker/src/ui/components/chat/tool-part.tsx +40 -0
- package/templates/docker/src/ui/components/chat/typing-indicator.tsx +19 -0
- package/templates/docker/src/ui/components/conversations/conversation-item.tsx +120 -0
- package/templates/docker/src/ui/components/conversations/conversation-list.tsx +66 -0
- package/templates/docker/src/ui/components/conversations/conversation-sidebar.tsx +78 -0
- package/templates/docker/src/ui/components/conversations/delete-confirmation.tsx +27 -0
- package/templates/docker/src/ui/context.tsx +78 -0
- package/templates/docker/src/ui/hooks/use-admin-fetch.ts +104 -0
- package/templates/docker/src/ui/hooks/use-conversations.ts +184 -0
- package/templates/docker/src/ui/hooks/use-dark-mode.ts +17 -0
- package/templates/docker/src/ui/lib/action-types.ts +63 -0
- package/templates/docker/src/ui/lib/helpers.ts +104 -0
- package/templates/docker/src/ui/lib/types.ts +145 -0
- package/templates/docker/tsconfig.json +41 -0
- package/templates/docker/vercel.json +3 -0
- package/templates/nextjs-standalone/.env.example +68 -0
- package/templates/nextjs-standalone/bin/__tests__/benchmark.test.ts +598 -0
- package/templates/nextjs-standalone/bin/__tests__/duckdb-ingest.test.ts +171 -0
- package/templates/nextjs-standalone/bin/__tests__/eval.test.ts +434 -0
- package/templates/nextjs-standalone/bin/__tests__/matview-partition.test.ts +615 -0
- package/templates/nextjs-standalone/bin/__tests__/multi-source.test.ts +113 -0
- package/templates/nextjs-standalone/bin/__tests__/plugin-cli.test.ts +322 -0
- package/templates/nextjs-standalone/bin/__tests__/profiler-heuristics.test.ts +608 -0
- package/templates/nextjs-standalone/bin/__tests__/query.test.ts +240 -0
- package/templates/nextjs-standalone/bin/__tests__/schema-drift.test.ts +542 -0
- package/templates/nextjs-standalone/bin/__tests__/view-yaml-generation.test.ts +146 -0
- package/templates/nextjs-standalone/bin/atlas.ts +5044 -0
- package/templates/nextjs-standalone/bin/benchmark.ts +695 -0
- package/templates/nextjs-standalone/bin/enrich.ts +559 -0
- package/templates/nextjs-standalone/bin/eval.ts +770 -0
- package/templates/nextjs-standalone/bin/smoke.ts +438 -0
- package/templates/nextjs-standalone/data/.gitkeep +0 -0
- package/templates/nextjs-standalone/data/cybersec.sql +1961 -0
- package/templates/nextjs-standalone/data/demo-semantic/catalog.yml +40 -0
- package/templates/nextjs-standalone/data/demo-semantic/entities/accounts.yml +170 -0
- package/templates/nextjs-standalone/data/demo-semantic/entities/companies.yml +207 -0
- package/templates/nextjs-standalone/data/demo-semantic/entities/people.yml +145 -0
- package/templates/nextjs-standalone/data/demo-semantic/glossary.yml +22 -0
- package/templates/nextjs-standalone/data/demo-semantic/metrics/accounts.yml +38 -0
- package/templates/nextjs-standalone/data/demo-semantic/metrics/companies.yml +89 -0
- package/templates/nextjs-standalone/data/demo.sql +373 -0
- package/templates/nextjs-standalone/data/ecommerce.sql +1690 -0
- package/templates/nextjs-standalone/data/init-demo-db.sql +8 -0
- package/templates/nextjs-standalone/docs/deploy.md +390 -0
- package/templates/nextjs-standalone/eslint.config.mjs +18 -0
- package/templates/nextjs-standalone/gitignore +5 -0
- package/templates/nextjs-standalone/next.config.ts +10 -0
- package/templates/nextjs-standalone/package.json +63 -0
- package/templates/nextjs-standalone/postcss.config.mjs +8 -0
- package/templates/nextjs-standalone/semantic/catalog.yml +5 -0
- package/templates/nextjs-standalone/semantic/entities/.gitkeep +0 -0
- package/templates/nextjs-standalone/semantic/glossary.yml +6 -0
- package/templates/nextjs-standalone/semantic/metrics/.gitkeep +0 -0
- package/templates/nextjs-standalone/src/api/__tests__/actions.test.ts +683 -0
- package/templates/nextjs-standalone/src/api/__tests__/admin.test.ts +820 -0
- package/templates/nextjs-standalone/src/api/__tests__/auth.test.ts +165 -0
- package/templates/nextjs-standalone/src/api/__tests__/chat.test.ts +376 -0
- package/templates/nextjs-standalone/src/api/__tests__/conversations.test.ts +555 -0
- package/templates/nextjs-standalone/src/api/__tests__/cors.test.ts +135 -0
- package/templates/nextjs-standalone/src/api/__tests__/health-plugin.test.ts +169 -0
- package/templates/nextjs-standalone/src/api/__tests__/health.test.ts +261 -0
- package/templates/nextjs-standalone/src/api/__tests__/query.test.ts +891 -0
- package/templates/nextjs-standalone/src/api/__tests__/scheduled-tasks.test.ts +601 -0
- package/templates/nextjs-standalone/src/api/__tests__/slack.test.ts +847 -0
- package/templates/nextjs-standalone/src/api/index.ts +117 -0
- package/templates/nextjs-standalone/src/api/routes/actions.ts +274 -0
- package/templates/nextjs-standalone/src/api/routes/admin.ts +757 -0
- package/templates/nextjs-standalone/src/api/routes/auth.ts +48 -0
- package/templates/nextjs-standalone/src/api/routes/chat.ts +465 -0
- package/templates/nextjs-standalone/src/api/routes/conversations.ts +266 -0
- package/templates/nextjs-standalone/src/api/routes/health.ts +287 -0
- package/templates/nextjs-standalone/src/api/routes/openapi.ts +390 -0
- package/templates/nextjs-standalone/src/api/routes/query.ts +318 -0
- package/templates/nextjs-standalone/src/api/routes/scheduled-tasks.ts +467 -0
- package/templates/nextjs-standalone/src/api/routes/slack.ts +611 -0
- package/templates/nextjs-standalone/src/api/server.ts +226 -0
- package/templates/nextjs-standalone/src/app/api/[...route]/route.ts +33 -0
- package/templates/nextjs-standalone/src/app/error.tsx +24 -0
- package/templates/nextjs-standalone/src/app/global-error.tsx +68 -0
- package/templates/nextjs-standalone/src/app/globals.css +126 -0
- package/templates/nextjs-standalone/src/app/layout.tsx +19 -0
- package/templates/nextjs-standalone/src/app/page.tsx +14 -0
- package/templates/nextjs-standalone/src/lib/__tests__/agent-cache.test.ts +437 -0
- package/templates/nextjs-standalone/src/lib/__tests__/agent-dialect.test.ts +114 -0
- package/templates/nextjs-standalone/src/lib/__tests__/agent-health-annotations.test.ts +164 -0
- package/templates/nextjs-standalone/src/lib/__tests__/agent-integration.test.ts +514 -0
- package/templates/nextjs-standalone/src/lib/__tests__/config-actions.test.ts +166 -0
- package/templates/nextjs-standalone/src/lib/__tests__/config.test.ts +1063 -0
- package/templates/nextjs-standalone/src/lib/__tests__/conversations.test.ts +589 -0
- package/templates/nextjs-standalone/src/lib/__tests__/errors.test.ts +256 -0
- package/templates/nextjs-standalone/src/lib/__tests__/logger.test.ts +200 -0
- package/templates/nextjs-standalone/src/lib/__tests__/providers.test.ts +99 -0
- package/templates/nextjs-standalone/src/lib/__tests__/rls.test.ts +435 -0
- package/templates/nextjs-standalone/src/lib/__tests__/scheduled-task-types.test.ts +124 -0
- package/templates/nextjs-standalone/src/lib/__tests__/scheduled-tasks.test.ts +550 -0
- package/templates/nextjs-standalone/src/lib/__tests__/semantic-index.test.ts +547 -0
- package/templates/nextjs-standalone/src/lib/__tests__/semantic-multisource.test.ts +544 -0
- package/templates/nextjs-standalone/src/lib/__tests__/semantic.test.ts +363 -0
- package/templates/nextjs-standalone/src/lib/__tests__/startup-actions.test.ts +452 -0
- package/templates/nextjs-standalone/src/lib/__tests__/startup.test.ts +465 -0
- package/templates/nextjs-standalone/src/lib/__tests__/tracing.test.ts +28 -0
- package/templates/nextjs-standalone/src/lib/action-types.ts +95 -0
- package/templates/nextjs-standalone/src/lib/agent-query.ts +178 -0
- package/templates/nextjs-standalone/src/lib/agent.ts +505 -0
- package/templates/nextjs-standalone/src/lib/api-url.ts +3 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/audit.test.ts +418 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/byot-integration.test.ts +222 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/byot.test.ts +366 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/detect.test.ts +190 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/managed.test.ts +173 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/middleware.test.ts +456 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/migrate.test.ts +201 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/permissions.test.ts +225 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/server.test.ts +34 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/simple-key.test.ts +176 -0
- package/templates/nextjs-standalone/src/lib/auth/__tests__/types.test.ts +44 -0
- package/templates/nextjs-standalone/src/lib/auth/audit.ts +89 -0
- package/templates/nextjs-standalone/src/lib/auth/byot.ts +158 -0
- package/templates/nextjs-standalone/src/lib/auth/client.ts +23 -0
- package/templates/nextjs-standalone/src/lib/auth/detect.ts +83 -0
- package/templates/nextjs-standalone/src/lib/auth/managed.ts +73 -0
- package/templates/nextjs-standalone/src/lib/auth/middleware.ts +208 -0
- package/templates/nextjs-standalone/src/lib/auth/migrate.ts +111 -0
- package/templates/nextjs-standalone/src/lib/auth/permissions.ts +156 -0
- package/templates/nextjs-standalone/src/lib/auth/server.ts +142 -0
- package/templates/nextjs-standalone/src/lib/auth/simple-key.ts +92 -0
- package/templates/nextjs-standalone/src/lib/auth/types.ts +49 -0
- package/templates/nextjs-standalone/src/lib/config.ts +704 -0
- package/templates/nextjs-standalone/src/lib/conversation-types.ts +29 -0
- package/templates/nextjs-standalone/src/lib/conversations.ts +270 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/connection.test.ts +69 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/duckdb.test.ts +141 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/internal.test.ts +387 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/registry-health.test.ts +207 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/registry-pool-limits.test.ts +156 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/registry.test.ts +595 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/salesforce.test.ts +339 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/snowflake.test.ts +217 -0
- package/templates/nextjs-standalone/src/lib/db/__tests__/source-rate-limit.test.ts +130 -0
- package/templates/nextjs-standalone/src/lib/db/connection.ts +753 -0
- package/templates/nextjs-standalone/src/lib/db/duckdb.ts +122 -0
- package/templates/nextjs-standalone/src/lib/db/internal.ts +273 -0
- package/templates/nextjs-standalone/src/lib/db/salesforce.ts +342 -0
- package/templates/nextjs-standalone/src/lib/db/source-rate-limit.ts +191 -0
- package/templates/nextjs-standalone/src/lib/errors.ts +154 -0
- package/templates/nextjs-standalone/src/lib/logger.ts +98 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/hooks-integration.test.ts +202 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/hooks.test.ts +529 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/migrate.test.ts +521 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/registry.test.ts +346 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/tools.test.ts +49 -0
- package/templates/nextjs-standalone/src/lib/plugins/__tests__/wiring.test.ts +585 -0
- package/templates/nextjs-standalone/src/lib/plugins/hooks.ts +162 -0
- package/templates/nextjs-standalone/src/lib/plugins/index.ts +9 -0
- package/templates/nextjs-standalone/src/lib/plugins/migrate.ts +309 -0
- package/templates/nextjs-standalone/src/lib/plugins/registry.ts +231 -0
- package/templates/nextjs-standalone/src/lib/plugins/tools.ts +39 -0
- package/templates/nextjs-standalone/src/lib/plugins/wiring.ts +291 -0
- package/templates/nextjs-standalone/src/lib/providers.ts +102 -0
- package/templates/nextjs-standalone/src/lib/rls.ts +321 -0
- package/templates/nextjs-standalone/src/lib/scheduled-task-types.ts +132 -0
- package/templates/nextjs-standalone/src/lib/scheduled-tasks.ts +475 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/delivery.test.ts +192 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/engine.test.ts +248 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/format-email.test.ts +96 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/format-slack.test.ts +78 -0
- package/templates/nextjs-standalone/src/lib/scheduler/__tests__/format-webhook.test.ts +78 -0
- package/templates/nextjs-standalone/src/lib/scheduler/delivery.ts +248 -0
- package/templates/nextjs-standalone/src/lib/scheduler/engine.ts +317 -0
- package/templates/nextjs-standalone/src/lib/scheduler/executor.ts +73 -0
- package/templates/nextjs-standalone/src/lib/scheduler/format-email.ts +109 -0
- package/templates/nextjs-standalone/src/lib/scheduler/format-slack.ts +35 -0
- package/templates/nextjs-standalone/src/lib/scheduler/format-webhook.ts +37 -0
- package/templates/nextjs-standalone/src/lib/scheduler/index.ts +7 -0
- package/templates/nextjs-standalone/src/lib/security.ts +11 -0
- package/templates/nextjs-standalone/src/lib/semantic-index.ts +503 -0
- package/templates/nextjs-standalone/src/lib/semantic.ts +387 -0
- package/templates/nextjs-standalone/src/lib/sidecar-types.ts +16 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/api.test.ts +160 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/format.test.ts +237 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/store.test.ts +188 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/threads.test.ts +112 -0
- package/templates/nextjs-standalone/src/lib/slack/__tests__/verify.test.ts +111 -0
- package/templates/nextjs-standalone/src/lib/slack/api.ts +102 -0
- package/templates/nextjs-standalone/src/lib/slack/format.ts +209 -0
- package/templates/nextjs-standalone/src/lib/slack/store.ts +107 -0
- package/templates/nextjs-standalone/src/lib/slack/threads.ts +64 -0
- package/templates/nextjs-standalone/src/lib/slack/verify.ts +71 -0
- package/templates/nextjs-standalone/src/lib/startup.ts +730 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/action-permissions.test.ts +594 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/custom-validation.test.ts +238 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-backend.test.ts +267 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-nsjail.test.ts +492 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-plugin.test.ts +374 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-sdk-compat.test.ts +82 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/explore-sidecar.test.ts +208 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/registry-actions.test.ts +144 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/registry.test.ts +235 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/salesforce-tool.test.ts +154 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/soql-validation.test.ts +303 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql-audit.test.ts +225 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql-connection-whitelist.test.ts +98 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql-duckdb.test.ts +233 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql-ratelimit.test.ts +225 -0
- package/templates/nextjs-standalone/src/lib/tools/__tests__/sql.test.ts +1012 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/__tests__/audit.test.ts +211 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/__tests__/email.test.ts +378 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/__tests__/handler.test.ts +681 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/__tests__/jira.test.ts +427 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/audit.ts +47 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/email.ts +191 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/handler.ts +591 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/index.ts +23 -0
- package/templates/nextjs-standalone/src/lib/tools/actions/jira.ts +220 -0
- package/templates/nextjs-standalone/src/lib/tools/explore-nsjail.ts +343 -0
- package/templates/nextjs-standalone/src/lib/tools/explore-sandbox.ts +264 -0
- package/templates/nextjs-standalone/src/lib/tools/explore-sidecar.ts +163 -0
- package/templates/nextjs-standalone/src/lib/tools/explore.ts +379 -0
- package/templates/nextjs-standalone/src/lib/tools/registry.ts +221 -0
- package/templates/nextjs-standalone/src/lib/tools/salesforce.ts +138 -0
- package/templates/nextjs-standalone/src/lib/tools/soql-validation.ts +172 -0
- package/templates/nextjs-standalone/src/lib/tools/sql.ts +680 -0
- package/templates/nextjs-standalone/src/lib/tracing.ts +40 -0
- package/templates/nextjs-standalone/src/lib/utils.ts +6 -0
- package/templates/nextjs-standalone/src/test-setup.ts +38 -0
- package/templates/nextjs-standalone/src/ui/components/actions/action-approval-card.tsx +295 -0
- package/templates/nextjs-standalone/src/ui/components/actions/action-status-badge.tsx +50 -0
- package/templates/nextjs-standalone/src/ui/components/admin/admin-layout.tsx +26 -0
- package/templates/nextjs-standalone/src/ui/components/admin/admin-sidebar.tsx +96 -0
- package/templates/nextjs-standalone/src/ui/components/admin/empty-state.tsx +24 -0
- package/templates/nextjs-standalone/src/ui/components/admin/entity-detail.tsx +233 -0
- package/templates/nextjs-standalone/src/ui/components/admin/entity-list.tsx +96 -0
- package/templates/nextjs-standalone/src/ui/components/admin/error-banner.tsx +22 -0
- package/templates/nextjs-standalone/src/ui/components/admin/feature-disabled.tsx +44 -0
- package/templates/nextjs-standalone/src/ui/components/admin/health-badge.tsx +30 -0
- package/templates/nextjs-standalone/src/ui/components/admin/loading-state.tsx +14 -0
- package/templates/nextjs-standalone/src/ui/components/admin/stat-card.tsx +32 -0
- package/templates/nextjs-standalone/src/ui/components/atlas-chat.tsx +370 -0
- package/templates/nextjs-standalone/src/ui/components/chart/chart-detection.ts +261 -0
- package/templates/nextjs-standalone/src/ui/components/chart/result-chart.tsx +375 -0
- package/templates/nextjs-standalone/src/ui/components/chat/api-key-bar.tsx +66 -0
- package/templates/nextjs-standalone/src/ui/components/chat/copy-button.tsx +25 -0
- package/templates/nextjs-standalone/src/ui/components/chat/data-table.tsx +102 -0
- package/templates/nextjs-standalone/src/ui/components/chat/error-banner.tsx +32 -0
- package/templates/nextjs-standalone/src/ui/components/chat/explore-card.tsx +41 -0
- package/templates/nextjs-standalone/src/ui/components/chat/loading-card.tsx +10 -0
- package/templates/nextjs-standalone/src/ui/components/chat/managed-auth-card.tsx +116 -0
- package/templates/nextjs-standalone/src/ui/components/chat/markdown.tsx +72 -0
- package/templates/nextjs-standalone/src/ui/components/chat/sql-block.tsx +30 -0
- package/templates/nextjs-standalone/src/ui/components/chat/sql-result-card.tsx +144 -0
- package/templates/nextjs-standalone/src/ui/components/chat/starter-prompts.ts +6 -0
- package/templates/nextjs-standalone/src/ui/components/chat/tool-part.tsx +40 -0
- package/templates/nextjs-standalone/src/ui/components/chat/typing-indicator.tsx +19 -0
- package/templates/nextjs-standalone/src/ui/components/conversations/conversation-item.tsx +120 -0
- package/templates/nextjs-standalone/src/ui/components/conversations/conversation-list.tsx +66 -0
- package/templates/nextjs-standalone/src/ui/components/conversations/conversation-sidebar.tsx +78 -0
- package/templates/nextjs-standalone/src/ui/components/conversations/delete-confirmation.tsx +27 -0
- package/templates/nextjs-standalone/src/ui/context.tsx +78 -0
- package/templates/nextjs-standalone/src/ui/hooks/use-admin-fetch.ts +104 -0
- package/templates/nextjs-standalone/src/ui/hooks/use-conversations.ts +184 -0
- package/templates/nextjs-standalone/src/ui/hooks/use-dark-mode.ts +17 -0
- package/templates/nextjs-standalone/src/ui/lib/action-types.ts +63 -0
- package/templates/nextjs-standalone/src/ui/lib/helpers.ts +104 -0
- package/templates/nextjs-standalone/src/ui/lib/types.ts +145 -0
- package/templates/nextjs-standalone/tsconfig.json +32 -0
- package/templates/nextjs-standalone/vercel.json +4 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from "bun:test";
|
|
2
|
+
import { resetAuthModeCache } from "@atlas/api/lib/auth/detect";
|
|
3
|
+
import { _resetPool } from "@atlas/api/lib/db/internal";
|
|
4
|
+
import { _setAuthInstance } from "@atlas/api/lib/auth/server";
|
|
5
|
+
import {
|
|
6
|
+
migrateAuthTables,
|
|
7
|
+
resetMigrationState,
|
|
8
|
+
getMigrationError,
|
|
9
|
+
} from "@atlas/api/lib/auth/migrate";
|
|
10
|
+
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
// Mock pool for internal DB migration tracking
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
|
|
15
|
+
function createTrackingPool(opts: { shouldThrow?: boolean } = {}) {
|
|
16
|
+
const queries: string[] = [];
|
|
17
|
+
return {
|
|
18
|
+
pool: {
|
|
19
|
+
async query(sql: string) {
|
|
20
|
+
if (opts.shouldThrow) throw new Error("permission denied for CREATE TABLE");
|
|
21
|
+
queries.push(sql);
|
|
22
|
+
return { rows: [] };
|
|
23
|
+
},
|
|
24
|
+
async end() {},
|
|
25
|
+
on() {},
|
|
26
|
+
},
|
|
27
|
+
queries,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
// Mock auth instance for Better Auth migration tracking
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
function createTrackingAuth(opts: { shouldThrow?: boolean } = {}) {
|
|
36
|
+
let migrationCount = 0;
|
|
37
|
+
return {
|
|
38
|
+
instance: {
|
|
39
|
+
$context: Promise.resolve({
|
|
40
|
+
runMigrations: async () => {
|
|
41
|
+
if (opts.shouldThrow) throw new Error("Better Auth migration error");
|
|
42
|
+
migrationCount++;
|
|
43
|
+
},
|
|
44
|
+
}),
|
|
45
|
+
},
|
|
46
|
+
getMigrationCount: () => migrationCount,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// Env snapshot
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
|
|
54
|
+
const MANAGED_VARS = [
|
|
55
|
+
"DATABASE_URL",
|
|
56
|
+
"BETTER_AUTH_SECRET",
|
|
57
|
+
"ATLAS_AUTH_JWKS_URL",
|
|
58
|
+
"ATLAS_API_KEY",
|
|
59
|
+
] as const;
|
|
60
|
+
|
|
61
|
+
const saved: Record<string, string | undefined> = {};
|
|
62
|
+
|
|
63
|
+
beforeEach(() => {
|
|
64
|
+
for (const key of MANAGED_VARS) {
|
|
65
|
+
saved[key] = process.env[key];
|
|
66
|
+
}
|
|
67
|
+
resetMigrationState();
|
|
68
|
+
resetAuthModeCache();
|
|
69
|
+
_resetPool();
|
|
70
|
+
_setAuthInstance(null);
|
|
71
|
+
|
|
72
|
+
// Default: no auth env vars
|
|
73
|
+
delete process.env.DATABASE_URL;
|
|
74
|
+
delete process.env.BETTER_AUTH_SECRET;
|
|
75
|
+
delete process.env.ATLAS_AUTH_JWKS_URL;
|
|
76
|
+
delete process.env.ATLAS_API_KEY;
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
afterEach(() => {
|
|
80
|
+
for (const key of MANAGED_VARS) {
|
|
81
|
+
if (saved[key] !== undefined) process.env[key] = saved[key];
|
|
82
|
+
else delete process.env[key];
|
|
83
|
+
}
|
|
84
|
+
resetMigrationState();
|
|
85
|
+
resetAuthModeCache();
|
|
86
|
+
_resetPool();
|
|
87
|
+
_setAuthInstance(null);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
// Tests
|
|
92
|
+
// ---------------------------------------------------------------------------
|
|
93
|
+
|
|
94
|
+
describe("migrateAuthTables", () => {
|
|
95
|
+
it("runs internal DB migration when DATABASE_URL is set", async () => {
|
|
96
|
+
process.env.DATABASE_URL = "postgresql://user:pass@localhost:5432/atlas";
|
|
97
|
+
const { pool, queries } = createTrackingPool();
|
|
98
|
+
_resetPool(pool);
|
|
99
|
+
|
|
100
|
+
await migrateAuthTables();
|
|
101
|
+
|
|
102
|
+
// migrateInternalDB: 3 audit_log + 4 conversations/messages + 2 starred column + 3 slack + 5 action_log + 2 source tracking = 19 queries
|
|
103
|
+
expect(queries.length).toBe(26);
|
|
104
|
+
expect(queries[0]).toContain("CREATE TABLE IF NOT EXISTS audit_log");
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("skips internal DB migration when DATABASE_URL is not set", async () => {
|
|
108
|
+
delete process.env.DATABASE_URL;
|
|
109
|
+
const { queries } = createTrackingPool();
|
|
110
|
+
// Don't inject pool — hasInternalDB() returns false, no pool needed
|
|
111
|
+
|
|
112
|
+
await migrateAuthTables();
|
|
113
|
+
|
|
114
|
+
expect(queries.length).toBe(0);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("runs Better Auth migration in managed mode", async () => {
|
|
118
|
+
process.env.DATABASE_URL = "postgresql://user:pass@localhost:5432/atlas";
|
|
119
|
+
process.env.BETTER_AUTH_SECRET = "a".repeat(32);
|
|
120
|
+
const { pool } = createTrackingPool();
|
|
121
|
+
_resetPool(pool);
|
|
122
|
+
const { instance, getMigrationCount } = createTrackingAuth();
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
124
|
+
_setAuthInstance(instance as any);
|
|
125
|
+
|
|
126
|
+
await migrateAuthTables();
|
|
127
|
+
|
|
128
|
+
expect(getMigrationCount()).toBe(1);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("only runs once (idempotent guard)", async () => {
|
|
132
|
+
process.env.DATABASE_URL = "postgresql://user:pass@localhost:5432/atlas";
|
|
133
|
+
process.env.BETTER_AUTH_SECRET = "a".repeat(32);
|
|
134
|
+
const { pool, queries } = createTrackingPool();
|
|
135
|
+
_resetPool(pool);
|
|
136
|
+
const { instance, getMigrationCount } = createTrackingAuth();
|
|
137
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
138
|
+
_setAuthInstance(instance as any);
|
|
139
|
+
|
|
140
|
+
await migrateAuthTables();
|
|
141
|
+
await migrateAuthTables();
|
|
142
|
+
await migrateAuthTables();
|
|
143
|
+
|
|
144
|
+
// Internal DB migration runs once (26 queries: audit_log + conversations/messages + starred column + slack + action_log + source tracking + scheduled_tasks/runs)
|
|
145
|
+
expect(queries.length).toBe(26);
|
|
146
|
+
// Better Auth migration runs once
|
|
147
|
+
expect(getMigrationCount()).toBe(1);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("skips Better Auth migration when not in managed mode", async () => {
|
|
151
|
+
process.env.DATABASE_URL = "postgresql://user:pass@localhost:5432/atlas";
|
|
152
|
+
// No BETTER_AUTH_SECRET → auth mode is "none"
|
|
153
|
+
const { pool } = createTrackingPool();
|
|
154
|
+
_resetPool(pool);
|
|
155
|
+
const { getMigrationCount } = createTrackingAuth();
|
|
156
|
+
|
|
157
|
+
await migrateAuthTables();
|
|
158
|
+
|
|
159
|
+
expect(getMigrationCount()).toBe(0);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("skips Better Auth migration when no internal DB (managed mode)", async () => {
|
|
163
|
+
delete process.env.DATABASE_URL;
|
|
164
|
+
process.env.BETTER_AUTH_SECRET = "a".repeat(32);
|
|
165
|
+
|
|
166
|
+
await migrateAuthTables();
|
|
167
|
+
|
|
168
|
+
// No pool injected, no queries possible — migration was skipped
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("does not throw when internal DB migration fails", async () => {
|
|
172
|
+
process.env.DATABASE_URL = "postgresql://user:pass@localhost:5432/atlas";
|
|
173
|
+
const { pool } = createTrackingPool({ shouldThrow: true });
|
|
174
|
+
_resetPool(pool);
|
|
175
|
+
|
|
176
|
+
// Should resolve without throwing
|
|
177
|
+
await expect(migrateAuthTables()).resolves.toBeUndefined();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("getMigrationError returns error message after internal DB failure", async () => {
|
|
181
|
+
process.env.DATABASE_URL = "postgresql://user:pass@localhost:5432/atlas";
|
|
182
|
+
const { pool } = createTrackingPool({ shouldThrow: true });
|
|
183
|
+
_resetPool(pool);
|
|
184
|
+
|
|
185
|
+
await migrateAuthTables();
|
|
186
|
+
|
|
187
|
+
const err = getMigrationError();
|
|
188
|
+
expect(err).toBeString();
|
|
189
|
+
expect(err).toContain("migration failed");
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("getMigrationError returns null on success", async () => {
|
|
193
|
+
process.env.DATABASE_URL = "postgresql://user:pass@localhost:5432/atlas";
|
|
194
|
+
const { pool } = createTrackingPool();
|
|
195
|
+
_resetPool(pool);
|
|
196
|
+
|
|
197
|
+
await migrateAuthTables();
|
|
198
|
+
|
|
199
|
+
expect(getMigrationError()).toBeNull();
|
|
200
|
+
});
|
|
201
|
+
});
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for role-based action permissions.
|
|
3
|
+
*
|
|
4
|
+
* Covers:
|
|
5
|
+
* - getUserRole() defaults per auth mode
|
|
6
|
+
* - getUserRole() with explicit role
|
|
7
|
+
* - parseRole() validation
|
|
8
|
+
* - canApprove() across all role x approval mode combinations
|
|
9
|
+
* - canApprove() with per-action requiredRole override
|
|
10
|
+
* - Edge cases: undefined user, auto approval mode
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { describe, it, expect } from "bun:test";
|
|
14
|
+
import { canApprove, getUserRole, parseRole } from "../permissions";
|
|
15
|
+
import { createAtlasUser } from "../types";
|
|
16
|
+
import type { AtlasRole } from "../types";
|
|
17
|
+
import type { ActionApprovalMode } from "@atlas/api/lib/action-types";
|
|
18
|
+
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// Helpers
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
function makeUser(mode: "simple-key" | "managed" | "byot", role?: AtlasRole) {
|
|
24
|
+
return createAtlasUser(`user-${mode}`, mode, `${mode}-label`, role);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// getUserRole()
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
describe("getUserRole()", () => {
|
|
32
|
+
it("returns explicit role when set", () => {
|
|
33
|
+
expect(getUserRole(makeUser("simple-key", "admin"))).toBe("admin");
|
|
34
|
+
expect(getUserRole(makeUser("managed", "analyst"))).toBe("analyst");
|
|
35
|
+
expect(getUserRole(makeUser("byot", "viewer"))).toBe("viewer");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("defaults to analyst for simple-key mode", () => {
|
|
39
|
+
expect(getUserRole(makeUser("simple-key"))).toBe("analyst");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("defaults to viewer for managed mode", () => {
|
|
43
|
+
expect(getUserRole(makeUser("managed"))).toBe("viewer");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("defaults to viewer for byot mode", () => {
|
|
47
|
+
expect(getUserRole(makeUser("byot"))).toBe("viewer");
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
// parseRole()
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
|
|
55
|
+
describe("parseRole()", () => {
|
|
56
|
+
it("returns valid roles", () => {
|
|
57
|
+
expect(parseRole("viewer")).toBe("viewer");
|
|
58
|
+
expect(parseRole("analyst")).toBe("analyst");
|
|
59
|
+
expect(parseRole("admin")).toBe("admin");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("is case-insensitive", () => {
|
|
63
|
+
expect(parseRole("ADMIN")).toBe("admin");
|
|
64
|
+
expect(parseRole("Analyst")).toBe("analyst");
|
|
65
|
+
expect(parseRole("VIEWER")).toBe("viewer");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("trims whitespace", () => {
|
|
69
|
+
expect(parseRole(" admin ")).toBe("admin");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("returns undefined for invalid values", () => {
|
|
73
|
+
expect(parseRole("superadmin")).toBeUndefined();
|
|
74
|
+
expect(parseRole("")).toBeUndefined();
|
|
75
|
+
expect(parseRole(undefined)).toBeUndefined();
|
|
76
|
+
expect(parseRole("root")).toBeUndefined();
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
// canApprove() — core matrix
|
|
82
|
+
// ---------------------------------------------------------------------------
|
|
83
|
+
|
|
84
|
+
describe("canApprove()", () => {
|
|
85
|
+
describe("with undefined user (no-auth mode)", () => {
|
|
86
|
+
it("denies all approval modes (no user = no approval ability)", () => {
|
|
87
|
+
// Even auto is denied because canApprove only runs on manual approval endpoints.
|
|
88
|
+
// Auto-approved actions are auto-executed by handleAction — they never reach canApprove.
|
|
89
|
+
expect(canApprove(undefined, "auto")).toBe(false);
|
|
90
|
+
expect(canApprove(undefined, "manual")).toBe(false);
|
|
91
|
+
expect(canApprove(undefined, "admin-only")).toBe(false);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("auto approval mode", () => {
|
|
96
|
+
it("allows all roles (no human approval needed)", () => {
|
|
97
|
+
expect(canApprove(makeUser("managed", "viewer"), "auto")).toBe(true);
|
|
98
|
+
expect(canApprove(makeUser("simple-key", "analyst"), "auto")).toBe(true);
|
|
99
|
+
expect(canApprove(makeUser("byot", "admin"), "auto")).toBe(true);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe("manual approval mode", () => {
|
|
104
|
+
it("denies viewer", () => {
|
|
105
|
+
expect(canApprove(makeUser("managed", "viewer"), "manual")).toBe(false);
|
|
106
|
+
expect(canApprove(makeUser("byot", "viewer"), "manual")).toBe(false);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("allows analyst", () => {
|
|
110
|
+
expect(canApprove(makeUser("simple-key", "analyst"), "manual")).toBe(true);
|
|
111
|
+
expect(canApprove(makeUser("managed", "analyst"), "manual")).toBe(true);
|
|
112
|
+
expect(canApprove(makeUser("byot", "analyst"), "manual")).toBe(true);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("allows admin", () => {
|
|
116
|
+
expect(canApprove(makeUser("simple-key", "admin"), "manual")).toBe(true);
|
|
117
|
+
expect(canApprove(makeUser("managed", "admin"), "manual")).toBe(true);
|
|
118
|
+
expect(canApprove(makeUser("byot", "admin"), "manual")).toBe(true);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("admin-only approval mode", () => {
|
|
123
|
+
it("denies viewer", () => {
|
|
124
|
+
expect(canApprove(makeUser("managed", "viewer"), "admin-only")).toBe(false);
|
|
125
|
+
expect(canApprove(makeUser("byot", "viewer"), "admin-only")).toBe(false);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("denies analyst", () => {
|
|
129
|
+
expect(canApprove(makeUser("simple-key", "analyst"), "admin-only")).toBe(false);
|
|
130
|
+
expect(canApprove(makeUser("managed", "analyst"), "admin-only")).toBe(false);
|
|
131
|
+
expect(canApprove(makeUser("byot", "analyst"), "admin-only")).toBe(false);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("allows admin", () => {
|
|
135
|
+
expect(canApprove(makeUser("simple-key", "admin"), "admin-only")).toBe(true);
|
|
136
|
+
expect(canApprove(makeUser("managed", "admin"), "admin-only")).toBe(true);
|
|
137
|
+
expect(canApprove(makeUser("byot", "admin"), "admin-only")).toBe(true);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// -------------------------------------------------------------------------
|
|
142
|
+
// Per-action requiredRole override
|
|
143
|
+
// -------------------------------------------------------------------------
|
|
144
|
+
|
|
145
|
+
describe("with requiredRole override", () => {
|
|
146
|
+
it("overrides manual default — requires admin", () => {
|
|
147
|
+
// manual normally allows analyst, but requiredRole=admin blocks them
|
|
148
|
+
expect(canApprove(makeUser("simple-key", "analyst"), "manual", "admin")).toBe(false);
|
|
149
|
+
expect(canApprove(makeUser("simple-key", "admin"), "manual", "admin")).toBe(true);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("overrides admin-only default — requires analyst", () => {
|
|
153
|
+
// admin-only normally requires admin, but requiredRole=analyst lowers the bar
|
|
154
|
+
expect(canApprove(makeUser("managed", "analyst"), "admin-only", "analyst")).toBe(true);
|
|
155
|
+
expect(canApprove(makeUser("managed", "viewer"), "admin-only", "analyst")).toBe(false);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("viewer requiredRole allows all authenticated users", () => {
|
|
159
|
+
expect(canApprove(makeUser("managed", "viewer"), "manual", "viewer")).toBe(true);
|
|
160
|
+
expect(canApprove(makeUser("simple-key", "analyst"), "manual", "viewer")).toBe(true);
|
|
161
|
+
expect(canApprove(makeUser("byot", "admin"), "manual", "viewer")).toBe(true);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("still denies undefined user even with viewer requiredRole", () => {
|
|
165
|
+
expect(canApprove(undefined, "manual", "viewer")).toBe(false);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("does not apply to auto mode", () => {
|
|
169
|
+
// Auto mode always returns true regardless of requiredRole
|
|
170
|
+
expect(canApprove(makeUser("managed", "viewer"), "auto", "admin")).toBe(true);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// -------------------------------------------------------------------------
|
|
175
|
+
// Auth mode default roles (no explicit role set)
|
|
176
|
+
// -------------------------------------------------------------------------
|
|
177
|
+
|
|
178
|
+
describe("with auth mode default roles (no explicit role)", () => {
|
|
179
|
+
it("simple-key defaults to analyst — can approve manual, blocked from admin-only", () => {
|
|
180
|
+
const user = makeUser("simple-key"); // defaults to analyst
|
|
181
|
+
expect(canApprove(user, "manual")).toBe(true);
|
|
182
|
+
expect(canApprove(user, "admin-only")).toBe(false);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("managed defaults to viewer — blocked from manual and admin-only", () => {
|
|
186
|
+
const user = makeUser("managed"); // defaults to viewer
|
|
187
|
+
expect(canApprove(user, "manual")).toBe(false);
|
|
188
|
+
expect(canApprove(user, "admin-only")).toBe(false);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("byot defaults to viewer — blocked from manual and admin-only", () => {
|
|
192
|
+
const user = makeUser("byot"); // defaults to viewer
|
|
193
|
+
expect(canApprove(user, "manual")).toBe(false);
|
|
194
|
+
expect(canApprove(user, "admin-only")).toBe(false);
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// ---------------------------------------------------------------------------
|
|
200
|
+
// Full auth mode x role x approval mode matrix
|
|
201
|
+
// ---------------------------------------------------------------------------
|
|
202
|
+
|
|
203
|
+
describe("full permission matrix", () => {
|
|
204
|
+
const modes = ["simple-key", "managed", "byot"] as const;
|
|
205
|
+
const roles: AtlasRole[] = ["viewer", "analyst", "admin"];
|
|
206
|
+
const approvalModes: ActionApprovalMode[] = ["auto", "manual", "admin-only"];
|
|
207
|
+
|
|
208
|
+
// Expected results: [role][approvalMode] => boolean
|
|
209
|
+
const expected: Record<AtlasRole, Record<ActionApprovalMode, boolean>> = {
|
|
210
|
+
viewer: { auto: true, manual: false, "admin-only": false },
|
|
211
|
+
analyst: { auto: true, manual: true, "admin-only": false },
|
|
212
|
+
admin: { auto: true, manual: true, "admin-only": true },
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
for (const mode of modes) {
|
|
216
|
+
for (const role of roles) {
|
|
217
|
+
for (const approval of approvalModes) {
|
|
218
|
+
it(`${mode}/${role} + ${approval} => ${expected[role][approval]}`, () => {
|
|
219
|
+
const user = makeUser(mode, role);
|
|
220
|
+
expect(canApprove(user, approval)).toBe(expected[role][approval]);
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach } from "bun:test";
|
|
2
|
+
import { betterAuth } from "better-auth";
|
|
3
|
+
import { bearer } from "better-auth/plugins";
|
|
4
|
+
import { apiKey } from "@better-auth/api-key";
|
|
5
|
+
import { resetAuthInstance } from "../server";
|
|
6
|
+
|
|
7
|
+
describe("Better Auth instance shape", () => {
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
resetAuthInstance();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("betterAuth() with @better-auth/api-key returns expected shape", async () => {
|
|
13
|
+
// Verify the `as unknown as AuthInstance` cast in server.ts doesn't
|
|
14
|
+
// hide a missing property. This uses the real betterAuth() constructor
|
|
15
|
+
// with the same plugins as production.
|
|
16
|
+
const instance = betterAuth({
|
|
17
|
+
// Minimal adapter stub — enough for construction, never queried.
|
|
18
|
+
database: {
|
|
19
|
+
db: null,
|
|
20
|
+
type: "sqlite",
|
|
21
|
+
} as unknown as Parameters<typeof betterAuth>[0]["database"],
|
|
22
|
+
secret: "test-secret-at-least-32-characters-long",
|
|
23
|
+
plugins: [bearer(), apiKey()],
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
expect(typeof instance.handler).toBe("function");
|
|
27
|
+
expect(typeof instance.api.getSession).toBe("function");
|
|
28
|
+
expect(instance.$context).toBeInstanceOf(Promise);
|
|
29
|
+
|
|
30
|
+
// Drain the $context promise so the async DB adapter init error
|
|
31
|
+
// doesn't surface as an unhandled rejection after the test ends.
|
|
32
|
+
await instance.$context.catch(() => {});
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from "bun:test";
|
|
2
|
+
import { validateApiKey } from "../simple-key";
|
|
3
|
+
|
|
4
|
+
function makeReq(headers?: Record<string, string>): Request {
|
|
5
|
+
return new Request("http://localhost/api/chat", { headers });
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
describe("validateApiKey()", () => {
|
|
9
|
+
const origApiKey = process.env.ATLAS_API_KEY;
|
|
10
|
+
const origApiKeyRole = process.env.ATLAS_API_KEY_ROLE;
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
process.env.ATLAS_API_KEY = "sk-test-abcdef1234567890";
|
|
14
|
+
delete process.env.ATLAS_API_KEY_ROLE;
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
if (origApiKey !== undefined) process.env.ATLAS_API_KEY = origApiKey;
|
|
19
|
+
else delete process.env.ATLAS_API_KEY;
|
|
20
|
+
if (origApiKeyRole !== undefined) process.env.ATLAS_API_KEY_ROLE = origApiKeyRole;
|
|
21
|
+
else delete process.env.ATLAS_API_KEY_ROLE;
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("authenticates with correct Authorization: Bearer header", () => {
|
|
25
|
+
const req = makeReq({ Authorization: "Bearer sk-test-abcdef1234567890" });
|
|
26
|
+
const result = validateApiKey(req);
|
|
27
|
+
expect(result.authenticated).toBe(true);
|
|
28
|
+
if (result.authenticated) {
|
|
29
|
+
expect(result.user).toBeDefined();
|
|
30
|
+
expect(result.user!.mode).toBe("simple-key");
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("authenticates with correct X-API-Key header", () => {
|
|
35
|
+
const req = makeReq({ "X-API-Key": "sk-test-abcdef1234567890" });
|
|
36
|
+
const result = validateApiKey(req);
|
|
37
|
+
expect(result.authenticated).toBe(true);
|
|
38
|
+
if (result.authenticated) {
|
|
39
|
+
expect(result.user).toBeDefined();
|
|
40
|
+
expect(result.user!.mode).toBe("simple-key");
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("rejects mismatched key", () => {
|
|
45
|
+
const req = makeReq({ Authorization: "Bearer wrong-key" });
|
|
46
|
+
const result = validateApiKey(req);
|
|
47
|
+
expect(result.authenticated).toBe(false);
|
|
48
|
+
if (!result.authenticated) {
|
|
49
|
+
expect(result.status).toBe(401);
|
|
50
|
+
expect(result.error).toBe("Invalid API key");
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("rejects when no key header is present", () => {
|
|
55
|
+
const req = makeReq();
|
|
56
|
+
const result = validateApiKey(req);
|
|
57
|
+
expect(result.authenticated).toBe(false);
|
|
58
|
+
if (!result.authenticated) {
|
|
59
|
+
expect(result.status).toBe(401);
|
|
60
|
+
expect(result.error).toBe("API key required");
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("rejects when ATLAS_API_KEY is not configured", () => {
|
|
65
|
+
delete process.env.ATLAS_API_KEY;
|
|
66
|
+
const req = makeReq({ Authorization: "Bearer some-key" });
|
|
67
|
+
const result = validateApiKey(req);
|
|
68
|
+
expect(result.authenticated).toBe(false);
|
|
69
|
+
if (!result.authenticated) {
|
|
70
|
+
expect(result.status).toBe(401);
|
|
71
|
+
expect(result.error).toBe("API key not configured");
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("produces a stable user ID (same key → same hash)", () => {
|
|
76
|
+
const req1 = makeReq({ Authorization: "Bearer sk-test-abcdef1234567890" });
|
|
77
|
+
const req2 = makeReq({ "X-API-Key": "sk-test-abcdef1234567890" });
|
|
78
|
+
const r1 = validateApiKey(req1);
|
|
79
|
+
const r2 = validateApiKey(req2);
|
|
80
|
+
expect(r1.authenticated).toBe(true);
|
|
81
|
+
expect(r2.authenticated).toBe(true);
|
|
82
|
+
if (r1.authenticated && r1.user && r2.authenticated && r2.user) {
|
|
83
|
+
expect(r1.user.id).toBe(r2.user.id);
|
|
84
|
+
expect(r1.user.id).toMatch(/^api-key-[0-9a-f]{8}$/);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("user label contains key prefix (first 4 chars)", () => {
|
|
89
|
+
const req = makeReq({ Authorization: "Bearer sk-test-abcdef1234567890" });
|
|
90
|
+
const result = validateApiKey(req);
|
|
91
|
+
expect(result.authenticated).toBe(true);
|
|
92
|
+
if (result.authenticated && result.user) {
|
|
93
|
+
expect(result.user.label).toBe("api-key-sk-t");
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("Authorization header takes precedence over X-API-Key", () => {
|
|
98
|
+
const req = makeReq({
|
|
99
|
+
Authorization: "Bearer sk-test-abcdef1234567890",
|
|
100
|
+
"X-API-Key": "wrong-key",
|
|
101
|
+
});
|
|
102
|
+
const result = validateApiKey(req);
|
|
103
|
+
// Should succeed because Authorization header has the correct key
|
|
104
|
+
expect(result.authenticated).toBe(true);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("rejects Authorization header without Bearer prefix", () => {
|
|
108
|
+
const req = makeReq({ Authorization: "sk-test-abcdef1234567890" });
|
|
109
|
+
// No "Bearer " prefix → extractKey falls through to X-API-Key (not present) → null
|
|
110
|
+
const result = validateApiKey(req);
|
|
111
|
+
expect(result.authenticated).toBe(false);
|
|
112
|
+
if (!result.authenticated) {
|
|
113
|
+
expect(result.error).toBe("API key required");
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("rejects very short key without throwing", () => {
|
|
118
|
+
const req = makeReq({ Authorization: "Bearer a" });
|
|
119
|
+
const result = validateApiKey(req);
|
|
120
|
+
expect(result.authenticated).toBe(false);
|
|
121
|
+
if (!result.authenticated) {
|
|
122
|
+
expect(result.status).toBe(401);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("rejects very long key without throwing", () => {
|
|
127
|
+
const req = makeReq({ Authorization: "Bearer " + "x".repeat(1000) });
|
|
128
|
+
const result = validateApiKey(req);
|
|
129
|
+
expect(result.authenticated).toBe(false);
|
|
130
|
+
if (!result.authenticated) {
|
|
131
|
+
expect(result.status).toBe(401);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe("role extraction via ATLAS_API_KEY_ROLE", () => {
|
|
136
|
+
it("ATLAS_API_KEY_ROLE=admin produces user with role 'admin'", () => {
|
|
137
|
+
process.env.ATLAS_API_KEY_ROLE = "admin";
|
|
138
|
+
const req = makeReq({ Authorization: "Bearer sk-test-abcdef1234567890" });
|
|
139
|
+
const result = validateApiKey(req);
|
|
140
|
+
expect(result.authenticated).toBe(true);
|
|
141
|
+
if (result.authenticated && result.user) {
|
|
142
|
+
expect(result.user.role).toBe("admin");
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("ATLAS_API_KEY_ROLE=invalid falls back (no role on user)", () => {
|
|
147
|
+
process.env.ATLAS_API_KEY_ROLE = "invalid";
|
|
148
|
+
const req = makeReq({ Authorization: "Bearer sk-test-abcdef1234567890" });
|
|
149
|
+
const result = validateApiKey(req);
|
|
150
|
+
expect(result.authenticated).toBe(true);
|
|
151
|
+
if (result.authenticated && result.user) {
|
|
152
|
+
expect(result.user.role).toBeUndefined();
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("ATLAS_API_KEY_ROLE=ADMIN (case insensitive) works", () => {
|
|
157
|
+
process.env.ATLAS_API_KEY_ROLE = "ADMIN";
|
|
158
|
+
const req = makeReq({ Authorization: "Bearer sk-test-abcdef1234567890" });
|
|
159
|
+
const result = validateApiKey(req);
|
|
160
|
+
expect(result.authenticated).toBe(true);
|
|
161
|
+
if (result.authenticated && result.user) {
|
|
162
|
+
expect(result.user.role).toBe("admin");
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("no ATLAS_API_KEY_ROLE set — user has no explicit role", () => {
|
|
167
|
+
delete process.env.ATLAS_API_KEY_ROLE;
|
|
168
|
+
const req = makeReq({ Authorization: "Bearer sk-test-abcdef1234567890" });
|
|
169
|
+
const result = validateApiKey(req);
|
|
170
|
+
expect(result.authenticated).toBe(true);
|
|
171
|
+
if (result.authenticated && result.user) {
|
|
172
|
+
expect(result.user.role).toBeUndefined();
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for createAtlasUser validation and AUTH_MODES constant.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect } from "bun:test";
|
|
6
|
+
import { createAtlasUser, AUTH_MODES } from "../types";
|
|
7
|
+
|
|
8
|
+
describe("createAtlasUser()", () => {
|
|
9
|
+
it("throws when id is empty string", () => {
|
|
10
|
+
expect(() => createAtlasUser("", "simple-key", "label")).toThrow(
|
|
11
|
+
"AtlasUser id must be non-empty",
|
|
12
|
+
);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("throws when label is empty string", () => {
|
|
16
|
+
expect(() => createAtlasUser("usr_1", "managed", "")).toThrow(
|
|
17
|
+
"AtlasUser label must be non-empty",
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("returns an object with correct id, mode, and label", () => {
|
|
22
|
+
const user = createAtlasUser("usr_1", "byot", "alice@example.com");
|
|
23
|
+
expect(user.id).toBe("usr_1");
|
|
24
|
+
expect(user.mode).toBe("byot");
|
|
25
|
+
expect(user.label).toBe("alice@example.com");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("returns a frozen object", () => {
|
|
29
|
+
const user = createAtlasUser("usr_1", "simple-key", "api-key-sk-t");
|
|
30
|
+
expect(Object.isFrozen(user)).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("AUTH_MODES", () => {
|
|
35
|
+
it("contains all four auth modes", () => {
|
|
36
|
+
expect(AUTH_MODES).toEqual(["none", "simple-key", "managed", "byot"]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("is a readonly tuple at the type level", () => {
|
|
40
|
+
// `as const` makes the array readonly at compile time; at runtime it's a plain array
|
|
41
|
+
expect(Array.isArray(AUTH_MODES)).toBe(true);
|
|
42
|
+
expect(AUTH_MODES.length).toBe(4);
|
|
43
|
+
});
|
|
44
|
+
});
|