Refactor template tests to verify behavior, not implementation details

Problem

The template test suite (tests/zk-extras/template_spec.lua) is testing implementation details rather than intended behavior. This makes the tests brittle and doesn't verify actual user requirements.

Specific Issues

1. ISO datetime format test (lines 440-445)

Current: Tests exact timezone calculation matching implementation

assert.is_true(ctx.iso_datetime:match("%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%d[+-]%d%d:%d%d") ~= nil)

Should test: ISO 8601 compliance and correct timezone representation

  • Verify format is valid ISO 8601
  • Verify timezone offset is correct for system
  • Don't test the exact calculation method

2. Input prompt caching (lines 513-548)

Current: Tests that {{input:Prompt}} values are cached and reused

assert.equals(1, input_count)  -- Should only be asked once

Issue: Is caching a documented requirement or just implementation detail?

  • No documentation mentions this behavior
  • Test assumes implementation detail is a feature
  • Should verify: "user is prompted for input variables" not "caching works exactly this way"

3. Variable expansion order tests

Current: Tests expand in specific order (ENV → reg → input → plain)

Should test:

  • All variable types are expanded correctly
  • Variables don't interfere with each other
  • Edge cases (missing vars, malformed syntax)

Recommended Changes

  1. Define behavior specifications first:

    • What MUST the template system do?
    • What are the documented features?
    • What guarantees do we make to users?
  2. Rewrite tests as behavior verification:

-- Instead of:
it("handles {{input:Prompt}} variables", function()
  -- test exact implementation

-- Write:
it("prompts user for input variables and uses provided values", function()
  -- test the outcome, not the mechanism
  1. Add missing behavioral tests:
    • Malformed template syntax handling
    • Circular variable references
    • Very large template files (performance)
    • Unicode in variable names/values
    • Concurrent template expansions

Success Criteria

  • All template tests verify user-facing behavior
  • Tests pass with alternative implementations
  • Edge cases are covered
  • Error messages are verified to be helpful
  • Performance characteristics are tested

References

  • tests/zk-extras/template_spec.lua lines 440-445, 513-548
  • lua/zk-extras/template.lua