services:
  deps:
    image: node:20-alpine
    working_dir: /app
    environment:
      - NODE_ENV=development
      - NPM_CONFIG_FUND=false
      - NPM_CONFIG_AUDIT=false
      - NPM_CONFIG_UPDATE_NOTIFIER=false
    volumes:
      - ./:/app
      - test_node_modules:/app/node_modules
      - test_npm_cache:/root/.npm
    command:
      [
        "sh",
        "-lc",
        "set -e; LOCK_HASH=\"$$(sha1sum package-lock.json | awk '{print $$1}')\"; MARKER=\"node_modules/.gridpilot_lock_hash_test\"; if [ -f \"$$MARKER\" ] && [ \"$$(cat \"$$MARKER\")\" = \"$$LOCK_HASH\" ]; then echo \"[deps] node_modules up-to-date\"; else echo \"[deps] installing workspace deps\"; rm -rf apps/api/node_modules apps/website/node_modules apps/companion/node_modules; npm install --no-package-lock --include-workspace-root --no-audit --fund=false --prefer-offline; echo \"$$LOCK_HASH\" > \"$$MARKER\"; fi",
      ]
    networks:
      - gridpilot-test-network
    restart: "no"

  api:
    image: node:20-alpine
    working_dir: /app
    environment:
      - NODE_ENV=test
      - PORT=3000
    ports:
      - "3101:3000"
    volumes:
      - ./:/app
    command: ["sh", "-lc", "node testing/mock-api-server.cjs"]
    networks:
      - gridpilot-test-network
    restart: unless-stopped
    healthcheck:
      test:
        [
          "CMD",
          "node",
          "-e",
          "fetch('http://localhost:3000/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))",
        ]
      interval: 2s
      timeout: 2s
      retries: 30

  website:
    image: gridpilot-website-test
    build:
      context: .
      dockerfile: apps/website/Dockerfile.dev
    environment:
      - NEXT_TELEMETRY_DISABLED=1
      - NODE_ENV=development
      - DOCKER=true
      - NEXT_PUBLIC_GRIDPILOT_MODE=alpha
      - API_BASE_URL=http://api:3000
      - NEXT_PUBLIC_API_BASE_URL=http://localhost:3101
    ports:
      - "3100:3000"
    volumes:
      - ./:/app
      - test_node_modules:/app/node_modules
      - test_npm_cache:/root/.npm
    command: ["sh", "-lc", "npm run dev --workspace=@gridpilot/website"]
    depends_on:
      api:
        condition: service_healthy
    networks:
      - gridpilot-test-network
    restart: unless-stopped
    healthcheck:
      test:
        ["CMD", "node", "-e", "fetch('http://localhost:3000').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
      interval: 5s
      timeout: 5s
      retries: 20

networks:
  gridpilot-test-network:
    driver: bridge

volumes:
  test_node_modules:
  test_npm_cache: