nyxcore-systems
6 min read

From Orphaned Reports to Robust Exports: A Deep Dive into Our Latest Dev Sprint

Join us as we recount a recent development sprint, tackling critical E2E testing for compliance exports, refining report rendering, and ensuring data integrity across our platform.

e2e-testingplaywrightnextjstrpccompliancereportstypescriptnextauthfrontendbackenddatabase

Every development cycle brings its unique set of challenges and triumphs. Our recent sprint was a prime example, a focused effort to bolster our platform's compliance features, enhance reporting, and solidify our testing infrastructure. The mission? To deliver robust End-to-End (E2E) tests for our compliance export functionality, fix long-standing report rendering quirks, and ensure every report is correctly linked to its originating project.

After a focused session, we're thrilled to report that all these goals have been met, culminating in three significant code commits and a crucial database backfill. Let's unpack the journey.

Fortifying Compliance: Our E2E Testing Odyssey

When it comes to compliance, "it works on my machine" simply isn't good enough. We needed ironclad assurance that our compliance export feature, a critical component for our users, behaved exactly as expected across various scenarios. This called for a comprehensive E2E testing suite, powered by Playwright.

Crafting a Reusable Authentication Fixture

One of the initial hurdles in E2E testing authenticated routes is, well, authentication itself. Manually logging in for every test is slow and brittle. Our solution involved forging a NextAuth v5 JWT cookie directly. We created a dedicated helper: tests/e2e/helpers/auth.ts. This fixture leverages Node.js's process.loadEnvFile (with a robust fallback) to load environment variables, enabling us to programmatically generate the necessary JWT for authenticated test sessions. This means our tests can run against a fully authenticated state without ever touching a login form.

Comprehensive Coverage for Compliance Exports

With authentication streamlined, we built tests/e2e/compliance-export.spec.ts. This single file now houses 9 detailed E2E tests, meticulously covering every aspect of the compliance export panel:

  • Panel Rendering: Ensuring the UI appears correctly.
  • Expand/Collapse Functionality: Verifying interactive elements.
  • Export Download: Crucially, confirming the export file is generated and downloadable.
  • PR Checkbox Visibility & Toggle: Testing the interaction for selecting pull requests.
  • PR Link Display: Ensuring links to relevant pull requests are present and correct.
  • Error & Loading States: Validating graceful handling of various operational states.

The result? All 40 of our E2E tests (18 new, 22 existing) are now passing flawlessly across both Chromium and mobile viewports, providing a high degree of confidence in our application's stability and correctness.

Refining the Reporting Experience

Beyond compliance exports, we also tackled some long-standing issues within our reporting system to enhance both visual fidelity and data integrity.

Taming Raw HTML: The rehype-raw Solution

Our custom report headers, generated server-side using buildReportHeader(), were appearing as raw HTML tags within the in-app preview instead of beautifully rendered content. This was a classic case of a Markdown renderer being too safe, escaping HTML instead of rendering it. The fix was elegant: we integrated rehype-raw into our MarkdownRenderer component. This plugin now correctly processes the server-generated HTML, allowing our headers to render as intended, providing a much cleaner and professional look.

Connecting the Dots: Project Linking for Reports

A more subtle but critical issue was discovered: reports generated from our workflows, auto-fix, and refactor pages were being saved to the database but without a crucial link to their projectId or projectName. This meant these reports were "orphaned," making them harder to track and manage within their respective projects. We quickly patched the ReportGeneratorModal on these three pages to correctly capture and persist the projectId and projectName during report creation.

Data Cleanup: Backfilling Orphaned Reports

Identifying the root cause was only half the battle. We had 12 existing reports in the database that suffered from this "orphan" status. Rather than leaving them unlinked, we performed a targeted database backfill. By looking up the projectId from their source workflow or autofix run, we updated each of these records, restoring their proper association and ensuring data integrity moving forward.

Navigating the Technical Maze: Lessons Learned

No sprint is without its challenges. Overcoming these hurdles not only delivers working features but also enriches our collective knowledge. Here are a few key takeaways from this session:

The tRPC Superjson Saga

Challenge: When building our E2E tests, we initially tried to mock tRPC responses using plain JSON via Playwright's route interception. While the page rendered, components relying on tRPC data failed to hydrate, throwing 500 errors from the real server. Discovery: The root cause was tRPC's use of the superjson transformer. Its wire format isn't plain JSON; it expects responses to be wrapped in a specific structure: { result: { data: { json: <your_data> } } }, not simply { result: { data: <your_data> } }. Solution: We created a superjsonResult() helper function to automatically wrap our mock data in the correct superjson format. This was a crucial insight: all tRPC mock responses in our E2E tests must now adhere to this wire format.

typescript
// Example of the superjsonResult helper (simplified)
const superjsonResult = (data: any) => ({
  result: {
    data: {
      json: data,
    },
  },
});

// Used like:
// page.route("**/api/trpc/someProcedure", route => {
//   route.fulfill({
//     status: 200,
//     contentType: "application/json",
//     body: JSON.stringify(superjsonResult({ message: "Mocked data!" })),
//   });
// });

Environment Variables in Playwright Workers

Challenge: Running E2E tests locally led to an error: "AUTH_SECRET env var must be set". Playwright's worker processes, by default, don't inherit environment variables from the main process or automatically load .env files. Solution: We explicitly added process.loadEnvFile() (a Node.js 23+ feature) within our authentication helper. This ensures that each Playwright worker correctly loads the .env file, making necessary environment variables available for secure JWT generation. A try/catch block was included for graceful fallback on older Node.js versions or different environments.

The Elusive Batch Procedure

Challenge: During tRPC route interception with page.route("**/api/trpc/**"), we noticed some components still making real server requests. Discovery: tRPC often batches multiple queries into a single request. We had mocked several procedures, but missed one: dashboard.activeProcesses. This procedure was part of a batch request, causing the entire batch to hit the real server when our mock map was incomplete. Solution: A quick addition of dashboard.activeProcesses to our tRPC procedure mock map resolved the issue, ensuring all relevant tRPC calls were correctly intercepted.

Looking Ahead

While this sprint delivered significant improvements, development is an ongoing journey. Our immediate next steps include:

  • Ensuring the "Code Analysis" page also correctly links projectId to its ReportGeneratorModal.
  • Updating our official documentation (docs/06-workflow-intelligence.md) to reflect the new compliance report export features.
  • Investigating the possibility of integrating our E2E auth fixture as a proper Playwright storageState setup project for even broader reuse.
  • Addressing the missing CSS for nyx-header classes used by buildReportHeader(), which currently renders unstyled HTML divs within the in-app preview (though it works perfectly in PDF exports).

This sprint was a testament to the power of focused effort, meticulous testing, and a commitment to resolving technical debt. We're excited about the enhanced stability, improved user experience, and robust compliance capabilities now live in our platform!