Feature: YAML Template Engine coverage

  Background:
    Given the YAML template engine is initialized for coverage

  Scenario: Postprocess splits combined mapping values
    Given I have YAML with specific line splitting pattern:
      """
      config:
        combined: value1 key2: value2
        test: normal value
      """
    When I process postprocessing line splitting
    Then specific line splitting should occur
    And the lines should be restructured

  Scenario: Rejects non-dict plain YAML without templates
    Given I have plain YAML without templates:
      """
      - item1
      - item2
      """
    When I load YAML content through the template engine
    Then a ValueError should be raised indicating dict requirement

  Scenario: Deferred loading preserves template placeholders
    Given the YAML template engine is initialized for coverage
    Given I have a YAML string with inline template placeholders:
      """
      agent:
        name: "{{ name }}"
        model: "{{ model }}"
      """
    When I load YAML content without context for deferred rendering
    Then the deferred template should keep placeholders
    And the deferred template should be a mapping with key "agent"

  Scenario: Deferred loading surfaces YAML errors for invalid templates
    Given I have invalid templated YAML for deferred rendering:
      """
      agent:
        name: {{ name
      """
    When I load YAML content without context for deferred rendering
    Then a YAML error should be raised for deferred loading

  Scenario: Preprocess inserts indentation hints for for-loops
    Given I have a YAML string with for loops requiring preprocessing:
      """
      agents:
        {% for item in items %}
        - name: {{ item }}
        {% endfor %}
      """
    When I preprocess the YAML content for rendering
    Then indentation hints should be present after the loop line

  Scenario: Rendering with colon-heavy values triggers fallback fix
    Given I have a templated YAML that renders malformed mapping:
      """
      config: {{ value_with_colons }}
      """
    And I have a template context with colon rich value
    When I render the YAML with context
    Then a YAML parsing error should be raised after attempting fixes

  Scenario: Rendering simple template succeeds with merged context
    Given I have a YAML string with inline Jinja2 templates:
      """
      items:
        {% for item in items %}
        - {{ item }}
        {% endfor %}
      context_value: {{ context.existing }}
      """
    And I have a template context with list items
    When I render the YAML with context
    Then the rendered YAML should be parsed into a mapping
    And the rendered mapping should include loop results and merged context

  Scenario: Selectattr filter fills defaults
    Given I have objects with missing attributes
    When I apply the selectattr filter for attribute "score" with default 0
    Then the selected attributes should include defaults

  Scenario: Sum and helper filters format values
    Given I have numeric values for the sum filter
    When I apply the sum filter and helper formatters
    Then the helper filters should format output

  Scenario: Analyze YAML structure skips comments and finds templates
    Given I have YAML with mixed content for structure analysis:
      """
      # comment
      root:
        value: static
        {{ inline_var }}
        {% for x in items %}
        - name: {{ x }}
        {% endfor %}
      """
    When I analyze the YAML structure
    Then template blocks and inline templates should be located

  Scenario: Load file wrapper processes templates from disk
    Given I have a temporary YAML file with template content:
      """
      agent:
        name: "{{ name }}"
        role: static
      """
    When I load the YAML file with context data
    Then the file load result should merge templated values

  Scenario: Rendering list template raises mapping ValueError
    Given I have a templated YAML that renders to a list root:
      """
      {% for item in items %}
      - {{ item }}
      {% endfor %}
      """
    And I have a template context with list items
    When I render the YAML expecting a non-mapping error
    Then a ValueError should indicate rendered YAML must be a mapping

  Scenario: Malformed rendered YAML triggers fallback fix path
    Given I have templated YAML that renders invalid mappings:
      """
      config: {{ messy_value }}
      """
    And I have a messy value context for rendering
    When I render the malformed YAML content
    Then the parser should attempt fixes then raise YAML error

  Scenario: Deferred templated list raises mapping ValueError
    Given I have a deferred YAML template that becomes a list:
      """
      - "{{ value }}"
      """
    When I load YAML content without context for deferred rendering
    Then a ValueError should be raised for non-mapping deferred templates

  Scenario: Parse retry succeeds after YAML error
    Given I have YAML with a placeholder causing parse retry:
      """
      result: {{ value }}
      """
    And I have a template context for retry parsing
    When I render YAML with a transient YAML error on first parse
    Then the rendering retry should yield a mapping result

  Scenario: Parse retry raises ValueError when repaired YAML is not a mapping
    Given I have YAML with a placeholder causing parse retry:
      """
      result: {{ value }}
      """
    And I have a template context for retry parsing
    When rendering retry returns non-mapping after YAML error
    Then a ValueError should be raised for non-mapping retry

  Scenario: Fix common YAML issues splits multiple colons
    Given I have inline YAML with multiple colons on one line:
      """
      config: key1: val1 key2: val2
      """
    When I apply the common YAML fixes
    Then the multi-colon line should be split into nested entries

  Scenario: Synthetic postprocess respects single-colon guard
    Given I have synthetic YAML content for postprocess:
      """
      config: alpha beta: gamma
      """
    When I postprocess using a single-colon synthetic line
    Then the synthetic postprocess should keep the line intact

  Scenario: Load file without template markers
    Given I have a YAML file without templates:
      """
      config:
        name: PlainConfig
        version: 1.0
      """
    When I load the YAML file without context
    Then the YAML file should parse as a mapping
    And the parsed YAML should include config name "PlainConfig"

  Scenario: Load string without template markers
    Given I have a YAML string without templates:
      """
      simple:
        key: value
        list:
          - item1
          - item2
      """
    When I load the string directly
    Then the YAML string should parse as a mapping
    And the parsed YAML string should include key value "value"

  Scenario: Render context merges nested context
    Given I have a template using nested context and overrides:
      """
      result:
        nested: "{{ context.shared }}"
        explicit: "{{ value }}"
        context_copy: "{{ context.explicit }}"
      """
    And I have a template context with nested overrides
    When I render the YAML with merged context
    Then the render context should include nested and top-level values

  Scenario: Utility functions are available during rendering
    Given I have template using utility functions:
      """
      utilities:
        range_values: {{ range(3) | list }}
        abs_value: {{ abs(-4) }}
        round_value: {{ round(3.1415, 2) }}
      """
    When I process with complete render context utilities
    Then utility functions should be evaluated

  Scenario: Sum filter surfaces errors for invalid sequences
    Given I have non-summable values for the sum filter
    When I apply the sum filter expecting an error
    Then a ValueError should be raised for the sum filter

  # -- New scenarios for additional coverage --

  Scenario: Postprocess removes blank lines between top-level sections
    Given I have rendered YAML with blank lines between sections:
      """
      section_one:
        key: value

      section_two:
        key: value2
      """
    When I postprocess the rendered YAML for blank line cleanup
    Then blank lines before top-level keys should be removed

  Scenario: Postprocess keeps blank lines before indented content
    Given I have rendered YAML with blank lines before indented content:
      """
      section:
        key1: value1

        key2: value2
      """
    When I postprocess the rendered YAML for blank line cleanup
    Then blank lines before indented content should be kept

  Scenario: Create render context without nested context key
    Given I have a template context without nested context key
    When I create the render context
    Then the render context should have an empty context mapping

  Scenario: Create render context with non-dict nested context
    Given I have a template context with non-dict nested context
    When I create the render context
    Then the render context should override context with non-dict value

  Scenario: Analyze YAML structure handles empty and blank lines
    Given I have YAML with empty and blank lines for structure analysis:
      """

      root:
        nested: value

        deep:
          key: data
      """
    When I analyze the YAML structure
    Then hierarchy should contain root and nested keys
    And blank lines should be skipped in analysis

  Scenario: Analyze YAML structure pops path for dedented keys
    Given I have YAML with dedented keys for structure analysis:
      """
      parent:
        child: value
      sibling: other
      """
    When I analyze the YAML structure
    Then hierarchy paths should reflect dedentation correctly

  Scenario: Fix common YAML issues handles colon-ending tokens
    Given I have YAML with colon-ending tokens:
      """
      config: key1: val1 key2: val2
      """
    When I apply the common YAML fixes
    Then colon-ending tokens should be split onto separate lines

  Scenario: Fix common YAML issues preserves lines without multiple colons
    Given I have YAML without multiple colons per line:
      """
      simple: value
      another: data
      """
    When I apply the common YAML fixes
    Then lines without multiple colons should remain unchanged

  Scenario: Fix common YAML issues handles trailing tokens after colons
    Given I have YAML with mixed colon tokens for fixing:
      """
      config: key1: val1 remainder
      """
    When I apply the common YAML fixes
    Then the trailing tokens should be handled correctly

  Scenario: Postprocess handles value with colon in subsequent words
    Given I have rendered YAML with colon in value words:
      """
      endpoint: http value:8080 extra
      """
    When I postprocess the rendered YAML with colon values
    Then the colon value should trigger line splitting

  Scenario: Preprocess does not add hints for non-loop Jinja blocks
    Given I have YAML with non-loop Jinja blocks:
      """
      config:
        {% if condition %}
        enabled: true
        {% endif %}
      """
    When I preprocess the YAML content for rendering
    Then no indentation hints should be added for non-loop blocks

  Scenario: Simple template extraction raises on invalid YAML
    Given I have invalid YAML for simple template extraction:
      """
      invalid: [unterminated
      """
    When I extract templates from invalid YAML
    Then a YAML error should be raised during extraction

  Scenario: Load string with templates and context calls render path
    Given I have a YAML string with inline Jinja2 templates:
      """
      greeting: "{{ message }}"
      """
    And I have a simple template context with message
    When I render the YAML with context
    Then the rendered YAML should be parsed into a mapping
    And the rendered result should contain the message value

  Scenario: Selectattr filter returns attribute values for all matching objects
    Given I have objects that all have the target attribute
    When I apply the selectattr filter for attribute "name" with default "none"
    Then all attribute values should be returned without defaults

  Scenario: Selectattr filter uses default for all missing attributes
    Given I have objects that all lack the target attribute
    When I apply the selectattr filter for attribute "missing" with default -1
    Then all values should be the default

  Scenario: Indent filter applies custom spacing to multiline text
    Given I have a multiline string for indentation
    When I apply the indent filter with 4 spaces
    Then each line should be indented by 4 spaces

  Scenario: YAML filter serializes complex nested structures
    Given I have a complex nested structure for YAML serialization
    When I apply the YAML filter to serialize it
    Then the serialized output should be valid YAML text

  Scenario: Load string returns template content as context for deferred rendering
    Given I have YAML with block-level templates for deferred loading:
      """
      config:
        name: "{{ agent_name }}"
        version: 1
      """
    When I load YAML content without context for deferred rendering
    Then the deferred result should preserve quoted template strings
