swallowkit 1.1.2 → 2.0.0-alpha.10
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.ja.md +61 -7
- package/README.md +61 -7
- package/dist/cli/commands/add-auth.d.ts.map +1 -1
- package/dist/cli/commands/add-auth.js +103 -27
- package/dist/cli/commands/add-auth.js.map +1 -1
- package/dist/cli/commands/configure-mcp.d.ts +12 -0
- package/dist/cli/commands/configure-mcp.d.ts.map +1 -0
- package/dist/cli/commands/configure-mcp.js +42 -0
- package/dist/cli/commands/configure-mcp.js.map +1 -0
- package/dist/cli/commands/dev.d.ts +5 -2
- package/dist/cli/commands/dev.d.ts.map +1 -1
- package/dist/cli/commands/dev.js +143 -67
- package/dist/cli/commands/dev.js.map +1 -1
- package/dist/cli/commands/init.d.ts +14 -7
- package/dist/cli/commands/init.d.ts.map +1 -1
- package/dist/cli/commands/init.js +149 -170
- package/dist/cli/commands/init.js.map +1 -1
- package/dist/cli/commands/migrations.d.ts.map +1 -1
- package/dist/cli/commands/migrations.js +25 -0
- package/dist/cli/commands/migrations.js.map +1 -1
- package/dist/cli/commands/provision.d.ts +5 -0
- package/dist/cli/commands/provision.d.ts.map +1 -1
- package/dist/cli/commands/provision.js +278 -55
- package/dist/cli/commands/provision.js.map +1 -1
- package/dist/cli/commands/scaffold.d.ts +5 -1
- package/dist/cli/commands/scaffold.d.ts.map +1 -1
- package/dist/cli/commands/scaffold.js +123 -18
- package/dist/cli/commands/scaffold.js.map +1 -1
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +49 -8
- package/dist/cli/index.js.map +1 -1
- package/dist/core/config.d.ts +2 -1
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js +19 -0
- package/dist/core/config.js.map +1 -1
- package/dist/core/operations/create-model.d.ts +2 -0
- package/dist/core/operations/create-model.d.ts.map +1 -1
- package/dist/core/operations/create-model.js +60 -8
- package/dist/core/operations/create-model.js.map +1 -1
- package/dist/core/operations/migration-generator.d.ts.map +1 -1
- package/dist/core/operations/migration-generator.js +12 -1
- package/dist/core/operations/migration-generator.js.map +1 -1
- package/dist/core/operations/model-migration.d.ts +22 -0
- package/dist/core/operations/model-migration.d.ts.map +1 -0
- package/dist/core/operations/model-migration.js +36 -0
- package/dist/core/operations/model-migration.js.map +1 -0
- package/dist/core/operations/provision-deployment.d.ts +52 -0
- package/dist/core/operations/provision-deployment.d.ts.map +1 -0
- package/dist/core/operations/provision-deployment.js +164 -0
- package/dist/core/operations/provision-deployment.js.map +1 -0
- package/dist/core/operations/provision-operations.d.ts +62 -6
- package/dist/core/operations/provision-operations.d.ts.map +1 -1
- package/dist/core/operations/provision-operations.js +653 -115
- package/dist/core/operations/provision-operations.js.map +1 -1
- package/dist/core/operations/scaffold-apply.d.ts +1 -0
- package/dist/core/operations/scaffold-apply.d.ts.map +1 -1
- package/dist/core/operations/scaffold-apply.js +1 -0
- package/dist/core/operations/scaffold-apply.js.map +1 -1
- package/dist/core/operations/scaffold-machine.d.ts +1 -0
- package/dist/core/operations/scaffold-machine.d.ts.map +1 -1
- package/dist/core/operations/scaffold-machine.js.map +1 -1
- package/dist/core/operations/scaffold-plan.d.ts +2 -0
- package/dist/core/operations/scaffold-plan.d.ts.map +1 -1
- package/dist/core/operations/scaffold-plan.js +3 -1
- package/dist/core/operations/scaffold-plan.js.map +1 -1
- package/dist/core/project/agent-instructions.d.ts +2 -1
- package/dist/core/project/agent-instructions.d.ts.map +1 -1
- package/dist/core/project/agent-instructions.js +15 -0
- package/dist/core/project/agent-instructions.js.map +1 -1
- package/dist/core/project/boundaries.d.ts +5 -2
- package/dist/core/project/boundaries.d.ts.map +1 -1
- package/dist/core/project/boundaries.js +51 -20
- package/dist/core/project/boundaries.js.map +1 -1
- package/dist/core/project/capabilities.d.ts +17 -0
- package/dist/core/project/capabilities.d.ts.map +1 -1
- package/dist/core/project/capabilities.js +45 -6
- package/dist/core/project/capabilities.js.map +1 -1
- package/dist/core/project/home-page.d.ts +4 -0
- package/dist/core/project/home-page.d.ts.map +1 -0
- package/dist/core/project/home-page.js +113 -0
- package/dist/core/project/home-page.js.map +1 -0
- package/dist/core/project/manifest.d.ts +3 -1
- package/dist/core/project/manifest.d.ts.map +1 -1
- package/dist/core/project/manifest.js +47 -25
- package/dist/core/project/manifest.js.map +1 -1
- package/dist/core/project/mcp-config.d.ts +26 -0
- package/dist/core/project/mcp-config.d.ts.map +1 -0
- package/dist/core/project/mcp-config.js +117 -0
- package/dist/core/project/mcp-config.js.map +1 -0
- package/dist/core/project/react-project.d.ts +26 -0
- package/dist/core/project/react-project.d.ts.map +1 -0
- package/dist/core/project/react-project.js +443 -0
- package/dist/core/project/react-project.js.map +1 -0
- package/dist/core/project/state.d.ts +7 -0
- package/dist/core/project/state.d.ts.map +1 -1
- package/dist/core/project/state.js +86 -0
- package/dist/core/project/state.js.map +1 -1
- package/dist/core/project/validation.d.ts.map +1 -1
- package/dist/core/project/validation.js +32 -10
- package/dist/core/project/validation.js.map +1 -1
- package/dist/core/project/workflows.d.ts.map +1 -1
- package/dist/core/project/workflows.js +53 -7
- package/dist/core/project/workflows.js.map +1 -1
- package/dist/core/scaffold/auth-generator.d.ts.map +1 -1
- package/dist/core/scaffold/auth-generator.js +26 -27
- package/dist/core/scaffold/auth-generator.js.map +1 -1
- package/dist/core/scaffold/model-parser.d.ts +23 -0
- package/dist/core/scaffold/model-parser.d.ts.map +1 -1
- package/dist/core/scaffold/model-parser.js +407 -137
- package/dist/core/scaffold/model-parser.js.map +1 -1
- package/dist/core/scaffold/native-schema-generator.d.ts +13 -0
- package/dist/core/scaffold/native-schema-generator.d.ts.map +1 -1
- package/dist/core/scaffold/native-schema-generator.js +294 -91
- package/dist/core/scaffold/native-schema-generator.js.map +1 -1
- package/dist/core/scaffold/openapi-generator.d.ts +5 -0
- package/dist/core/scaffold/openapi-generator.d.ts.map +1 -1
- package/dist/core/scaffold/openapi-generator.js +53 -3
- package/dist/core/scaffold/openapi-generator.js.map +1 -1
- package/dist/core/scaffold/react-bff-generator.d.ts +10 -0
- package/dist/core/scaffold/react-bff-generator.d.ts.map +1 -0
- package/dist/core/scaffold/react-bff-generator.js +252 -0
- package/dist/core/scaffold/react-bff-generator.js.map +1 -0
- package/dist/core/scaffold/react-ui-generator.d.ts +6 -0
- package/dist/core/scaffold/react-ui-generator.d.ts.map +1 -0
- package/dist/core/scaffold/react-ui-generator.js +41 -0
- package/dist/core/scaffold/react-ui-generator.js.map +1 -0
- package/dist/core/scaffold/ui-generator.d.ts +0 -3
- package/dist/core/scaffold/ui-generator.d.ts.map +1 -1
- package/dist/core/scaffold/ui-generator.js +68 -7
- package/dist/core/scaffold/ui-generator.js.map +1 -1
- package/dist/core/verify/index.d.ts +4 -3
- package/dist/core/verify/index.d.ts.map +1 -1
- package/dist/core/verify/index.js +122 -5
- package/dist/core/verify/index.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/machine/contracts.d.ts +1 -0
- package/dist/machine/contracts.d.ts.map +1 -1
- package/dist/machine/errors.d.ts +3 -2
- package/dist/machine/errors.d.ts.map +1 -1
- package/dist/machine/errors.js +2 -1
- package/dist/machine/errors.js.map +1 -1
- package/dist/machine/index.d.ts.map +1 -1
- package/dist/machine/index.js +61 -30
- package/dist/machine/index.js.map +1 -1
- package/dist/mcp/index.d.ts +6 -4
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +210 -25
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/version-selection.d.ts +55 -0
- package/dist/mcp/version-selection.d.ts.map +1 -0
- package/dist/mcp/version-selection.js +152 -0
- package/dist/mcp/version-selection.js.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/utils/azure-cli.d.ts +56 -0
- package/dist/utils/azure-cli.d.ts.map +1 -0
- package/dist/utils/azure-cli.js +326 -0
- package/dist/utils/azure-cli.js.map +1 -0
- package/dist/utils/azure-diagnostics.d.ts +16 -0
- package/dist/utils/azure-diagnostics.d.ts.map +1 -0
- package/dist/utils/azure-diagnostics.js +68 -0
- package/dist/utils/azure-diagnostics.js.map +1 -0
- package/dist/utils/package-manager.d.ts +2 -2
- package/dist/utils/package-manager.d.ts.map +1 -1
- package/dist/utils/package-manager.js +6 -3
- package/dist/utils/package-manager.js.map +1 -1
- package/dist/utils/python-uv.d.ts +5 -7
- package/dist/utils/python-uv.d.ts.map +1 -1
- package/dist/utils/python-uv.js +50 -24
- package/dist/utils/python-uv.js.map +1 -1
- package/dist/utils/react-dev.d.ts +4 -0
- package/dist/utils/react-dev.d.ts.map +1 -0
- package/dist/utils/react-dev.js +35 -0
- package/dist/utils/react-dev.js.map +1 -0
- package/dist/utils/semver.d.ts +14 -0
- package/dist/utils/semver.d.ts.map +1 -0
- package/dist/utils/semver.js +61 -0
- package/dist/utils/semver.js.map +1 -0
- package/package.json +7 -4
- package/src/__tests__/__snapshots__/boundaries.test.ts.snap +154 -0
- package/src/__tests__/agent-instructions.test.ts +6 -1
- package/src/__tests__/agent-loop-phase34.test.ts +143 -8
- package/src/__tests__/auth.test.ts +9 -0
- package/src/__tests__/azure-cli.test.ts +202 -0
- package/src/__tests__/boundaries.test.ts +42 -0
- package/src/__tests__/csharp-schema.test.ts +261 -0
- package/src/__tests__/dev.test.ts +13 -6
- package/src/__tests__/fixtures/az wrapper.cmd +2 -0
- package/src/__tests__/fixtures/azure-cli.cjs +153 -0
- package/src/__tests__/fixtures/provision-runner.cjs +28 -0
- package/src/__tests__/generated-project-matrix.test.ts +789 -0
- package/src/__tests__/home-page.test.ts +50 -0
- package/src/__tests__/init.test.ts +145 -5
- package/src/__tests__/mcp-version.test.ts +262 -0
- package/src/__tests__/mcp.test.ts +120 -3
- package/src/__tests__/multi-auth.test.ts +115 -4
- package/src/__tests__/package-manager.test.ts +17 -2
- package/src/__tests__/plan-apply.test.ts +179 -0
- package/src/__tests__/provision-runtime.test.ts +722 -0
- package/src/__tests__/python-uv.test.ts +50 -14
- package/src/__tests__/python-venv.test.ts +48 -0
- package/src/__tests__/react-dev.test.ts +72 -0
- package/src/__tests__/react.test.ts +838 -0
- package/src/__tests__/release-metadata.test.ts +121 -0
- package/src/__tests__/schema-analysis.test.ts +329 -0
- package/src/cli/commands/add-auth.ts +103 -29
- package/src/cli/commands/configure-mcp.ts +54 -0
- package/src/cli/commands/dev.ts +145 -73
- package/src/cli/commands/init.ts +147 -172
- package/src/cli/commands/migrations.ts +22 -0
- package/src/cli/commands/provision.ts +294 -56
- package/src/cli/commands/scaffold.ts +145 -22
- package/src/cli/index.ts +46 -10
- package/src/core/config.ts +21 -1
- package/src/core/operations/create-model.ts +51 -13
- package/src/core/operations/migration-generator.ts +14 -1
- package/src/core/operations/model-migration.ts +52 -0
- package/src/core/operations/provision-deployment.ts +183 -0
- package/src/core/operations/provision-operations.ts +662 -154
- package/src/core/operations/scaffold-apply.ts +2 -0
- package/src/core/operations/scaffold-machine.ts +1 -0
- package/src/core/operations/scaffold-plan.ts +5 -1
- package/src/core/project/agent-instructions.ts +17 -1
- package/src/core/project/agent-templates/AGENTS.md +113 -15
- package/src/core/project/agent-templates/CLAUDE.md +14 -1
- package/src/core/project/agent-templates/bff-routes.instructions.md +5 -3
- package/src/core/project/agent-templates/copilot-instructions.md +15 -1
- package/src/core/project/agent-templates/shared-models.instructions.md +11 -3
- package/src/core/project/boundaries.ts +62 -20
- package/src/core/project/capabilities.ts +63 -6
- package/src/core/project/home-page.ts +114 -0
- package/src/core/project/manifest.ts +52 -27
- package/src/core/project/mcp-config.ts +84 -0
- package/src/core/project/react-project.ts +529 -0
- package/src/core/project/state.ts +70 -0
- package/src/core/project/validation.ts +32 -10
- package/src/core/project/workflows.ts +53 -7
- package/src/core/scaffold/auth-generator.ts +26 -27
- package/src/core/scaffold/model-parser.ts +413 -129
- package/src/core/scaffold/native-schema-generator.ts +319 -103
- package/src/core/scaffold/openapi-generator.ts +63 -5
- package/src/core/scaffold/react-bff-generator.ts +292 -0
- package/src/core/scaffold/react-ui-generator.ts +51 -0
- package/src/core/scaffold/ui-generator.ts +70 -8
- package/src/core/verify/index.ts +118 -6
- package/src/index.ts +3 -1
- package/src/machine/contracts.ts +1 -0
- package/src/machine/errors.ts +4 -2
- package/src/machine/index.ts +67 -36
- package/src/mcp/index.ts +221 -31
- package/src/mcp/version-selection.ts +163 -0
- package/src/types/index.ts +4 -0
- package/src/utils/azure-cli.ts +315 -0
- package/src/utils/azure-diagnostics.ts +75 -0
- package/src/utils/package-manager.ts +7 -4
- package/src/utils/python-uv.ts +53 -33
- package/src/utils/react-dev.ts +31 -0
- package/src/utils/semver.ts +59 -0
|
@@ -0,0 +1,838 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import * as net from "net";
|
|
4
|
+
import ts from "typescript";
|
|
5
|
+
import type { Mock } from "jest-mock";
|
|
6
|
+
import { z } from "zod/v4";
|
|
7
|
+
import {
|
|
8
|
+
getFrontendFramework,
|
|
9
|
+
loadConfig,
|
|
10
|
+
validateConfig,
|
|
11
|
+
} from "../core/config";
|
|
12
|
+
import {
|
|
13
|
+
createReactProject,
|
|
14
|
+
createReactBffProject,
|
|
15
|
+
buildReactSwaConfig,
|
|
16
|
+
} from "../core/project/react-project";
|
|
17
|
+
import { buildHomePage } from "../core/project/home-page";
|
|
18
|
+
import {
|
|
19
|
+
generateManagedFunctionConfig,
|
|
20
|
+
generateReactBffHelper,
|
|
21
|
+
generateReactBffRoute,
|
|
22
|
+
generateReactAuthBff,
|
|
23
|
+
} from "../core/scaffold/react-bff-generator";
|
|
24
|
+
import { createBasicModelInfo, createRdbConnectorModelInfo } from "./fixtures";
|
|
25
|
+
import { planScaffoldOperation } from "../core/operations/scaffold-plan";
|
|
26
|
+
import { applyScaffoldOperation } from "../core/operations/scaffold-apply";
|
|
27
|
+
import { buildProjectManifest } from "../core/project/manifest";
|
|
28
|
+
import { validateProject } from "../core/project/validation";
|
|
29
|
+
import {
|
|
30
|
+
buildAgentsMd,
|
|
31
|
+
buildPathSpecificInstructions,
|
|
32
|
+
} from "../core/project/agent-instructions";
|
|
33
|
+
import {
|
|
34
|
+
buildGitHubSwaWorkflow,
|
|
35
|
+
buildAzureSwaPipeline,
|
|
36
|
+
buildSwallowKitConfigSource,
|
|
37
|
+
} from "../cli/commands/init";
|
|
38
|
+
import { getBuildScript } from "../utils/package-manager";
|
|
39
|
+
import {
|
|
40
|
+
assertDevPortAvailable,
|
|
41
|
+
buildReactDevArgs,
|
|
42
|
+
buildSwaStartArgs,
|
|
43
|
+
} from "../cli/commands/dev";
|
|
44
|
+
import { addAuthCommand } from "../cli/commands/add-auth";
|
|
45
|
+
import { captureConsoleMessages } from "../core/operations/runtime";
|
|
46
|
+
|
|
47
|
+
interface TestRequest {
|
|
48
|
+
method: string;
|
|
49
|
+
headers: Record<string, string>;
|
|
50
|
+
params: Record<string, string>;
|
|
51
|
+
query?: Record<string, string>;
|
|
52
|
+
body?: unknown;
|
|
53
|
+
}
|
|
54
|
+
interface TestResponse {
|
|
55
|
+
status: number;
|
|
56
|
+
body?: unknown;
|
|
57
|
+
headers?: Record<string, string>;
|
|
58
|
+
}
|
|
59
|
+
interface Helper {
|
|
60
|
+
callFunction(config: {
|
|
61
|
+
request: TestRequest;
|
|
62
|
+
method: string;
|
|
63
|
+
path: string;
|
|
64
|
+
body?: unknown;
|
|
65
|
+
inputSchema?: z.ZodType;
|
|
66
|
+
responseSchema?: z.ZodType;
|
|
67
|
+
}): Promise<TestResponse>;
|
|
68
|
+
}
|
|
69
|
+
type Handler = (
|
|
70
|
+
_context: object,
|
|
71
|
+
request: TestRequest
|
|
72
|
+
) => Promise<TestResponse>;
|
|
73
|
+
|
|
74
|
+
function evaluate<T>(source: string, modules: Record<string, unknown> = {}): T {
|
|
75
|
+
const { outputText, diagnostics } = ts.transpileModule(source, {
|
|
76
|
+
compilerOptions: {
|
|
77
|
+
module: ts.ModuleKind.CommonJS,
|
|
78
|
+
target: ts.ScriptTarget.ES2022,
|
|
79
|
+
esModuleInterop: true,
|
|
80
|
+
},
|
|
81
|
+
reportDiagnostics: true,
|
|
82
|
+
});
|
|
83
|
+
expect(diagnostics).toEqual([]);
|
|
84
|
+
const output = { exports: {} };
|
|
85
|
+
new Function("require", "module", "exports", outputText)(
|
|
86
|
+
(name: string) => {
|
|
87
|
+
if (name === "zod/v4") return { z };
|
|
88
|
+
if (name in modules) return modules[name];
|
|
89
|
+
throw new Error(`Unexpected generated runtime import: ${name}`);
|
|
90
|
+
},
|
|
91
|
+
output,
|
|
92
|
+
output.exports
|
|
93
|
+
);
|
|
94
|
+
return output.exports as T;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function request(overrides: Partial<TestRequest> = {}): TestRequest {
|
|
98
|
+
return { method: "GET", headers: {}, params: {}, ...overrides };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
describe("React managed BFF runtime", () => {
|
|
102
|
+
const originalEnv = { ...process.env };
|
|
103
|
+
let fetchMock: Mock<typeof fetch>;
|
|
104
|
+
function fetchInit(index = 0) {
|
|
105
|
+
const init = fetchMock.mock.calls[index][1];
|
|
106
|
+
const headers: Record<string, string> = {};
|
|
107
|
+
new Headers(init?.headers).forEach((value, key) => {
|
|
108
|
+
headers[key] = value;
|
|
109
|
+
});
|
|
110
|
+
return { ...init, headers };
|
|
111
|
+
}
|
|
112
|
+
beforeEach(() => {
|
|
113
|
+
process.env.BACKEND_FUNCTIONS_BASE_URL = "https://backend.example.test";
|
|
114
|
+
process.env.BACKEND_FUNCTIONS_KEY = "server-only-key";
|
|
115
|
+
fetchMock = jest.fn<typeof fetch>(
|
|
116
|
+
async () =>
|
|
117
|
+
new Response(JSON.stringify({ id: "one", title: "Hello" }), {
|
|
118
|
+
status: 200,
|
|
119
|
+
})
|
|
120
|
+
);
|
|
121
|
+
jest.spyOn(global, "fetch").mockImplementation(fetchMock);
|
|
122
|
+
jest.spyOn(console, "error").mockImplementation(() => {});
|
|
123
|
+
});
|
|
124
|
+
afterEach(() => {
|
|
125
|
+
process.env = { ...originalEnv };
|
|
126
|
+
jest.restoreAllMocks();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("uses the server key and ignores client-supplied keys/credentials when auth is disabled", async () => {
|
|
130
|
+
const helper = evaluate<Helper>(generateReactBffHelper("sample"));
|
|
131
|
+
const response = await helper.callFunction({
|
|
132
|
+
request: request({
|
|
133
|
+
headers: {
|
|
134
|
+
"x-functions-key": "attacker-key",
|
|
135
|
+
authorization: "Bearer attacker",
|
|
136
|
+
},
|
|
137
|
+
}),
|
|
138
|
+
method: "GET",
|
|
139
|
+
path: "/api/todo",
|
|
140
|
+
});
|
|
141
|
+
expect(response.status).toBe(200);
|
|
142
|
+
expect(fetchMock.mock.calls[0][0]).toBe(
|
|
143
|
+
"https://backend.example.test/api/todo"
|
|
144
|
+
);
|
|
145
|
+
expect(fetchInit().headers).toEqual({
|
|
146
|
+
"content-type": "application/json",
|
|
147
|
+
"x-functions-key": "server-only-key",
|
|
148
|
+
});
|
|
149
|
+
expect(fetchInit().redirect).toBe("error");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("fails closed for remote backends without a key, including localhost-lookalike hosts", async () => {
|
|
153
|
+
delete process.env.BACKEND_FUNCTIONS_KEY;
|
|
154
|
+
const helper = evaluate<Helper>(generateReactBffHelper("sample"));
|
|
155
|
+
for (const base of [
|
|
156
|
+
"https://backend.example.test",
|
|
157
|
+
"http://localhost:1234@evil.example.test",
|
|
158
|
+
"http://localhost.example.test:7071",
|
|
159
|
+
]) {
|
|
160
|
+
process.env.BACKEND_FUNCTIONS_BASE_URL = base;
|
|
161
|
+
expect(
|
|
162
|
+
(
|
|
163
|
+
await helper.callFunction({
|
|
164
|
+
request: request(),
|
|
165
|
+
method: "GET",
|
|
166
|
+
path: "/api/todo",
|
|
167
|
+
})
|
|
168
|
+
).status
|
|
169
|
+
).toBe(502);
|
|
170
|
+
}
|
|
171
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
172
|
+
process.env.BACKEND_FUNCTIONS_BASE_URL = "http://localhost:7071";
|
|
173
|
+
expect(
|
|
174
|
+
(
|
|
175
|
+
await helper.callFunction({
|
|
176
|
+
request: request(),
|
|
177
|
+
method: "GET",
|
|
178
|
+
path: "/api/todo",
|
|
179
|
+
})
|
|
180
|
+
).status
|
|
181
|
+
).toBe(200);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("validates input before forwarding and rejects invalid backend output", async () => {
|
|
185
|
+
const helper = evaluate<Helper>(generateReactBffHelper("sample"));
|
|
186
|
+
const config = {
|
|
187
|
+
request: request(),
|
|
188
|
+
method: "POST",
|
|
189
|
+
path: "/api/todo",
|
|
190
|
+
inputSchema: z.object({ title: z.string().min(1) }),
|
|
191
|
+
};
|
|
192
|
+
expect(
|
|
193
|
+
(await helper.callFunction({ ...config, body: { title: "" } })).status
|
|
194
|
+
).toBe(400);
|
|
195
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
196
|
+
expect(
|
|
197
|
+
(
|
|
198
|
+
await helper.callFunction({
|
|
199
|
+
...config,
|
|
200
|
+
body: { title: "valid" },
|
|
201
|
+
responseSchema: z.object({ count: z.number() }),
|
|
202
|
+
})
|
|
203
|
+
).status
|
|
204
|
+
).toBe(502);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("preserves backend errors and bodyless DELETE responses", async () => {
|
|
208
|
+
const helper = evaluate<Helper>(generateReactBffHelper("sample"));
|
|
209
|
+
fetchMock.mockResolvedValueOnce(new Response("forbidden", { status: 403 }));
|
|
210
|
+
expect(
|
|
211
|
+
(
|
|
212
|
+
await helper.callFunction({
|
|
213
|
+
request: request(),
|
|
214
|
+
method: "GET",
|
|
215
|
+
path: "/api/todo",
|
|
216
|
+
})
|
|
217
|
+
).status
|
|
218
|
+
).toBe(403);
|
|
219
|
+
fetchMock.mockResolvedValueOnce(new Response(null, { status: 204 }));
|
|
220
|
+
expect(
|
|
221
|
+
await helper.callFunction({
|
|
222
|
+
request: request(),
|
|
223
|
+
method: "DELETE",
|
|
224
|
+
path: "/api/todo/one",
|
|
225
|
+
})
|
|
226
|
+
).toEqual({ status: 204 });
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("forwards normalized SWA/bearer credentials only from the current request", async () => {
|
|
230
|
+
const helper = evaluate<Helper>(
|
|
231
|
+
generateReactBffHelper("sample", {
|
|
232
|
+
schemes: {
|
|
233
|
+
staff: { provider: "swa" },
|
|
234
|
+
external: { provider: "external-token" },
|
|
235
|
+
},
|
|
236
|
+
})
|
|
237
|
+
);
|
|
238
|
+
const principal = {
|
|
239
|
+
identityProvider: "aad",
|
|
240
|
+
userId: "user",
|
|
241
|
+
userRoles: ["authenticated", 42],
|
|
242
|
+
extra: "not-forwarded",
|
|
243
|
+
};
|
|
244
|
+
await helper.callFunction({
|
|
245
|
+
request: request({
|
|
246
|
+
headers: {
|
|
247
|
+
authorization: "Bearer token",
|
|
248
|
+
"x-ms-client-principal": Buffer.from(
|
|
249
|
+
JSON.stringify(principal)
|
|
250
|
+
).toString("base64"),
|
|
251
|
+
},
|
|
252
|
+
}),
|
|
253
|
+
method: "GET",
|
|
254
|
+
path: "/api/todo",
|
|
255
|
+
});
|
|
256
|
+
const headers = fetchInit().headers;
|
|
257
|
+
expect(headers.authorization).toBe("Bearer token");
|
|
258
|
+
expect(
|
|
259
|
+
JSON.parse(
|
|
260
|
+
Buffer.from(headers["x-ms-client-principal"], "base64").toString()
|
|
261
|
+
)
|
|
262
|
+
).toEqual({
|
|
263
|
+
identityProvider: "aad",
|
|
264
|
+
userId: "user",
|
|
265
|
+
userDetails: "",
|
|
266
|
+
userRoles: ["authenticated"],
|
|
267
|
+
});
|
|
268
|
+
await helper.callFunction({
|
|
269
|
+
request: request(),
|
|
270
|
+
method: "GET",
|
|
271
|
+
path: "/api/todo",
|
|
272
|
+
});
|
|
273
|
+
expect(fetchInit(1).headers.authorization).toBeUndefined();
|
|
274
|
+
expect(fetchInit(1).headers["x-ms-client-principal"]).toBeUndefined();
|
|
275
|
+
expect(
|
|
276
|
+
(
|
|
277
|
+
await helper.callFunction({
|
|
278
|
+
request: request({ headers: { "x-ms-client-principal": "invalid" } }),
|
|
279
|
+
method: "GET",
|
|
280
|
+
path: "/api/todo",
|
|
281
|
+
})
|
|
282
|
+
).status
|
|
283
|
+
).toBe(401);
|
|
284
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("converts the HttpOnly JWT cookie to a backend bearer token, without forwarding cookies", async () => {
|
|
288
|
+
const helper = evaluate<Helper>(
|
|
289
|
+
generateReactBffHelper("sample", { provider: "custom-jwt" })
|
|
290
|
+
);
|
|
291
|
+
await helper.callFunction({
|
|
292
|
+
request: request({
|
|
293
|
+
headers: { cookie: "other=ignored; sample-auth-token=jwt-token" },
|
|
294
|
+
}),
|
|
295
|
+
method: "GET",
|
|
296
|
+
path: "/api/todo",
|
|
297
|
+
});
|
|
298
|
+
expect(fetchInit().headers.authorization).toBe("Bearer jwt-token");
|
|
299
|
+
expect(fetchInit().headers.cookie).toBeUndefined();
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
function crud(model = createBasicModelInfo()): Handler {
|
|
303
|
+
const helper = evaluate<Helper>(generateReactBffHelper("sample"));
|
|
304
|
+
return evaluate<{ default: Handler }>(
|
|
305
|
+
generateReactBffRoute(model, "@sample/shared"),
|
|
306
|
+
{
|
|
307
|
+
"../lib/call-function": helper,
|
|
308
|
+
"@sample/shared": {
|
|
309
|
+
todoSchema: z.object({
|
|
310
|
+
id: z.string(),
|
|
311
|
+
title: z.string().min(1),
|
|
312
|
+
createdAt: z.string().optional(),
|
|
313
|
+
updatedAt: z.string().optional(),
|
|
314
|
+
}),
|
|
315
|
+
userSchema: z.object({
|
|
316
|
+
id: z.string(),
|
|
317
|
+
title: z.string(),
|
|
318
|
+
createdAt: z.string().optional(),
|
|
319
|
+
updatedAt: z.string().optional(),
|
|
320
|
+
}),
|
|
321
|
+
},
|
|
322
|
+
}
|
|
323
|
+
).default;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
it("handles CRUD, encoded IDs, schema-owned fields and malformed JSON", async () => {
|
|
327
|
+
const handler = crud();
|
|
328
|
+
expect(
|
|
329
|
+
(
|
|
330
|
+
await handler(
|
|
331
|
+
{},
|
|
332
|
+
request({
|
|
333
|
+
method: "POST",
|
|
334
|
+
body: { id: "client-id", title: "Hello", createdAt: "client-time" },
|
|
335
|
+
})
|
|
336
|
+
)
|
|
337
|
+
).status
|
|
338
|
+
).toBe(201);
|
|
339
|
+
expect(fetchInit().body).toBe(JSON.stringify({ title: "Hello" }));
|
|
340
|
+
expect(
|
|
341
|
+
(await handler({}, request({ params: { id: "a/b?#" } }))).status
|
|
342
|
+
).toBe(200);
|
|
343
|
+
expect(fetchMock.mock.calls[1][0]).toBe(
|
|
344
|
+
"https://backend.example.test/api/todo/a%2Fb%3F%23"
|
|
345
|
+
);
|
|
346
|
+
expect(
|
|
347
|
+
(await handler({}, request({ method: "POST", body: "{broken" }))).status
|
|
348
|
+
).toBe(400);
|
|
349
|
+
expect(
|
|
350
|
+
(
|
|
351
|
+
await handler(
|
|
352
|
+
{},
|
|
353
|
+
request({ method: "POST", params: { id: "one" }, body: {} })
|
|
354
|
+
)
|
|
355
|
+
).status
|
|
356
|
+
).toBe(405);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it("does not generate writable connector operations for read-only models", async () => {
|
|
360
|
+
const handler = crud(createRdbConnectorModelInfo());
|
|
361
|
+
expect(
|
|
362
|
+
(await handler({}, request({ method: "POST", body: { title: "Hello" } })))
|
|
363
|
+
.status
|
|
364
|
+
).toBe(405);
|
|
365
|
+
expect(
|
|
366
|
+
(await handler({}, request({ method: "DELETE", params: { id: "one" } })))
|
|
367
|
+
.status
|
|
368
|
+
).toBe(405);
|
|
369
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it("keeps login tokens in an HttpOnly cookie and includes the backend key", async () => {
|
|
373
|
+
process.env.NODE_ENV = "production";
|
|
374
|
+
const helper = evaluate<Helper>(
|
|
375
|
+
generateReactBffHelper("sample", { provider: "custom-jwt" })
|
|
376
|
+
);
|
|
377
|
+
const user = {
|
|
378
|
+
id: "one",
|
|
379
|
+
loginId: "test",
|
|
380
|
+
name: "Test",
|
|
381
|
+
email: "",
|
|
382
|
+
roles: [],
|
|
383
|
+
};
|
|
384
|
+
fetchMock.mockResolvedValueOnce(
|
|
385
|
+
new Response(JSON.stringify({ token: "private-token", user }))
|
|
386
|
+
);
|
|
387
|
+
const handler = evaluate<{ default: Handler }>(
|
|
388
|
+
generateReactAuthBff("sample", "@sample/shared", true),
|
|
389
|
+
{
|
|
390
|
+
"../lib/call-function": helper,
|
|
391
|
+
"@sample/shared": {
|
|
392
|
+
AuthUser: z.object({ id: z.string() }),
|
|
393
|
+
LoginRequest: z.object({ loginId: z.string(), password: z.string() }),
|
|
394
|
+
LoginResponse: z.object({ token: z.string(), user: z.unknown() }),
|
|
395
|
+
},
|
|
396
|
+
}
|
|
397
|
+
).default;
|
|
398
|
+
const result = await handler(
|
|
399
|
+
{},
|
|
400
|
+
request({
|
|
401
|
+
method: "POST",
|
|
402
|
+
params: { action: "login" },
|
|
403
|
+
body: { loginId: "test", password: "password" },
|
|
404
|
+
})
|
|
405
|
+
);
|
|
406
|
+
expect(result.body).toEqual({ user });
|
|
407
|
+
expect(result.headers?.["Set-Cookie"]).toContain("HttpOnly; SameSite=Lax");
|
|
408
|
+
expect(result.headers?.["Set-Cookie"]).toContain("; Secure");
|
|
409
|
+
expect(fetchInit().headers["x-functions-key"]).toBe("server-only-key");
|
|
410
|
+
expect(
|
|
411
|
+
(
|
|
412
|
+
await handler(
|
|
413
|
+
{},
|
|
414
|
+
request({ method: "POST", params: { action: "logout" } })
|
|
415
|
+
)
|
|
416
|
+
).headers?.["Set-Cookie"]
|
|
417
|
+
).toContain("Max-Age=0");
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
it("normalizes verified JWT subjects to the frontend user contract without exposing claims", async () => {
|
|
421
|
+
const helper = evaluate<Helper>(
|
|
422
|
+
generateReactBffHelper("sample", { provider: "custom-jwt" })
|
|
423
|
+
);
|
|
424
|
+
const handler = evaluate<{ default: Handler }>(
|
|
425
|
+
generateReactAuthBff("sample", "@sample/shared", true),
|
|
426
|
+
{
|
|
427
|
+
"../lib/call-function": helper,
|
|
428
|
+
"@sample/shared": {
|
|
429
|
+
AuthUser: z.object({ id: z.string(), roles: z.array(z.string()) }),
|
|
430
|
+
LoginRequest: z.object({ loginId: z.string(), password: z.string() }),
|
|
431
|
+
LoginResponse: z.object({ token: z.string(), user: z.unknown() }),
|
|
432
|
+
},
|
|
433
|
+
}
|
|
434
|
+
).default;
|
|
435
|
+
fetchMock.mockResolvedValueOnce(
|
|
436
|
+
new Response(JSON.stringify({ sub: "one", roles: ["admin"], exp: 123 }))
|
|
437
|
+
);
|
|
438
|
+
const result = await handler(
|
|
439
|
+
{},
|
|
440
|
+
request({
|
|
441
|
+
params: { action: "me" },
|
|
442
|
+
headers: { cookie: "sample-auth-token=private-token" },
|
|
443
|
+
})
|
|
444
|
+
);
|
|
445
|
+
expect(result).toMatchObject({
|
|
446
|
+
status: 200,
|
|
447
|
+
body: { id: "one", roles: ["admin"] },
|
|
448
|
+
});
|
|
449
|
+
expect(fetchInit().headers.authorization).toBe("Bearer private-token");
|
|
450
|
+
expect(result.body).not.toHaveProperty("sub");
|
|
451
|
+
expect(result.body).not.toHaveProperty("exp");
|
|
452
|
+
fetchMock.mockResolvedValueOnce(
|
|
453
|
+
new Response(JSON.stringify({ roles: ["admin"] }))
|
|
454
|
+
);
|
|
455
|
+
expect(
|
|
456
|
+
(await handler({}, request({ params: { action: "me" } }))).status
|
|
457
|
+
).toBe(502);
|
|
458
|
+
});
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
describe("React project integration", () => {
|
|
462
|
+
const originalCwd = process.cwd();
|
|
463
|
+
let project: string;
|
|
464
|
+
beforeEach(() => {
|
|
465
|
+
project = fs.mkdtempSync(path.join(originalCwd, ".tmp-react-test-"));
|
|
466
|
+
createReactProject(project, "sample");
|
|
467
|
+
createReactBffProject(project, "sample", "npm");
|
|
468
|
+
fs.mkdirSync(path.join(project, "functions", "src"), { recursive: true });
|
|
469
|
+
fs.mkdirSync(path.join(project, "shared", "models"), { recursive: true });
|
|
470
|
+
fs.writeFileSync(
|
|
471
|
+
path.join(project, "swallowkit.config.js"),
|
|
472
|
+
buildSwallowKitConfigSource("typescript", "react")
|
|
473
|
+
);
|
|
474
|
+
fs.writeFileSync(
|
|
475
|
+
path.join(project, "staticwebapp.config.json"),
|
|
476
|
+
JSON.stringify(buildReactSwaConfig())
|
|
477
|
+
);
|
|
478
|
+
fs.writeFileSync(
|
|
479
|
+
path.join(project, "shared", "package.json"),
|
|
480
|
+
JSON.stringify({ name: "@sample/shared" })
|
|
481
|
+
);
|
|
482
|
+
fs.writeFileSync(
|
|
483
|
+
path.join(project, "shared", "index.ts"),
|
|
484
|
+
"export { Todo } from './models/todo';\n"
|
|
485
|
+
);
|
|
486
|
+
fs.writeFileSync(
|
|
487
|
+
path.join(project, "shared", "models", "todo.ts"),
|
|
488
|
+
`import { z } from 'zod/v4';\nexport const Todo = z.object({ id: z.string(), title: z.string().min(1), createdAt: z.string().optional(), updatedAt: z.string().optional() });\n`
|
|
489
|
+
);
|
|
490
|
+
process.chdir(project);
|
|
491
|
+
});
|
|
492
|
+
afterEach(() => {
|
|
493
|
+
process.chdir(originalCwd);
|
|
494
|
+
fs.rmSync(project, { recursive: true, force: true });
|
|
495
|
+
jest.restoreAllMocks();
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
it("persists the frontend choice and preserves Next.js for existing projects", () => {
|
|
499
|
+
expect(getFrontendFramework()).toBe("react");
|
|
500
|
+
expect(loadConfig(undefined, true, true).frontend?.framework).toBe("react");
|
|
501
|
+
fs.rmSync(path.join(project, "swallowkit.config.js"));
|
|
502
|
+
fs.writeFileSync(path.join(project, "swallowkit.config.json"), "{}");
|
|
503
|
+
expect(getFrontendFramework()).toBe("nextjs");
|
|
504
|
+
expect(
|
|
505
|
+
validateConfig({ frontend: { framework: "invalid" as never } }).valid
|
|
506
|
+
).toBe(false);
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
it("generates the shared home page with the selected package manager and Tailwind defaults", () => {
|
|
510
|
+
createReactProject(project, "sample", "npm");
|
|
511
|
+
expect(fs.readFileSync(path.join(project, "src", "Home.tsx"), "utf8"))
|
|
512
|
+
.toBe(buildHomePage("react", "npm"));
|
|
513
|
+
|
|
514
|
+
const css = fs.readFileSync(path.join(project, "src", "style.css"), "utf8");
|
|
515
|
+
expect(css).toContain('@import "tailwindcss"');
|
|
516
|
+
expect(css).toContain("@media (prefers-color-scheme: dark)");
|
|
517
|
+
expect(css).toContain("--background: #ffffff");
|
|
518
|
+
expect(css).toContain("--background: #0a0a0a");
|
|
519
|
+
expect(css).toContain("--color-background: var(--background)");
|
|
520
|
+
expect(css).toContain("font-family: Arial, Helvetica, sans-serif");
|
|
521
|
+
expect(fs.readFileSync(path.join(project, "index.html"), "utf8"))
|
|
522
|
+
.toContain('class="h-full antialiased"');
|
|
523
|
+
expect(fs.readFileSync(path.join(project, "vite.config.mts"), "utf8"))
|
|
524
|
+
.toContain("plugins: [react(), tailwindcss()]");
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
it("plans and applies BFF bindings, React pages and metadata through the common path", async () => {
|
|
528
|
+
const plan = await planScaffoldOperation({ model: "todo" });
|
|
529
|
+
expect(plan.requiresApproval).toBe(false);
|
|
530
|
+
expect(plan.operations).toEqual(
|
|
531
|
+
expect.arrayContaining([
|
|
532
|
+
expect.objectContaining({
|
|
533
|
+
path: "bff/src/todo/index.ts",
|
|
534
|
+
ownership: "managed",
|
|
535
|
+
}),
|
|
536
|
+
expect.objectContaining({
|
|
537
|
+
path: "bff/todo/function.json",
|
|
538
|
+
ownership: "managed",
|
|
539
|
+
}),
|
|
540
|
+
expect.objectContaining({ path: "src/pages/todo/routes.tsx" }),
|
|
541
|
+
expect.objectContaining({ path: "functions/src/todo.ts" }),
|
|
542
|
+
])
|
|
543
|
+
);
|
|
544
|
+
expect(
|
|
545
|
+
plan.operations.some((operation) => operation.path.startsWith("app/"))
|
|
546
|
+
).toBe(false);
|
|
547
|
+
expect(
|
|
548
|
+
fs.existsSync(path.join(project, "bff", "src", "todo", "index.ts"))
|
|
549
|
+
).toBe(false);
|
|
550
|
+
await applyScaffoldOperation({ planId: plan.planId });
|
|
551
|
+
const { manifest } = await buildProjectManifest(project);
|
|
552
|
+
expect(manifest.frontendFramework).toBe("react");
|
|
553
|
+
expect(
|
|
554
|
+
manifest.routes.find((route) => route.name === "Todo-bff-detail")
|
|
555
|
+
).toMatchObject({ filePath: "bff/src/todo/index.ts", exists: true });
|
|
556
|
+
expect(
|
|
557
|
+
(await validateProject(project)).violations.filter(
|
|
558
|
+
(violation) => violation.severity === "error"
|
|
559
|
+
)
|
|
560
|
+
).toEqual([]);
|
|
561
|
+
const page = fs.readFileSync(
|
|
562
|
+
path.join(project, "src", "pages", "todo", "[id]", "page.tsx"),
|
|
563
|
+
"utf8"
|
|
564
|
+
);
|
|
565
|
+
expect(page).toContain("react-router-dom");
|
|
566
|
+
expect(page).not.toContain("next/");
|
|
567
|
+
const secondPlan = await planScaffoldOperation({ model: "todo" });
|
|
568
|
+
expect(secondPlan.requiresApproval).toBe(false);
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
it("generates only server artifacts with api-only", async () => {
|
|
572
|
+
const plan = await planScaffoldOperation({ model: "todo", apiOnly: true });
|
|
573
|
+
expect(
|
|
574
|
+
plan.operations.some((operation) => operation.path.startsWith("bff/"))
|
|
575
|
+
).toBe(true);
|
|
576
|
+
expect(
|
|
577
|
+
plan.operations.some((operation) =>
|
|
578
|
+
operation.path.startsWith("src/pages/")
|
|
579
|
+
)
|
|
580
|
+
).toBe(false);
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it.each<["csharp" | "python", string]>([
|
|
584
|
+
["csharp", "functions/Crud/TodoCrudFunctions.cs"],
|
|
585
|
+
["python", "functions/blueprints/todo.py"],
|
|
586
|
+
])(
|
|
587
|
+
"plans the same managed BFF and React UI with a %s standalone backend",
|
|
588
|
+
async (language, backendPath) => {
|
|
589
|
+
const source = buildSwallowKitConfigSource(language, "react");
|
|
590
|
+
const config = new Function(
|
|
591
|
+
source.replace("module.exports =", "return")
|
|
592
|
+
)();
|
|
593
|
+
fs.rmSync(path.join(project, "swallowkit.config.js"));
|
|
594
|
+
fs.writeFileSync(
|
|
595
|
+
path.join(project, "swallowkit.config.json"),
|
|
596
|
+
JSON.stringify(config)
|
|
597
|
+
);
|
|
598
|
+
if (language === "python") {
|
|
599
|
+
fs.writeFileSync(
|
|
600
|
+
path.join(project, "functions", "function_app.py"),
|
|
601
|
+
"import azure.functions as func\napp = func.FunctionApp()\n# SwallowKit scaffold registrations\n"
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
const plan = await planScaffoldOperation({ model: "todo" });
|
|
605
|
+
expect(plan.operations).toEqual(
|
|
606
|
+
expect.arrayContaining([
|
|
607
|
+
expect.objectContaining({ path: backendPath }),
|
|
608
|
+
expect.objectContaining({ path: "bff/src/todo/index.ts" }),
|
|
609
|
+
expect.objectContaining({ path: "bff/todo/function.json" }),
|
|
610
|
+
expect.objectContaining({ path: "src/pages/todo/routes.tsx" }),
|
|
611
|
+
])
|
|
612
|
+
);
|
|
613
|
+
expect(
|
|
614
|
+
plan.operations.some((operation) => operation.path.startsWith("app/"))
|
|
615
|
+
).toBe(false);
|
|
616
|
+
expect(fs.existsSync(path.join(project, backendPath))).toBe(false);
|
|
617
|
+
expect(getFrontendFramework()).toBe("react");
|
|
618
|
+
}
|
|
619
|
+
);
|
|
620
|
+
|
|
621
|
+
it("reports server dependencies and settings in browser code", async () => {
|
|
622
|
+
fs.writeFileSync(
|
|
623
|
+
path.join(project, "src", "unsafe.ts"),
|
|
624
|
+
"import { callFunction } from '../bff/src/lib/call-function';\nconst key = import.meta.env.VITE_BACKEND_FUNCTIONS_KEY;\n"
|
|
625
|
+
);
|
|
626
|
+
const result = await validateProject(project);
|
|
627
|
+
expect(result.violations).toEqual(
|
|
628
|
+
expect.arrayContaining([
|
|
629
|
+
expect.objectContaining({
|
|
630
|
+
code: "forbidden-client-backend-dependency",
|
|
631
|
+
severity: "error",
|
|
632
|
+
location: { path: "src/unsafe.ts" },
|
|
633
|
+
}),
|
|
634
|
+
])
|
|
635
|
+
);
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
it("detects manifest drift when the frontend changes without migrating routes", async () => {
|
|
639
|
+
const plan = await planScaffoldOperation({ model: "todo" });
|
|
640
|
+
await applyScaffoldOperation({ planId: plan.planId });
|
|
641
|
+
fs.rmSync(path.join(project, "swallowkit.config.js"));
|
|
642
|
+
fs.writeFileSync(
|
|
643
|
+
path.join(project, "swallowkit.config.json"),
|
|
644
|
+
JSON.stringify({ frontend: { framework: "nextjs" } })
|
|
645
|
+
);
|
|
646
|
+
expect((await validateProject(project)).violations).toEqual(
|
|
647
|
+
expect.arrayContaining([
|
|
648
|
+
expect.objectContaining({ code: "manifest-out-of-date" }),
|
|
649
|
+
])
|
|
650
|
+
);
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
it.each(["swa", "external-token", "custom-jwt"])(
|
|
654
|
+
"generates %s auth without Next.js imports",
|
|
655
|
+
async (provider) => {
|
|
656
|
+
await captureConsoleMessages(async () => addAuthCommand({ provider }));
|
|
657
|
+
const helper = fs.readFileSync(
|
|
658
|
+
path.join(project, "bff", "src", "lib", "call-function.ts"),
|
|
659
|
+
"utf8"
|
|
660
|
+
);
|
|
661
|
+
expect(helper).not.toContain("next/");
|
|
662
|
+
expect(helper).toContain(
|
|
663
|
+
provider === "swa"
|
|
664
|
+
? "const forwardSwa = true"
|
|
665
|
+
: "const forwardBearer = true"
|
|
666
|
+
);
|
|
667
|
+
const context = fs.readFileSync(
|
|
668
|
+
path.join(project, "lib", "auth", "auth-context.tsx"),
|
|
669
|
+
"utf8"
|
|
670
|
+
);
|
|
671
|
+
expect(context).not.toContain("next/");
|
|
672
|
+
expect(fs.existsSync(path.join(project, "proxy.ts"))).toBe(false);
|
|
673
|
+
expect(fs.existsSync(path.join(project, "app"))).toBe(false);
|
|
674
|
+
if (provider !== "external-token")
|
|
675
|
+
expect(
|
|
676
|
+
fs.existsSync(path.join(project, "src", "pages", "login.tsx"))
|
|
677
|
+
).toBe(true);
|
|
678
|
+
// JSON config avoids Jest's CJS module cache when reading the just-updated config.
|
|
679
|
+
const source = fs.readFileSync(
|
|
680
|
+
path.join(project, "swallowkit.config.js"),
|
|
681
|
+
"utf8"
|
|
682
|
+
);
|
|
683
|
+
const config = new Function(
|
|
684
|
+
source.replace("module.exports =", "return")
|
|
685
|
+
)();
|
|
686
|
+
fs.rmSync(path.join(project, "swallowkit.config.js"));
|
|
687
|
+
fs.writeFileSync(
|
|
688
|
+
path.join(project, "swallowkit.config.json"),
|
|
689
|
+
JSON.stringify(config)
|
|
690
|
+
);
|
|
691
|
+
const { manifest } = await buildProjectManifest(project);
|
|
692
|
+
expect(
|
|
693
|
+
manifest.routes.find((route) => route.name === "auth-login")?.exists
|
|
694
|
+
).toBe(provider === "custom-jwt");
|
|
695
|
+
expect(
|
|
696
|
+
manifest.routes.find((route) => route.name === "auth-me")?.exists
|
|
697
|
+
).toBe(provider !== "swa");
|
|
698
|
+
}
|
|
699
|
+
);
|
|
700
|
+
|
|
701
|
+
it("adds multiple named credential transports and model-scoped providers", async () => {
|
|
702
|
+
fs.rmSync(path.join(project, "swallowkit.config.js"));
|
|
703
|
+
fs.writeFileSync(
|
|
704
|
+
path.join(project, "swallowkit.config.json"),
|
|
705
|
+
JSON.stringify({
|
|
706
|
+
frontend: { framework: "react" },
|
|
707
|
+
auth: {
|
|
708
|
+
schemes: {},
|
|
709
|
+
authorization: { defaultPolicy: "anonymous", policies: {} },
|
|
710
|
+
},
|
|
711
|
+
})
|
|
712
|
+
);
|
|
713
|
+
await captureConsoleMessages(async () =>
|
|
714
|
+
addAuthCommand({
|
|
715
|
+
provider: "swa",
|
|
716
|
+
scheme: "staff",
|
|
717
|
+
allowedProviders: ["github"],
|
|
718
|
+
})
|
|
719
|
+
);
|
|
720
|
+
await captureConsoleMessages(async () =>
|
|
721
|
+
addAuthCommand({ provider: "external-token", scheme: "customers" })
|
|
722
|
+
);
|
|
723
|
+
const helper = fs.readFileSync(
|
|
724
|
+
path.join(project, "bff", "src", "lib", "call-function.ts"),
|
|
725
|
+
"utf8"
|
|
726
|
+
);
|
|
727
|
+
expect(helper).toContain("const forwardSwa = true");
|
|
728
|
+
expect(helper).toContain("const forwardBearer = true");
|
|
729
|
+
expect(
|
|
730
|
+
fs.existsSync(
|
|
731
|
+
path.join(
|
|
732
|
+
project,
|
|
733
|
+
"lib",
|
|
734
|
+
"auth",
|
|
735
|
+
"schemes",
|
|
736
|
+
"customers",
|
|
737
|
+
"token-adapter.ts"
|
|
738
|
+
)
|
|
739
|
+
)
|
|
740
|
+
).toBe(true);
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
it("describes framework-specific paths, development and deployment to agents", () => {
|
|
744
|
+
const context = {
|
|
745
|
+
projectName: "sample",
|
|
746
|
+
backendLanguage: "typescript" as const,
|
|
747
|
+
packageManager: "npm" as const,
|
|
748
|
+
frontend: "react" as const,
|
|
749
|
+
};
|
|
750
|
+
const agents = buildAgentsMd(context);
|
|
751
|
+
expect(agents).toContain("React + Vite SPA");
|
|
752
|
+
expect(agents).toContain("SWA managed Functions");
|
|
753
|
+
expect(agents).toContain("never directly to standalone Functions");
|
|
754
|
+
expect(agents).toContain("do not start/restart");
|
|
755
|
+
expect(agents).not.toContain("Next.js API");
|
|
756
|
+
expect(
|
|
757
|
+
buildPathSpecificInstructions(context)["bff-routes.instructions.md"]
|
|
758
|
+
).toContain('applyTo: "bff/**"');
|
|
759
|
+
});
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
describe("React deployment and development contracts", () => {
|
|
763
|
+
it("refuses occupied ports without stopping the existing listener", async () => {
|
|
764
|
+
const server = net.createServer();
|
|
765
|
+
await new Promise<void>((resolve) =>
|
|
766
|
+
server.listen(0, "127.0.0.1", resolve)
|
|
767
|
+
);
|
|
768
|
+
try {
|
|
769
|
+
const address = server.address();
|
|
770
|
+
if (!address || typeof address === "string")
|
|
771
|
+
throw new Error("Expected TCP address");
|
|
772
|
+
await expect(
|
|
773
|
+
assertDevPortAvailable(address.port, "127.0.0.1")
|
|
774
|
+
).rejects.toThrow("unavailable");
|
|
775
|
+
expect(server.listening).toBe(true);
|
|
776
|
+
} finally {
|
|
777
|
+
await new Promise<void>((resolve, reject) =>
|
|
778
|
+
server.close((error) => (error ? reject(error) : resolve()))
|
|
779
|
+
);
|
|
780
|
+
}
|
|
781
|
+
});
|
|
782
|
+
|
|
783
|
+
it.each(["npm", "pnpm"] as const)(
|
|
784
|
+
"deploys prebuilt SPA and managed BFF using %s, never the standalone backend",
|
|
785
|
+
(pm) => {
|
|
786
|
+
for (const workflow of [
|
|
787
|
+
buildGitHubSwaWorkflow(pm, pm, "react"),
|
|
788
|
+
buildAzureSwaPipeline(pm, pm, "react"),
|
|
789
|
+
]) {
|
|
790
|
+
expect(workflow).toContain("app_location: dist");
|
|
791
|
+
expect(workflow).toContain("api_location: bff");
|
|
792
|
+
expect(workflow).toContain("skip_app_build: true");
|
|
793
|
+
expect(workflow).toContain("skip_api_build: true");
|
|
794
|
+
expect(workflow).toContain("bff/**");
|
|
795
|
+
expect(workflow).toContain("staticwebapp.config.json");
|
|
796
|
+
expect(workflow).not.toContain("api_location: functions");
|
|
797
|
+
expect(workflow).not.toContain("next build");
|
|
798
|
+
}
|
|
799
|
+
expect(getBuildScript(pm, "react")).toContain("copyFileSync");
|
|
800
|
+
expect(getBuildScript(pm, "react")).not.toContain("next ");
|
|
801
|
+
}
|
|
802
|
+
);
|
|
803
|
+
|
|
804
|
+
it("binds managed HTTP functions and excludes APIs from SPA fallback", () => {
|
|
805
|
+
const binding = JSON.parse(
|
|
806
|
+
generateManagedFunctionConfig("todo/{id?}", ["GET"], "todo")
|
|
807
|
+
);
|
|
808
|
+
expect(binding.scriptFile).toBe("../dist/todo/index.js");
|
|
809
|
+
expect(binding.bindings[0]).toMatchObject({
|
|
810
|
+
authLevel: "anonymous",
|
|
811
|
+
route: "todo/{id?}",
|
|
812
|
+
type: "httpTrigger",
|
|
813
|
+
methods: ["get"],
|
|
814
|
+
});
|
|
815
|
+
expect(binding.bindings[1]).toEqual({
|
|
816
|
+
type: "http",
|
|
817
|
+
direction: "out",
|
|
818
|
+
name: "$return",
|
|
819
|
+
});
|
|
820
|
+
expect(buildReactSwaConfig().navigationFallback.exclude).toEqual(
|
|
821
|
+
expect.arrayContaining(["/api/*", "/.auth/*"])
|
|
822
|
+
);
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
it("routes SWA only to the managed BFF and starts Vite instead of Next.js", () => {
|
|
826
|
+
expect(buildReactDevArgs("npm", "3100", "127.0.0.1")).toEqual([
|
|
827
|
+
"vite",
|
|
828
|
+
"--port",
|
|
829
|
+
"3100",
|
|
830
|
+
"--host",
|
|
831
|
+
"127.0.0.1",
|
|
832
|
+
"--strictPort",
|
|
833
|
+
]);
|
|
834
|
+
const args = buildSwaStartArgs(undefined, "3100", "4285", "7079");
|
|
835
|
+
expect(args).toContain("http://localhost:7079");
|
|
836
|
+
expect(args).not.toContain("http://localhost:7071");
|
|
837
|
+
});
|
|
838
|
+
});
|