CDX-301c · Module 2

Style Enforcement & Test Generation

3 min read

Style enforcement via Codex fills the gap between what linters catch and what code reviewers comment on. Linters enforce syntax rules (semicolons, quotes, spacing). Codex enforces semantic style rules (consistent error handling patterns, naming conventions for domain concepts, import organization by category). These semantic rules are too complex for AST-based linters but straightforward for a language model that understands context.

Test generation is the highest-ROI CI automation pattern. When a PR adds new code without tests, Codex can generate test stubs that cover the happy path, error cases, and edge cases. The generated tests are not production-ready — they need human review — but they eliminate the blank-page problem and enforce the team standard that every PR includes tests. The key is generating test skeletons (describe/it blocks with assertions) rather than complete tests, so the author fills in the domain-specific details.

name: Codex Test Generation
on:
  pull_request:
    types: [opened, synchronize]

jobs:
  suggest-tests:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - run: npm install -g @openai/codex
      - name: Check for untested code
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
        run: |
          # Find new/modified source files without corresponding test files
          CHANGED=$(git diff --name-only origin/${{ github.base_ref }}...HEAD \
            | grep -E '\.(ts|tsx)


 \
            | grep -v '\.test\.' \
            | grep -v '\.spec\.')
          
          for FILE in $CHANGED; do
            TEST_FILE="${FILE%.ts}.test.ts"
            if [ ! -f "$TEST_FILE" ]; then
              echo "Generating test suggestions for: $FILE"
              codex --profile ci --approval full-auto \
                "Generate a test skeleton for $FILE. 
                 Use Vitest. Include describe/it blocks for:
                 - happy path, error cases, edge cases.
                 Output only the test file content." \
                > "suggested-${TEST_FILE##*/}"
            fi
          done
  1. Define style rules beyond linting List semantic style rules your team enforces in review but cannot express in ESLint. Error handling patterns, naming conventions for domain terms, import grouping. These become your Codex style enforcement rules.
  2. Configure test generation Create a CI job that identifies new source files without tests and generates test skeletons. Post suggestions as PR comments, not commits.
  3. Measure adoption Track what percentage of generated test skeletons get adopted (committed with modifications) versus ignored. Improve the generation prompt based on what authors change.