Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
20 changes: 1 addition & 19 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,9 @@ jobs:
steps:
- uses: actions/checkout@v7
- uses: ./.github/actions/setup
# One tier: every test runs against a real in-memory SQLite DB (src/tests/setup).
- run: pnpm test

integration:
name: Integration tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: ./.github/actions/setup

# .env.test is gitignored — recreate it for the dotenv loaders in vitest and drizzle.
- name: Write .env.test
run: echo 'TEST_DATABASE_URL=file:./.tmp/chaibuilder_test.sqlite' > .env.test

- name: Generate SQLite migrations
run: pnpm db:test:generate

- name: Run integration tests
env:
NODE_ENV: test
run: pnpm test:integration

build:
name: Build
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .sync-ref
Original file line number Diff line number Diff line change
@@ -1 +1 @@
chaibuilder/pro@103c78ca9ad4b79792205a357c0f16748aa2ae17
chaibuilder/pro@bab8bc4f11ed65f47b199132ab6fba6068d5caa4
8 changes: 3 additions & 5 deletions drizzle.config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { defineConfig } from "drizzle-kit";

config({ path: ".env.test", override: true });

const url = process.env.TEST_DATABASE_URL!;

if (!url) {
throw new Error("TEST_DATABASE_URL is not set in .env.test");
}
// Tests build their schema in memory (src/tests/setup/apply-schema.ts); this config only backs
// `db:test:generate` / `db:test:studio` for inspecting the schema by hand.
const url = process.env.TEST_DATABASE_URL || "file:./.tmp/chaibuilder_test.sqlite";

export default defineConfig({
// Edition-specific schema barrel (core tables only in chaicore, the full union in chaipro).
Expand Down
131 changes: 111 additions & 20 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,83 @@
import { defineConfig, globalIgnores } from "eslint/config";
import tseslint from "typescript-eslint";

// ---------------------------------------------------------------------------------------------
// `no-restricted-syntax` entries. Flat config replaces a rule's options wholesale for a file, so
// every block that sets this rule spreads the shared entries it still needs.

/** Modules tests may replace with `vi.mock` / `vi.doMock`: code that talks to the outside world. */
const MOCKABLE_MODULES = [
"@openrouter/ai-sdk-provider",
"@ai-sdk/openai-compatible",
"workers-ai-provider",
"@ai-sdk/react",
"ai",
"sonner",
];

const escapeRegex = (value) => value.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");

const viMockEntries = (allowed) => [
{
selector:
'CallExpression[callee.object.name="vi"][callee.property.name=/^(mock|doMock)$/]' +
`:not([arguments.0.value=/^(${allowed.map(escapeRegex).join("|")})$/])`,
message:
"Do not mock internal or library modules — tests run against the real system. Use the harness: " +
"~/tests/setup (withTestDB, useTestChaiConfig, withChaiConfig, withRequestContext, installMemoryPersistentCache), " +
"~/tests/builder (renderInBuilder, renderHookInBuilder, mockChaiAction, registerTestBlocks), " +
"~/tests/utils/capture-consola. Only network SDKs (AI providers) and sonner may be mocked.",
},
{
selector:
'CallExpression[callee.object.name="vi"][callee.property.name=/^(mock|doMock)$/][arguments.0.type!="Literal"]',
message: "vi.mock target must be a string literal so the mock allowlist can be checked.",
},
];

const DEFAULTS_BARREL_ENTRY = {
selector: "ImportDeclaration[source.value=/^~\\/server\\/defaults(\\/index)?$/]",
message:
"Import from ~/server/defaults/config-registry (or the specific submodule). The ~/server/defaults barrel loads " +
"builtin-chai-actions, which instantiates every action class and cycles back into action modules.",
};

/**
* Subtree boundary patterns, shared by the two blocks that enforce them. Flat config
* replaces a rule's options wholesale per file, so the block that exempts the MCP entry
* from the SDK restriction has to restate these rather than rely on an earlier block.
*/
const subtreeImportPatterns = [
{
// Host apps alias their own source as @/ (Next.js default); ~~/ and #/ are the
// other conventions in use. This repo's own alias is ~/, mapped to ./src.
// "[#]/**" not "#/**": minimatch reads a leading # as a comment and the
// pattern would silently never match.
group: ["@/*", "@/**", "~~/*", "~~/**", "[#]/**"],
message:
"Host-app alias import. This subtree must build standalone — use ~/ (this repo's src) or a package dependency.",
},
{
// Deepest legitimate import in src/ today is four levels; five would leave the
// subtree entirely when it sits at a shallow prefix in a host repo.
group: ["../../../../../*", "../../../../../**"],
message: "Relative import climbs out of the subtree. Use the ~/ alias to reach other modules in this repo.",
},
];

/**
* Optional-peer boundary: `@modelcontextprotocol/sdk` may only be imported by the module
* behind the `<pkg>/mcp` entry point. Anywhere else the static import would be pulled
* into an entry that hosts without the package also load, turning an optional peer into
* a required one and breaking their build. Tool authoring is SDK-free on purpose — see
* src/server/mcp/api.ts.
*/
const mcpSdkImportPattern = {
group: ["@modelcontextprotocol/sdk", "@modelcontextprotocol/sdk/*"],
message:
"Only ~/server/mcp/handle-request may import the MCP SDK (optional peer). Define tools with the SDK-free helpers in ~/server/mcp/api.",
};

const eslintConfig = defineConfig([
// src/utils/vendor: vendored third-party code, kept verbatim — its inline eslint
// directives reference plugins (react-hooks) this config does not install.
Expand Down Expand Up @@ -85,29 +162,43 @@ const eslintConfig = defineConfig([
rules: {
"@typescript-eslint/no-restricted-imports": [
"error",
{
patterns: [
{
// Host apps alias their own source as @/ (Next.js default); ~~/ and #/ are the
// other conventions in use. This repo's own alias is ~/, mapped to ./src.
// "[#]/**" not "#/**": minimatch reads a leading # as a comment and the
// pattern would silently never match.
group: ["@/*", "@/**", "~~/*", "~~/**", "[#]/**"],
message:
"Host-app alias import. This subtree must build standalone — use ~/ (this repo's src) or a package dependency.",
},
{
// Deepest legitimate import in src/ today is four levels; five would leave the
// subtree entirely when it sits at a shallow prefix in a host repo.
group: ["../../../../../*", "../../../../../**"],
message:
"Relative import climbs out of the subtree. Use the ~/ alias to reach other modules in this repo.",
},
],
},
{ patterns: [...subtreeImportPatterns, mcpSdkImportPattern] },
],
},
},
{
// Tests run against the real system (in-memory DB, real config, real jotai store, real
// i18n, msw at the HTTP boundary). In-source `import.meta.vitest` blocks live in non-test
// files, so this covers all of src/.
files: ["src/**/*.{ts,tsx}"],
rules: { "no-restricted-syntax": ["error", ...viMockEntries(MOCKABLE_MODULES)] },
},
{
// Action and page-data modules are loaded by builtin-chai-actions; they must not import the barrel.
files: [
"src/server/chai-actions/**/*.{ts,tsx}",
"src/server/chai-builder/**/*.{ts,tsx}",
"src/plugins/*/server/**/*.{ts,tsx}",
],
rules: { "no-restricted-syntax": ["error", ...viMockEntries(MOCKABLE_MODULES), DEFAULTS_BARREL_ENTRY] },
},
{
// The Tailwind compat adapter's contract is the shape of its calls into tailwindcss across
// major versions; only a stand-in compiler can observe that.
files: ["src/utils/tailwind-css-compat.test.ts"],
rules: { "no-restricted-syntax": ["error", ...viMockEntries([...MOCKABLE_MODULES, "tailwindcss"])] },
},
{
// The one module allowed to resolve the optional MCP SDK peer, plus tests — a test
// never ships in a bundle, and measuring what a client actually receives means using
// the SDK's own schema conversion rather than a copy of it that can drift. Restates
// the subtree patterns because this rule's options replace the block above for these
// files.
files: ["src/server/mcp/handle-request.ts", "src/**/*.test.ts"],
rules: {
"@typescript-eslint/no-restricted-imports": ["error", { patterns: subtreeImportPatterns }],
},
},
]);

export default eslintConfig;
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
"build": "NODE_OPTIONS='--max-old-space-size=6144' tsup --format esm,cjs && NODE_OPTIONS='--max-old-space-size=6144' node scripts/build-dts.mjs",
"build:dts": "NODE_OPTIONS='--max-old-space-size=6144' node scripts/build-dts.mjs",
"test": "vitest --run",
"test:integration": "vitest --run --config vitest.config.integration.ts",
"test:integration:watch": "vitest --config vitest.config.integration.ts",
"test:watch": "vitest",
"db:test:generate": "drizzle-kit generate --config drizzle.config.test.ts",
"db:test:studio": "drizzle-kit studio --config drizzle.config.test.ts",
"lint": "eslint src/",
Expand Down Expand Up @@ -174,6 +173,7 @@
"peerDependencies": {
"@ai-sdk/openai-compatible": ">=2.0.0",
"@libsql/client": ">=0.14.0",
"@modelcontextprotocol/sdk": ">=1.26.0 <2",
"@next/third-parties": ">=15.3.0",
"@openrouter/ai-sdk-provider": ">=2.0.0",
"@tailwindcss/postcss": "^4.0.0",
Expand All @@ -184,7 +184,8 @@
"react": ">=19.0.0",
"react-dom": ">=19.0.0",
"tailwindcss": "^4.0.0",
"workers-ai-provider": ">=3.0.0"
"workers-ai-provider": ">=3.0.0",
"pg": ">=8.0.0"
},
"peerDependenciesMeta": {
"@ai-sdk/openai-compatible": {
Expand Down Expand Up @@ -216,6 +217,12 @@
},
"workers-ai-provider": {
"optional": true
},
"@modelcontextprotocol/sdk": {
"optional": true
},
"pg": {
"optional": true
}
},
"dependencies": {
Expand Down Expand Up @@ -291,6 +298,7 @@
"react-i18next": "16.2.4",
"sonner": "2.0.7",
"streamdown": "2.5.0",
"virtual-react-json-diff": "1.0.20",
"xss": "1.0.15",
"zod": "4.3.5"
},
Expand All @@ -300,6 +308,7 @@
"@commitlint/config-conventional": "21.2.2",
"@faker-js/faker": "10.5.0",
"@libsql/client": "0.17.3",
"@modelcontextprotocol/sdk": "1.26.0",
"@next/third-parties": "^16.2.10",
"@openrouter/ai-sdk-provider": "2.10.0",
"@payloadcms/ui": "^3.86.0",
Expand All @@ -325,6 +334,7 @@
"msw": "2.14.6",
"next": "^16.3.3",
"payload": "^3.89.0",
"pg": "8.16.3",
"postgres": "3.4.9",
"prettier": "3.8.3",
"prettier-plugin-tailwindcss": "0.7.4",
Expand Down
Loading
Loading