name: CI

on:
  push:
    branches:
      - main
  pull_request:
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    name: Rust
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust stable
        run: rustup toolchain install stable --profile minimal --component clippy --component rustfmt

      - name: Check formatting
        run: cargo +stable fmt --all --check

      - name: Clippy
        run: cargo +stable clippy --locked --all-targets --all-features -- -D warnings

      - name: Build
        run: cargo +stable build --locked --all-targets --all-features

      # lazily-spec is the canonical cross-language wire-protocol source. Clone
      # it as a sibling BEFORE any feature-test step so every test target that
      # reads ../lazily-spec (conformance, collections_conformance,
      # seqcrdt_conformance, schema_compliance) has the canonical fixtures — CI
      # then validates lazily-rs against canonical fixtures and fails on drift
      # (#lzspecconf).
      - name: Fetch canonical lazily-spec conformance fixtures (#lzspecconf)
        run: git clone --depth 1 https://github.com/lazily-hub/lazily-spec.git ../lazily-spec

      - name: Test default features
        run: cargo +stable test --locked

      - name: Test thread-safe feature (#lzthreadsafe)
        run: cargo +stable test --locked --features thread-safe

      - name: Test Tokio feature
        run: cargo +stable test --locked --features "tokio thread-safe"

      - name: Test distributed feature
        run: cargo +stable test --locked --features "distributed serde"

      - name: Test distributed CRDT plane runtime integration (#lzcrdtplane5b)
        run: cargo +stable test --locked --features "distributed webrtc"

      - name: Test signaling client feature
        run: cargo +stable test --locked --features signaling-client

      - name: Test async feature
        run: cargo +stable test --locked --features async

      - name: Test WebRTC DataChannel transport + str0m backend (#webrtcbackend)
        run: cargo +stable test --locked --features webrtc-str0m

      - name: Test WebSocket DataChannel backend (#akp3)
        run: cargo +stable test --locked --features websocket

      - name: Test async resolve-loop windows (#k03k)
        run: cargo +stable test --locked --features "async instrumentation" --test async_resolve_loop

      - name: Test IPC conformance against canonical spec fixtures (#lzspecconf)
        run: |
          test -d ../lazily-spec/conformance || { echo "::error::../lazily-spec/conformance missing — conformance.rs would silently fall back to local fixtures"; exit 1; }
          cargo +stable test --locked --features ipc --test conformance

      - name: Test JSON Schema compliance against canonical spec schemas (#lzspecschema)
        run: |
          test -d ../lazily-spec/schemas || { echo "::error::../lazily-spec/schemas missing"; exit 1; }
          cargo +stable test --locked --features ipc --test schema_compliance

      - name: Test keyed cell collections conformance against canonical spec fixtures (#lzcellfamily)
        run: |
          test -d ../lazily-spec/conformance/collections || { echo "::error::../lazily-spec/conformance/collections missing — collections_conformance.rs would silently fall back to local fixtures"; exit 1; }
          cargo +stable test --locked --test collections_conformance

      - name: Test SeqCrdt conformance against canonical spec fixtures (#lzseqcrdt)
        run: |
          test -f ../lazily-spec/conformance/collections/seqcrdt_convergence.json || { echo "::error::seqcrdt_convergence.json missing — seqcrdt_conformance.rs cannot run"; exit 1; }
          cargo +stable test --locked --features distributed --test seqcrdt_conformance

      - name: Test loom model
        run: cargo +stable test --locked --features loom --test thread_safe_loom

  signaling:
    name: Signaling Worker (#yxjw)
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: signaling
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Node
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Install
        run: npm ci

      - name: Typecheck + test
        run: npm run check

  lean:
    name: Lean formal models (#lzleanmodel, #lzformal)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout canonical lazily-spec
        uses: actions/checkout@v4
        with:
          repository: lazily-hub/lazily-spec
          path: lazily-spec

      - name: Build lazily-spec Lean formal model (#lzleanmodel)
        uses: leanprover/lean-action@v1
        with:
          lake-package-directory: lazily-spec/formal/lean
          auto-config: false
          build: true
          test: false
          lint: false

      - name: Checkout canonical lazily-formal
        uses: actions/checkout@v4
        with:
          repository: lazily-hub/lazily-formal
          path: lazily-formal

      - name: Build lazily-formal Lean model (#lzformal)
        uses: leanprover/lean-action@v1
        with:
          lake-package-directory: lazily-formal
          auto-config: false
          build: true
          test: false
          lint: false
