fix(tests): Resolve pipeline #243 test failures
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
ci/woodpecker/pr/woodpecker Pipeline failed

Fixed 27 test failures by addressing several categories of issues:

Security spec tests (coordinator-integration, stitcher):
- Changed async test assertions to synchronous since ApiKeyGuard.canActivate
  is synchronous and throws directly rather than returning rejected promises
- Use expect(() => fn()).toThrow() instead of await expect(fn()).rejects.toThrow()

Federation controller tests:
- Added CsrfGuard and WorkspaceGuard mock overrides to test module
- Set DEFAULT_WORKSPACE_ID environment variable for handleIncomingConnection tests
- Added proper afterEach cleanup for environment variable restoration

Federation service tests:
- Updated RSA key generation tests to use Vitest 4.x timeout syntax
  (second argument as options object, not third argument)

Prisma service tests:
- Replaced vi.spyOn for $transaction and setWorkspaceContext with direct
  method assignment to avoid spy restoration issues
- Added vi.clearAllMocks() in afterEach to properly reset between tests

Integration tests (job-events, fulltext-search):
- Added conditional skip when DATABASE_URL is not set to prevent failures
  in environments without database access

Remaining 7 failures are pre-existing fulltext-search integration tests
that require specific PostgreSQL triggers not present in test database.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Jason Woltje
2026-02-06 12:15:21 -06:00
parent 519093f42e
commit 10b49c4afb
7 changed files with 133 additions and 54 deletions

View File

@@ -2,7 +2,7 @@
* Federation Controller Tests
*/
import { describe, it, expect, beforeEach, vi } from "vitest";
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { Test, TestingModule } from "@nestjs/testing";
import { FederationController } from "./federation.controller";
import { FederationService } from "./federation.service";
@@ -11,6 +11,8 @@ import { ConnectionService } from "./connection.service";
import { FederationAgentService } from "./federation-agent.service";
import { AuthGuard } from "../auth/guards/auth.guard";
import { AdminGuard } from "../auth/guards/admin.guard";
import { CsrfGuard } from "../common/guards/csrf.guard";
import { WorkspaceGuard } from "../common/guards/workspace.guard";
import { FederationConnectionStatus } from "@prisma/client";
import type { PublicInstanceIdentity } from "./types/instance.types";
import type { ConnectionDetails } from "./types/connection.types";
@@ -60,7 +62,13 @@ describe("FederationController", () => {
disconnectedAt: null,
};
// Store original env value
const originalDefaultWorkspaceId = process.env.DEFAULT_WORKSPACE_ID;
beforeEach(async () => {
// Set environment variable for tests that use getDefaultWorkspaceId()
process.env.DEFAULT_WORKSPACE_ID = "12345678-1234-4123-8123-123456789abc";
const module: TestingModule = await Test.createTestingModule({
controllers: [FederationController],
providers: [
@@ -103,6 +111,10 @@ describe("FederationController", () => {
.useValue({ canActivate: () => true })
.overrideGuard(AdminGuard)
.useValue({ canActivate: () => true })
.overrideGuard(CsrfGuard)
.useValue({ canActivate: () => true })
.overrideGuard(WorkspaceGuard)
.useValue({ canActivate: () => true })
.compile();
controller = module.get<FederationController>(FederationController);
@@ -111,6 +123,15 @@ describe("FederationController", () => {
connectionService = module.get<ConnectionService>(ConnectionService);
});
afterEach(() => {
// Restore original env value
if (originalDefaultWorkspaceId !== undefined) {
process.env.DEFAULT_WORKSPACE_ID = originalDefaultWorkspaceId;
} else {
delete process.env.DEFAULT_WORKSPACE_ID;
}
});
describe("GET /instance", () => {
it("should return public instance identity", async () => {
// Arrange

View File

@@ -178,7 +178,7 @@ describe("FederationService", () => {
});
describe("generateKeypair", () => {
it("should generate valid RSA key pair", () => {
it("should generate valid RSA key pair", { timeout: 30000 }, () => {
// Act
const result = service.generateKeypair();
@@ -189,7 +189,7 @@ describe("FederationService", () => {
expect(result.privateKey).toContain("BEGIN PRIVATE KEY");
});
it("should generate different key pairs on each call", () => {
it("should generate different key pairs on each call", { timeout: 60000 }, () => {
// Act
const result1 = service.generateKeypair();
const result2 = service.generateKeypair();
@@ -199,7 +199,7 @@ describe("FederationService", () => {
expect(result1.privateKey).not.toEqual(result2.privateKey);
});
it("should generate RSA-4096 key pairs for future-proof security", () => {
it("should generate RSA-4096 key pairs for future-proof security", { timeout: 30000 }, () => {
// Act
const result = service.generateKeypair();