name: 🐍👨‍🔬 [Core] Unit Tests

on:
  pull_request:
    branches:
      - main
  pull_request_target:
    branches:
      - main
    types: [labeled]

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
  unit-tests:
    # Run on pull_request OR on pull_request_target only when labeled "safe to test"
    if: github.event_name == 'pull_request' || (github.event_name == 'pull_request_target' && contains(github.event.pull_request.labels.*.name, 'safe to test'))
    uses: ./.github/workflows/reusable-test-workflow.yml
    with:
      test-type: 'unit'
      use-redis: true
      is-external-pr: ${{ github.event_name == 'pull_request_target' && !contains(github.event.pull_request.labels.*.name, 'safe to test') }}
      changed-files-pattern: |
        **
        .github/workflows/reusable-test-workflow.yml
        .github/workflows/core-unit-test.yml
      install-args: '--extra postgres --extra external-tools --extra dev --extra cloud-tool-sandbox'
      timeout-minutes: 15
      ref: ${{ github.event.pull_request.head.sha || github.sha }}
      matrix-strategy: |
        {
          "fail-fast": false,
          "matrix": {
            "include": [
              {"test_suite": "test_client.py"},
              {"test_suite": "test_sdk_client.py"},
              {"test_suite": "managers/"},
              {"test_suite": "test_tool_schema_parsing.py"},
              {"test_suite": "test_tool_rule_solver.py"},
              {"test_suite": "test_memory.py"},
              {"test_suite": "test_utils.py"},
              {"test_suite": "test_stream_buffer_readers.py"},
              {"test_suite": "test_agent_serialization_v2.py"},
              {"test_suite": "test_optimistic_json_parser.py"},
              {"test_suite": "test_llm_clients.py"},
              {"test_suite": "test_letta_agent_batch.py"},
              {"test_suite": "test_providers.py"},
              {"test_suite": "test_server_providers.py"},
              {"test_suite": "test_sources.py"},
              {"test_suite": "sdk/"},
              {"test_suite": "mcp_tests/"},
              {"test_suite": "test_timezone_formatting.py"},
              {"test_suite": "test_plugins.py"},
              {"test_suite": "test_embeddings.py"},
              {"test_suite": "test_crypto_utils.py"},
              {"test_suite": "test_mcp_encryption.py"},
              {"test_suite": "test_secret.py"}
            ]
          }
        }
    secrets: inherit
