example-remote-server/src/context.test.ts
Jerome 93c939614e
Fix tests to work with ES modules and TypeScript
- Updated Jest configuration to use ts-jest ESM preset
- Added NODE_OPTIONS flag for experimental VM modules support
- Fixed import statements in all test files to import Jest globals
- Rewrote provider.test.ts to work with ESM (using actual implementations with mock Redis)
- Fixed test expectations to match actual implementation
- Fixed encryption key generation in tests (using proper 64-char hex tokens)
- Standardized token_type to "Bearer" across codebase
- Removed unused imports to fix lint errors
- Excluded scratch folder from test runs

All tests now pass successfully with proper ES module support.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

Relocked
2025-06-04 13:16:42 +01:00

114 lines
No EOL
4.3 KiB
TypeScript

import { describe, test, expect } from '@jest/globals';
import { getFakeUpstreamInstallation, getMcpAccessToken, withContext } from "./context.js";
import { generateToken } from "./services/auth.js";
describe("context", () => {
const mockContext = {
mcpAccessToken: generateToken(),
fakeUpstreamInstallation: {
fakeAccessTokenForDemonstration: "fake-upstream-access-token",
fakeRefreshTokenForDemonstration: "fake-upstream-refresh-token",
},
};
test("getFakeUpstreamInstallation throws when called outside context", () => {
expect(() => getFakeUpstreamInstallation()).toThrow(
"No request context found - are you calling this from within a request handler?"
);
});
test("getMcpAccessToken throws when called outside context", () => {
expect(() => getMcpAccessToken()).toThrow(
"No request context found - are you calling this from within a request handler?"
);
});
test("context functions return data within context", async () => {
await withContext(mockContext, () => {
const upstreamInstall = getFakeUpstreamInstallation();
expect(upstreamInstall).toEqual(mockContext.fakeUpstreamInstallation);
const mcpToken = getMcpAccessToken();
expect(mcpToken).toEqual(mockContext.mcpAccessToken);
});
});
test("nested contexts maintain isolation", async () => {
const context1 = {
mcpAccessToken: generateToken(),
fakeUpstreamInstallation: {
fakeAccessTokenForDemonstration: "fake-upstream-access-token",
fakeRefreshTokenForDemonstration: "fake-upstream-refresh-token",
},
};
const context2 = {
mcpAccessToken: generateToken(),
fakeUpstreamInstallation: {
fakeAccessTokenForDemonstration: "fake-upstream-access-token",
fakeRefreshTokenForDemonstration: "fake-upstream-refresh-token",
},
};
await withContext(context1, async () => {
expect(getFakeUpstreamInstallation()).toEqual(context1.fakeUpstreamInstallation);
expect(getMcpAccessToken()).toEqual(context1.mcpAccessToken);
await withContext(context2, () => {
expect(getFakeUpstreamInstallation()).toEqual(context2.fakeUpstreamInstallation);
expect(getMcpAccessToken()).toEqual(context2.mcpAccessToken);
});
// Outer context should be preserved
expect(getFakeUpstreamInstallation()).toEqual(context1.fakeUpstreamInstallation);
expect(getMcpAccessToken()).toEqual(context1.mcpAccessToken);
});
});
test("concurrent contexts maintain isolation", async () => {
const context1 = {
mcpAccessToken: generateToken(),
fakeUpstreamInstallation: {
fakeAccessTokenForDemonstration: "fake-upstream-access-token",
fakeRefreshTokenForDemonstration: "fake-upstream-refresh-token",
},
};
const context2 = {
mcpAccessToken: generateToken(),
fakeUpstreamInstallation: {
fakeAccessTokenForDemonstration: "fake-upstream-access-token",
fakeRefreshTokenForDemonstration: "fake-upstream-refresh-token",
},
};
// Run two contexts concurrently
await Promise.all([
withContext(context1, async () => {
// Simulate async work
await new Promise(resolve => setTimeout(resolve, 10));
expect(getFakeUpstreamInstallation()).toEqual(context1.fakeUpstreamInstallation);
expect(getMcpAccessToken()).toEqual(context1.mcpAccessToken);
}),
withContext(context2, async () => {
// Simulate async work
await new Promise(resolve => setTimeout(resolve, 5));
expect(getFakeUpstreamInstallation()).toEqual(context2.fakeUpstreamInstallation);
expect(getMcpAccessToken()).toEqual(context2.mcpAccessToken);
}),
]);
});
test("context is preserved across async operations", async () => {
await withContext(mockContext, async () => {
const initialUpstreamInstall = getFakeUpstreamInstallation();
const initialMcpToken = getMcpAccessToken();
// Simulate async work
await new Promise(resolve => setTimeout(resolve, 10));
const afterUpstreamInstall = getFakeUpstreamInstallation();
const afterMcpToken = getMcpAccessToken();
expect(afterUpstreamInstall).toEqual(initialUpstreamInstall);
expect(afterMcpToken).toEqual(initialMcpToken);
});
});
});