Compare commits
15 Commits
testdrive-
...
4d899d0690
| Author | SHA1 | Date | |
|---|---|---|---|
| 4d899d0690 | |||
| dcb51b7e3a | |||
| d0432dbe0d | |||
| 45e4c7a6e9 | |||
| 01e5c811ab | |||
| 9fe2960842 | |||
| 7be37df3e4 | |||
| 21189f7664 | |||
| ddd8189576 | |||
| 2e6f292e48 | |||
| a1476a98b5 | |||
| 304959b3ee | |||
| 83086b3773 | |||
| 82eef76366 | |||
| 2838135450 |
323
.claude/capabilities/issue-facade.md
Normal file
323
.claude/capabilities/issue-facade.md
Normal file
@@ -0,0 +1,323 @@
|
||||
# Issue Facade - Agent Integration Context
|
||||
|
||||
**🤖 For Coding Agents: Read this to understand how to use issue tracking in this project.**
|
||||
|
||||
## Critical: DO NOT Bypass This Capability
|
||||
|
||||
⚠️ **IMPORTANT:** If you need to work with issues, **you MUST use this capability**. Do NOT:
|
||||
- ❌ Make direct API calls to Gitea/GitHub/GitLab
|
||||
- ❌ Use platform CLIs (gh, glab, etc.)
|
||||
- ❌ Import platform libraries (PyGithub, python-gitlab, etc.)
|
||||
- ❌ Parse HTML/scrape issue tracker web UIs
|
||||
|
||||
**Why?** Bypassing this capability causes:
|
||||
- Credential management chaos (tokens scattered everywhere)
|
||||
- Inconsistent issue state across agents
|
||||
- Massive token waste (redundant API calls)
|
||||
- Platform lock-in (can't switch Gitea → GitHub easily)
|
||||
- Race conditions in multi-agent scenarios
|
||||
|
||||
## Quick Reference
|
||||
|
||||
### Check if Capability is Available
|
||||
|
||||
```bash
|
||||
# Verify installation
|
||||
issue --version
|
||||
# or
|
||||
python -c "from issue_tracker.backends.gitea import GiteaBackend; print('OK')"
|
||||
```
|
||||
|
||||
### Basic Usage (Python)
|
||||
|
||||
```python
|
||||
from issue_tracker.backends.gitea import GiteaBackend
|
||||
from issue_tracker.core.models import Issue, Label, IssueState, User, Comment
|
||||
from issue_tracker.core.interfaces import IssueFilter
|
||||
from datetime import datetime, timezone
|
||||
import os
|
||||
|
||||
# Connect (assumes backend is configured)
|
||||
backend = GiteaBackend()
|
||||
backend.connect({
|
||||
'base_url': os.environ['GITEA_URL'],
|
||||
'token': os.environ['GITEA_API_TOKEN'],
|
||||
'owner': os.environ['GITEA_OWNER'],
|
||||
'repo': os.environ['GITEA_REPO']
|
||||
})
|
||||
|
||||
# List issues for me
|
||||
my_issues = backend.list_issues(IssueFilter(
|
||||
state='open',
|
||||
assignee='my-agent-id',
|
||||
labels=['needs-implementation']
|
||||
))
|
||||
|
||||
# Create issue
|
||||
new_issue = Issue(
|
||||
id=None, number=0,
|
||||
title="Implement feature X",
|
||||
description="Details...",
|
||||
state=IssueState.OPEN,
|
||||
created_at=datetime.now(timezone.utc),
|
||||
updated_at=datetime.now(timezone.utc),
|
||||
labels=[Label(name="feature"), Label(name="priority:high")]
|
||||
)
|
||||
created = backend.create_issue(new_issue)
|
||||
|
||||
# Update issue
|
||||
created.state = IssueState.IN_PROGRESS
|
||||
created.assignees = [User(id="agent-id", username="agent-id")]
|
||||
backend.update_issue(created)
|
||||
|
||||
# Add comment
|
||||
comment = Comment(
|
||||
id=None,
|
||||
body="Implementation started. Working on database schema.",
|
||||
author=User(id="agent-id", username="agent-id"),
|
||||
created_at=datetime.now(timezone.utc)
|
||||
)
|
||||
backend.add_comment(created.id, comment)
|
||||
|
||||
# Close when done
|
||||
created.state = IssueState.CLOSED
|
||||
created.closed_at = datetime.now(timezone.utc)
|
||||
backend.update_issue(created)
|
||||
```
|
||||
|
||||
### Basic Usage (CLI)
|
||||
|
||||
```bash
|
||||
# List my open issues
|
||||
issue list --state=open --assignee=agent-id --format=json
|
||||
|
||||
# Create issue
|
||||
issue create "Implement feature X" \
|
||||
--label=feature \
|
||||
--label=priority:high \
|
||||
--description="Details here"
|
||||
|
||||
# Update state
|
||||
issue edit 42 --state=in_progress --assignee=agent-id
|
||||
|
||||
# Add comment
|
||||
issue comment 42 "Implementation started"
|
||||
|
||||
# Close
|
||||
issue close 42 --comment="Completed successfully"
|
||||
```
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Pattern 1: Find Work
|
||||
|
||||
```python
|
||||
# Get next available task
|
||||
available_tasks = backend.list_issues(IssueFilter(
|
||||
state='open',
|
||||
labels=['ready', 'needs-implementation']
|
||||
))
|
||||
|
||||
# Filter to unassigned
|
||||
unassigned = [t for t in available_tasks if not t.assignees]
|
||||
|
||||
if unassigned:
|
||||
task = unassigned[0]
|
||||
# Claim it...
|
||||
```
|
||||
|
||||
### Pattern 2: Claim Issue (Prevent Race Conditions)
|
||||
|
||||
```python
|
||||
def claim_issue(issue: Issue, agent_id: str) -> bool:
|
||||
"""Claim an issue safely."""
|
||||
# Check if already claimed
|
||||
if issue.assignees:
|
||||
return False # Already taken
|
||||
|
||||
# Claim it
|
||||
issue.state = IssueState.IN_PROGRESS
|
||||
issue.assignees = [User(id=agent_id, username=agent_id)]
|
||||
backend.update_issue(issue)
|
||||
|
||||
# Announce claim
|
||||
backend.add_comment(issue.id, Comment(
|
||||
id=None,
|
||||
body=f"🤖 Claimed by {agent_id}",
|
||||
author=User(id=agent_id, username=agent_id),
|
||||
created_at=datetime.now(timezone.utc)
|
||||
))
|
||||
return True
|
||||
```
|
||||
|
||||
### Pattern 3: Progress Updates
|
||||
|
||||
```python
|
||||
def report_progress(issue: Issue, message: str, agent_id: str):
|
||||
"""Report progress on an issue."""
|
||||
backend.add_comment(issue.id, Comment(
|
||||
id=None,
|
||||
body=f"**Progress Update:**\n\n{message}",
|
||||
author=User(id=agent_id, username=agent_id),
|
||||
created_at=datetime.now(timezone.utc)
|
||||
))
|
||||
```
|
||||
|
||||
### Pattern 4: Agent-to-Agent Communication
|
||||
|
||||
```python
|
||||
import json
|
||||
|
||||
def post_agent_message(issue_id: str, msg_type: str, data: dict, agent_id: str):
|
||||
"""Post structured message for other agents."""
|
||||
message = {
|
||||
'type': msg_type,
|
||||
'agent': agent_id,
|
||||
'timestamp': datetime.now(timezone.utc).isoformat(),
|
||||
'data': data
|
||||
}
|
||||
backend.add_comment(issue_id, Comment(
|
||||
id=None,
|
||||
body=f"```agent-message\n{json.dumps(message, indent=2)}\n```",
|
||||
author=User(id=agent_id, username=agent_id),
|
||||
created_at=datetime.now(timezone.utc)
|
||||
))
|
||||
|
||||
def read_agent_messages(issue_id: str, msg_type: str = None):
|
||||
"""Read messages from other agents."""
|
||||
comments = backend.get_comments(issue_id)
|
||||
messages = []
|
||||
for comment in comments:
|
||||
if '```agent-message' in comment.body:
|
||||
try:
|
||||
json_str = comment.body.split('```agent-message\n')[1].split('\n```')[0]
|
||||
msg = json.loads(json_str)
|
||||
if msg_type is None or msg['type'] == msg_type:
|
||||
messages.append(msg)
|
||||
except:
|
||||
continue
|
||||
return messages
|
||||
```
|
||||
|
||||
## Configuration Check
|
||||
|
||||
Before using issue tracking, verify configuration:
|
||||
|
||||
```python
|
||||
def verify_issue_backend() -> bool:
|
||||
"""Verify issue backend is configured."""
|
||||
try:
|
||||
backend = GiteaBackend()
|
||||
backend.connect({
|
||||
'base_url': os.environ['GITEA_URL'],
|
||||
'token': os.environ['GITEA_API_TOKEN'],
|
||||
'owner': os.environ['GITEA_OWNER'],
|
||||
'repo': os.environ['GITEA_REPO']
|
||||
})
|
||||
return backend.test_connection()
|
||||
except Exception as e:
|
||||
print(f"Issue backend not configured: {e}")
|
||||
return False
|
||||
|
||||
# Use it
|
||||
if not verify_issue_backend():
|
||||
print("ERROR: Issue tracking not available. Check configuration.")
|
||||
sys.exit(1)
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
```python
|
||||
from issue_tracker.backends.gitea.backend import GiteaAPIError
|
||||
|
||||
try:
|
||||
issue = backend.get_issue_by_number(42)
|
||||
except GiteaAPIError as e:
|
||||
if e.status_code == 404:
|
||||
print("Issue not found")
|
||||
elif e.status_code == 401:
|
||||
print("Authentication failed - check GITEA_API_TOKEN")
|
||||
elif e.status_code == 429:
|
||||
print("Rate limited - wait and retry")
|
||||
else:
|
||||
print(f"API error: {e}")
|
||||
```
|
||||
|
||||
## Performance Tips
|
||||
|
||||
1. **Use filters** instead of fetching all issues:
|
||||
```python
|
||||
# BAD: Get all, filter in Python
|
||||
all_issues = backend.list_issues()
|
||||
my_issues = [i for i in all_issues if i.assignees and i.assignees[0].username == 'me']
|
||||
|
||||
# GOOD: Filter at backend
|
||||
my_issues = backend.list_issues(IssueFilter(assignee='me'))
|
||||
```
|
||||
|
||||
2. **Use JSON output** for CLI parsing:
|
||||
```bash
|
||||
issue list --format=json | jq '.[] | select(.state == "open")'
|
||||
```
|
||||
|
||||
3. **Batch comments** instead of rapid-fire updates
|
||||
|
||||
4. **Check local cache** before querying (if available)
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### "Backend not configured"
|
||||
```bash
|
||||
# Check config
|
||||
issue backend list
|
||||
|
||||
# If empty, configure
|
||||
export GITEA_API_TOKEN="your-token"
|
||||
issue backend add myproject gitea
|
||||
issue backend set-default myproject
|
||||
```
|
||||
|
||||
### "Authentication failed"
|
||||
```bash
|
||||
# Verify token
|
||||
curl -H "Authorization: token $GITEA_API_TOKEN" $GITEA_URL/api/v1/user
|
||||
```
|
||||
|
||||
### "Issue not found"
|
||||
```python
|
||||
# Use get_issue_by_number, not get_issue
|
||||
issue = backend.get_issue_by_number(42) # Correct
|
||||
# issue = backend.get_issue("42") # Wrong - needs backend_id
|
||||
```
|
||||
|
||||
## Full Documentation
|
||||
|
||||
- **Integration Guide:** `AGENT_INTEGRATION.md` (comprehensive patterns and strategies)
|
||||
- **API Reference:** `CLAUDE.md` (for developers extending the capability)
|
||||
- **Examples:** `examples/agents/` (working agent implementations)
|
||||
- **Roadmap:** `ROADMAP.md` (upcoming features)
|
||||
|
||||
## Current Limitations (v1.0)
|
||||
|
||||
Be aware of these limitations:
|
||||
|
||||
1. **Manual Configuration:** Backend must be configured before use (auto-detect in v1.1)
|
||||
2. **User Context:** Uses hardcoded user for CLI operations (agent identity in v1.2)
|
||||
3. **No Built-in Locking:** Use assignee + comment workaround for claiming (native in v1.2)
|
||||
4. **Basic Conflicts:** Manual resolution required for complex sync conflicts (advanced in v2.0)
|
||||
|
||||
Workarounds are documented in `AGENT_INTEGRATION.md`.
|
||||
|
||||
## Questions?
|
||||
|
||||
If you're unsure whether to use this capability for something:
|
||||
|
||||
**ASK:** "Does this involve creating, reading, updating, or searching issues?"
|
||||
- **YES** → Use this capability
|
||||
- **NO** → You can use other methods
|
||||
|
||||
**Example:**
|
||||
- "Create an issue for the bug I found" → **Use issue-facade**
|
||||
- "Read the project README" → Don't need issue-facade
|
||||
- "Check if issue #42 exists" → **Use issue-facade**
|
||||
- "Clone the repository" → Don't need issue-facade
|
||||
6
.claude/commands/use-issues.md
Normal file
6
.claude/commands/use-issues.md
Normal file
@@ -0,0 +1,6 @@
|
||||
Use the issue-facade capability for all issue tracking operations.
|
||||
|
||||
**Quick reference:** See `.claude/capabilities/issue-facade.md`
|
||||
**Examples:** See `capabilities/issue-facade/examples/agents/`
|
||||
|
||||
**DO NOT use direct API calls or platform CLIs!**
|
||||
8
.claude/context/capabilities.md
Normal file
8
.claude/context/capabilities.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# Available Capabilities
|
||||
|
||||
## Issue Tracking: issue-facade
|
||||
|
||||
**CRITICAL:** Always use this for issue operations. Never bypass with direct API calls.
|
||||
|
||||
**Docs:** `.claude/capabilities/issue-facade.md`
|
||||
**Usage:** `/use-issues`
|
||||
7
.gitmodules
vendored
7
.gitmodules
vendored
@@ -2,12 +2,13 @@
|
||||
path = wiki
|
||||
url = http://92.205.130.254:32166/coulomb/markitect_project.wiki.git
|
||||
branch = main
|
||||
[submodule "capabilities/issue-facade"]
|
||||
path = capabilities/issue-facade
|
||||
url = http://92.205.130.254:32166/coulomb/issue-facade.git
|
||||
[submodule "capabilities/kaizen-agentic"]
|
||||
path = capabilities/kaizen-agentic
|
||||
url = http://92.205.130.254:32166/coulomb/kaizen-agentic.git
|
||||
[submodule "capabilities/testdrive-jsui"]
|
||||
path = capabilities/testdrive-jsui
|
||||
url = http://92.205.130.254:32166/coulomb/testdrive-jsui.git
|
||||
[submodule "_issue-tracking/issue-facade"]
|
||||
path = _issue-tracking/issue-facade
|
||||
url = http://92.205.130.254:32166/coulomb/issue-facade.git
|
||||
branch = main
|
||||
|
||||
1
_issue-tracking/issue-facade
Submodule
1
_issue-tracking/issue-facade
Submodule
Submodule _issue-tracking/issue-facade added at f89772ac79
51
capabilities/DETACHED-issue-facade.yaml
Normal file
51
capabilities/DETACHED-issue-facade.yaml
Normal file
@@ -0,0 +1,51 @@
|
||||
# Detachment Manifest
|
||||
# This file records the removal of the issue-facade capability
|
||||
# Use this information to re-integrate with updated architecture
|
||||
|
||||
detachment:
|
||||
timestamp: 2025-12-17T21:23:14Z
|
||||
capability_name: issue-facade
|
||||
capability_family: issue-tracking
|
||||
integration_pattern: capabilities-directory
|
||||
original_location: /home/worsch/markitect_project/capabilities/issue-facade
|
||||
|
||||
capability_metadata:
|
||||
spec_file: CAPABILITY-issue-tracking.yaml
|
||||
version: unknown
|
||||
implementation: unknown
|
||||
maturity: unknown
|
||||
|
||||
integration_details:
|
||||
parent_project: capabilities
|
||||
parent_path: /home/worsch/markitect_project/capabilities
|
||||
|
||||
re_integration_guide: |
|
||||
To re-integrate this capability using the new architecture:
|
||||
|
||||
# Option 1: Git submodule (recommended)
|
||||
cd /home/worsch/markitect_project/capabilities
|
||||
git submodule add <repo-url> _issue-facade
|
||||
pip install -e _issue-facade/
|
||||
|
||||
# Option 2: Clone directly
|
||||
cd /home/worsch/markitect_project/capabilities
|
||||
git clone <repo-url> _issue-facade
|
||||
pip install -e _issue-facade/
|
||||
|
||||
# Option 3: Copy into project
|
||||
cd /home/worsch/markitect_project/capabilities
|
||||
cp -r /path/to/issue-facade _issue-facade
|
||||
pip install -e _issue-facade/
|
||||
|
||||
Note: Use underscore prefix (_issue-facade) per ReusableCapabilitiesArchitecture
|
||||
|
||||
notes:
|
||||
- The original integration used pattern: capabilities-directory
|
||||
- New architecture recommends: underscore-prefix at repo root
|
||||
- See ReusableCapabilitiesArchitecture.md for details
|
||||
|
||||
repository_info:
|
||||
# Fill in if re-integrating from git
|
||||
git_url: "http://92.205.130.254:32166/coulomb/issue-facade.git" # e.g., https://github.com/markitect/issue-facade
|
||||
git_branch: "main" # e.g., main
|
||||
git_commit: "35daa514e59788250847cd706c43ea78f24c5c1d" # Optional: specific commit to use
|
||||
Submodule capabilities/issue-facade deleted from 34a8bc7d4c
Submodule capabilities/testdrive-jsui updated: f5ce02cf8d...b8f13b4ae5
158
examples/design-patterns/CopyFirstMigration.md
Normal file
158
examples/design-patterns/CopyFirstMigration.md
Normal file
@@ -0,0 +1,158 @@
|
||||
# Design Principle: Copy First Migration
|
||||
|
||||
## Meta
|
||||
- **Name:** Copy First Migration
|
||||
- **ShortName:** CopyFirst
|
||||
- **Version:** 0.1
|
||||
- **Status:** Draft
|
||||
- **Tags:** refactoring, migration, safety, testing, legacy
|
||||
- **RelatedPrinciples:** Don’t Repeat Yourself, Safe Refactoring, Test Pyramid, Capability-Based Testing
|
||||
|
||||
---
|
||||
|
||||
## Intent
|
||||
Enable safe refactoring and structural migration of codebases by preserving
|
||||
existing, working functionality until the new implementation is fully verified.
|
||||
|
||||
This principle prioritizes **reversibility, confidence, and continuity** over
|
||||
speed or elegance.
|
||||
|
||||
---
|
||||
|
||||
## CoreStatement
|
||||
Never move code directly; always copy first and delete only after verified
|
||||
behavioral equivalence is established.
|
||||
|
||||
---
|
||||
|
||||
## Scope
|
||||
|
||||
### InScope
|
||||
- Large-scale refactors or directory restructurings
|
||||
- Technology or language migrations (e.g. JS → new JS layout, JS → Python integration)
|
||||
- Legacy code stabilization
|
||||
- Safety-critical or business-critical systems
|
||||
- Situations with incomplete test coverage
|
||||
|
||||
### OutOfScope
|
||||
- Greenfield development
|
||||
- Trivial refactors with full and trusted test coverage
|
||||
- One-off throwaway scripts
|
||||
- Performance-driven rewrites where duplication is unacceptable
|
||||
|
||||
---
|
||||
|
||||
## InterpretationGuidelines
|
||||
|
||||
### What “Copy First” Means
|
||||
- The original code remains untouched and functional
|
||||
- The new version is treated as **experimental until proven**
|
||||
- Deletion is a **final, explicit act**, not an implicit side effect
|
||||
|
||||
### Common Misinterpretations
|
||||
- “This is inefficient because it duplicates code”
|
||||
→ Duplication is intentional and temporary
|
||||
- “Moving files is faster”
|
||||
→ Speed is not the optimization target here
|
||||
- “Tests alone are enough”
|
||||
→ Tests are necessary but not sufficient without behavioral comparison
|
||||
|
||||
---
|
||||
|
||||
## DetectionHeuristics
|
||||
|
||||
### Structural Signals
|
||||
- Files or modules being relocated across directories or packages
|
||||
- Parallel implementations during migration
|
||||
- Introduction of a new architectural boundary
|
||||
|
||||
### Semantic Signals
|
||||
- Code paths that must remain behaviorally identical
|
||||
- Business rules with high regression risk
|
||||
- Legacy logic that is poorly documented but relied upon
|
||||
|
||||
### Change-Cost Signals
|
||||
- Rollbacks are expensive or disruptive
|
||||
- Failures would impact production or customers
|
||||
- Migration spans multiple commits or teams
|
||||
|
||||
---
|
||||
|
||||
## DiagnosticQuestions
|
||||
1. What breaks if this migration is wrong?
|
||||
2. Do we have a known-good reference implementation?
|
||||
3. Can both old and new code paths run in parallel?
|
||||
4. How quickly can we revert if a defect is found?
|
||||
5. What is the minimal proof of behavioral equivalence?
|
||||
|
||||
---
|
||||
|
||||
## RecommendedActions
|
||||
|
||||
### Low-Risk Actions
|
||||
- Copy files to the new location instead of moving
|
||||
- Preserve original imports and entry points
|
||||
- Add logging or tracing for comparison
|
||||
|
||||
### Medium-Risk Actions
|
||||
- Introduce dual-track execution (old + new)
|
||||
- Add integration tests targeting both implementations
|
||||
- Compare outputs, side effects, and error behavior
|
||||
|
||||
### High-Risk Actions
|
||||
- Switch production usage to the new implementation
|
||||
- Remove old code only after full verification
|
||||
- Collapse duplicated paths once confidence is established
|
||||
|
||||
---
|
||||
|
||||
## AcceptanceCriteria
|
||||
- Original code remains functional until final removal
|
||||
- New code passes all existing tests
|
||||
- New integration tests validate identical behavior
|
||||
- Dual-track comparisons show no regressions
|
||||
- Deletion of old code is deliberate and reversible up to the final step
|
||||
|
||||
---
|
||||
|
||||
## AntiPatterns
|
||||
- Moving files directly without a fallback
|
||||
- Refactoring and migration in a single irreversible step
|
||||
- Deleting “unused” code before equivalence is proven
|
||||
- Assuming test parity guarantees behavioral parity
|
||||
- Big-bang migrations without rollback paths
|
||||
|
||||
---
|
||||
|
||||
## Tradeoffs
|
||||
Applying Copy First Migration intentionally:
|
||||
- Introduces temporary duplication
|
||||
- Increases short-term codebase size
|
||||
- Slows perceived progress
|
||||
|
||||
These costs are justified by dramatically reduced risk and higher confidence
|
||||
during complex migrations.
|
||||
|
||||
---
|
||||
|
||||
## AgentUsage
|
||||
|
||||
### When to Apply This Lens
|
||||
- During directory, module, or architecture migrations
|
||||
- When refactoring legacy or poorly understood code
|
||||
- When safety and uptime matter more than speed
|
||||
- When rollback must remain possible at all times
|
||||
|
||||
### When to Suspend This Lens
|
||||
- In greenfield projects
|
||||
- When full test coverage and confidence already exist
|
||||
- For trivial mechanical refactors
|
||||
|
||||
### Expected Agent Output
|
||||
- Identification of migration boundaries
|
||||
- Copy-first migration plan with explicit stages
|
||||
- Test strategy (unit, integration, dual-track)
|
||||
- Rollback points and deletion criteria
|
||||
- Clear signal for when old code may be removed
|
||||
|
||||
xxx
|
||||
135
examples/design-patterns/DesignPrincipleSchema.json
Normal file
135
examples/design-patterns/DesignPrincipleSchema.json
Normal file
@@ -0,0 +1,135 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"type": "object",
|
||||
"title": "Schema for DesignPrinciples",
|
||||
"description": "JSON schema describing the markdown structure of OperationalKnowledge DesignPrinciples",
|
||||
"properties": {
|
||||
"headings": {
|
||||
"type": "object",
|
||||
"description": "Document heading structure",
|
||||
"properties": {
|
||||
"level_1": {
|
||||
"type": "array",
|
||||
"description": "Headings at level 1",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"content": {
|
||||
"type": "string"
|
||||
},
|
||||
"level": {
|
||||
"type": "integer"
|
||||
},
|
||||
"position": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"content",
|
||||
"level"
|
||||
]
|
||||
},
|
||||
"minItems": 1,
|
||||
"maxItems": 1
|
||||
},
|
||||
"level_2": {
|
||||
"type": "array",
|
||||
"description": "Headings at level 2",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"content": {
|
||||
"type": "string"
|
||||
},
|
||||
"level": {
|
||||
"type": "integer"
|
||||
},
|
||||
"position": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"content",
|
||||
"level"
|
||||
]
|
||||
},
|
||||
"minItems": 4,
|
||||
"maxItems": 12
|
||||
},
|
||||
"level_3": {
|
||||
"type": "array",
|
||||
"description": "Headings at level 3",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"content": {
|
||||
"type": "string"
|
||||
},
|
||||
"level": {
|
||||
"type": "integer"
|
||||
},
|
||||
"position": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"content",
|
||||
"level"
|
||||
]
|
||||
},
|
||||
"minItems": 0,
|
||||
"maxItems": 40
|
||||
}
|
||||
}
|
||||
},
|
||||
"paragraphs": {
|
||||
"type": "array",
|
||||
"description": "Text paragraphs",
|
||||
"minItems": 8,
|
||||
"maxItems": 120
|
||||
},
|
||||
"lists": {
|
||||
"type": "array",
|
||||
"description": "Lists (ordered and unordered)",
|
||||
"minItems": 0,
|
||||
"maxItems": 20
|
||||
},
|
||||
"emphasis": {
|
||||
"type": "array",
|
||||
"description": "Text emphasis (bold, italic)",
|
||||
"minItems": 0,
|
||||
"maxItems": 120
|
||||
},
|
||||
"metadata": {
|
||||
"type": "object",
|
||||
"description": "Document structure metadata",
|
||||
"properties": {
|
||||
"total_elements": {
|
||||
"type": "integer",
|
||||
"const": 115
|
||||
},
|
||||
"structure_types": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"description": "All structural element types found",
|
||||
"const": [
|
||||
"paragraph_close",
|
||||
"heading_close",
|
||||
"hr",
|
||||
"bullet_list_open",
|
||||
"paragraph_open",
|
||||
"heading_open",
|
||||
"ordered_list_open",
|
||||
"ordered_list_close",
|
||||
"inline",
|
||||
"list_item_close",
|
||||
"list_item_open",
|
||||
"bullet_list_close"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
160
examples/design-patterns/DontRepeatYourself.md
Normal file
160
examples/design-patterns/DontRepeatYourself.md
Normal file
@@ -0,0 +1,160 @@
|
||||
# Design Principle: Don’t Repeat Yourself (DRY)
|
||||
|
||||
## Meta
|
||||
- **Name:** Don’t Repeat Yourself
|
||||
- **ShortName:** DRY
|
||||
- **Version:** 0.1
|
||||
- **Status:** Stable
|
||||
- **Tags:** maintainability, refactoring, architecture, quality
|
||||
- **RelatedPrinciples:** Single Responsibility, YAGNI, Separation of Concerns
|
||||
|
||||
---
|
||||
|
||||
## Intent
|
||||
Reduce maintenance cost and behavioral drift by ensuring that each piece of
|
||||
knowledge, rule, or decision logic has a single authoritative representation
|
||||
in the codebase.
|
||||
|
||||
---
|
||||
|
||||
## CoreStatement
|
||||
A codebase violates DRY when the same knowledge is expressed in multiple places
|
||||
such that a change would require edits in more than one location or risks
|
||||
inconsistent behavior.
|
||||
|
||||
---
|
||||
|
||||
## Scope
|
||||
|
||||
### InScope
|
||||
- Business rules and decision logic
|
||||
- Algorithms and validation logic
|
||||
- Data schemas, DTOs, and field definitions
|
||||
- Configuration values and feature flags
|
||||
- Repeated workflows or orchestration logic
|
||||
- Test setup and invariant test scenarios
|
||||
|
||||
### OutOfScope
|
||||
- Superficial textual similarity without shared meaning
|
||||
- Intentional duplication for isolation or clarity
|
||||
- Early-stage exploratory code where abstractions are not yet clear
|
||||
- Performance-driven duplication with explicit justification
|
||||
|
||||
---
|
||||
|
||||
## InterpretationGuidelines
|
||||
|
||||
### What “Repeat” Means
|
||||
DRY is about **duplication of knowledge**, not duplication of text.
|
||||
|
||||
Examples of knowledge duplication:
|
||||
- The same validation rule implemented in multiple services
|
||||
- Identical conditional logic controlling the same behavior
|
||||
- The same data structure defined independently in multiple modules
|
||||
|
||||
### Common Misinterpretations
|
||||
- “Any repeated code is bad” (false)
|
||||
- “DRY means maximum abstraction” (false)
|
||||
- “Utility modules automatically improve DRY” (often false)
|
||||
|
||||
---
|
||||
|
||||
## DetectionHeuristics
|
||||
|
||||
### Structural Signals
|
||||
- Functions with highly similar bodies and signatures
|
||||
- Repeated constants, strings, regexes, or SQL fragments
|
||||
- Parallel modules with mirrored internal structure
|
||||
|
||||
### Semantic Signals
|
||||
- Identical error messages or validation rules in different layers
|
||||
- Repeated mapping logic between the same concepts
|
||||
- Copy-paste variations differing only in naming
|
||||
|
||||
### Change-Cost Signals
|
||||
- A requirement change touches multiple files for the same reason
|
||||
- Fixes applied in one location but missing in others
|
||||
- Tests failing inconsistently after partial updates
|
||||
|
||||
---
|
||||
|
||||
## DiagnosticQuestions
|
||||
1. Is this duplication representing the same rule or policy?
|
||||
2. If this rule changes, how many places must be updated?
|
||||
3. Is the duplicated logic stable or likely to evolve?
|
||||
4. Are the differences intentional or accidental?
|
||||
5. Where is the natural “source of truth” for this knowledge?
|
||||
6. Would abstraction reduce or increase cognitive load?
|
||||
|
||||
---
|
||||
|
||||
## RecommendedActions
|
||||
|
||||
### Low-Risk Refactors
|
||||
- Extract constants or configuration values
|
||||
- Centralize literals and error messages
|
||||
- Introduce shared test fixtures or helpers
|
||||
|
||||
### Medium-Risk Refactors
|
||||
- Extract pure helper functions
|
||||
- Introduce shared domain services or modules
|
||||
- Unify schema/type definitions
|
||||
|
||||
### High-Risk Refactors
|
||||
- Introduce strategy/template patterns
|
||||
- Merge parallel subsystems
|
||||
- Redesign domain boundaries to align ownership of rules
|
||||
|
||||
---
|
||||
|
||||
## AcceptanceCriteria
|
||||
- Each rule or behavior has a single authoritative implementation
|
||||
- Required changes affect fewer locations than before
|
||||
- Naming reflects domain meaning, not technical convenience
|
||||
- Tests pass without behavior regression
|
||||
- Coupling does not increase unintentionally
|
||||
|
||||
---
|
||||
|
||||
## AntiPatterns
|
||||
- “God” utility modules with unrelated helpers
|
||||
- Over-generalized abstractions with many parameters
|
||||
- Shared code across domains that should evolve independently
|
||||
- Premature abstraction of coincidental similarities
|
||||
- Hiding meaningful differences behind generic interfaces
|
||||
|
||||
---
|
||||
|
||||
## Tradeoffs
|
||||
Applying DRY may:
|
||||
- Increase indirection
|
||||
- Reduce local readability
|
||||
- Introduce coupling between modules
|
||||
|
||||
These costs are acceptable only when outweighed by reduced change cost
|
||||
and increased behavioral consistency.
|
||||
|
||||
---
|
||||
|
||||
## AgentUsage
|
||||
|
||||
### When to Apply This Lens
|
||||
- During refactoring or maintenance work
|
||||
- When change requests repeatedly touch similar code
|
||||
- When bugs recur due to partial updates
|
||||
- During architectural consolidation
|
||||
|
||||
### When to Suspend This Lens
|
||||
- During early exploration or prototyping
|
||||
- When future variability is unclear
|
||||
- When isolation is more valuable than reuse
|
||||
|
||||
### Expected Agent Output
|
||||
- Identified DRY violations with locations
|
||||
- Rationale for why duplication matters
|
||||
- Volatility assessment (stable vs evolving)
|
||||
- Recommended refactor type and target
|
||||
- Risk notes and minimal patch sequence
|
||||
|
||||
xxx
|
||||
|
||||
@@ -1284,11 +1284,25 @@ MISSING: {len(missing_components)} components
|
||||
html_content = markdown_content_with_dogtag.replace('\n\n', '</p><p>').replace('\n', '<br>')
|
||||
html_content = f'<p>{html_content}</p>'
|
||||
|
||||
# Generate or read CSS content
|
||||
if css:
|
||||
# If css is a file path, read it
|
||||
css_path = Path(css)
|
||||
if css_path.exists() and css_path.is_file():
|
||||
css_content = f'<style>\n{css_path.read_text(encoding="utf-8")}\n</style>'
|
||||
else:
|
||||
# Assume it's raw CSS content
|
||||
css_content = f'<style>\n{css}\n</style>'
|
||||
else:
|
||||
# Use template-based CSS generation
|
||||
css_content = self._get_template_css(template, image_max_width, image_max_height)
|
||||
|
||||
# Replace template placeholders using safe string replacement
|
||||
# This avoids conflicts with CSS curly braces
|
||||
html_template = template_content.replace('{title}', title)
|
||||
html_template = html_template.replace('{version}', version_str)
|
||||
html_template = html_template.replace('{content}', html_content)
|
||||
html_template = html_template.replace('{css_content}', css_content)
|
||||
|
||||
return html_template
|
||||
|
||||
@@ -1302,8 +1316,18 @@ MISSING: {len(missing_components)} components
|
||||
|
||||
template_content = template_path.read_text(encoding='utf-8')
|
||||
|
||||
# Generate CSS
|
||||
css_content = self._get_template_css(template, image_max_width, image_max_height) if not css else css
|
||||
# Generate or read CSS content
|
||||
if css:
|
||||
# If css is a file path, read it
|
||||
css_path = Path(css)
|
||||
if css_path.exists() and css_path.is_file():
|
||||
css_content = f'<style>\n{css_path.read_text(encoding="utf-8")}\n</style>'
|
||||
else:
|
||||
# Assume it's raw CSS content
|
||||
css_content = f'<style>\n{css}\n</style>'
|
||||
else:
|
||||
# Use template-based CSS generation
|
||||
css_content = self._get_template_css(template, image_max_width, image_max_height)
|
||||
|
||||
# Create configuration object - ONLY dynamic data interface
|
||||
config = {
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
<meta name="generator" content="Markitect {version}">
|
||||
<title>{title}</title>
|
||||
|
||||
{css_content}
|
||||
|
||||
<!-- Base styling for document content -->
|
||||
<style>
|
||||
body {
|
||||
|
||||
@@ -1,247 +0,0 @@
|
||||
"""
|
||||
Test suite for the new clean architecture implementation
|
||||
Tests the JSON configuration interface and separation of concerns
|
||||
"""
|
||||
import pytest
|
||||
import tempfile
|
||||
import json
|
||||
from pathlib import Path
|
||||
from markitect.clean_document_manager import CleanDocumentManager
|
||||
|
||||
|
||||
class TestCleanArchitecture:
|
||||
"""Test suite for clean JavaScript-Python separation"""
|
||||
|
||||
def setup_method(self):
|
||||
"""Setup for each test"""
|
||||
self.manager = CleanDocumentManager()
|
||||
|
||||
def test_clean_edit_mode_json_configuration(self):
|
||||
"""Test that edit mode uses clean JSON configuration interface"""
|
||||
test_markdown = '''# Test Document
|
||||
|
||||
## Section with Problematic Content
|
||||
```python
|
||||
script = f"""
|
||||
function test() {
|
||||
console.log("Hello {name}");
|
||||
}
|
||||
"""
|
||||
```
|
||||
|
||||
This content has quotes that previously broke JavaScript generation.
|
||||
'''
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as md_file:
|
||||
md_file.write(test_markdown)
|
||||
md_file.flush()
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as html_file:
|
||||
result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=html_file.name,
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
assert result['success'] is True
|
||||
|
||||
# Read generated HTML
|
||||
html_content = Path(html_file.name).read_text()
|
||||
|
||||
# Test 1: Check for clean template usage
|
||||
assert 'markitect-config' in html_content
|
||||
assert 'type="application/json"' in html_content
|
||||
|
||||
# Test 2: Extract and validate JSON configuration
|
||||
config_json = self.extract_config_json(html_content)
|
||||
assert config_json is not None, "Configuration JSON not found"
|
||||
|
||||
config = json.loads(config_json)
|
||||
|
||||
# Test 3: Validate configuration structure
|
||||
required_fields = ['markdownContent', 'mode', 'theme', 'originalFilename']
|
||||
for field in required_fields:
|
||||
assert field in config, f"Required field '{field}' missing from configuration"
|
||||
|
||||
# Test 4: Check that problematic content is properly escaped
|
||||
assert 'script = f"""' in config['markdownContent'] # Should be in JSON
|
||||
assert '"""' not in html_content.split('markitect-config')[1].split('</script>')[0], "Unescaped quotes in HTML"
|
||||
|
||||
def test_clean_architecture_no_python_js_mixing(self):
|
||||
"""Test that no Python code generates JavaScript strings"""
|
||||
test_markdown = "# Simple Test\n\nBasic content."
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as md_file:
|
||||
md_file.write(test_markdown)
|
||||
md_file.flush()
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as html_file:
|
||||
result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=html_file.name,
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
assert result['success'] is True
|
||||
html_content = Path(html_file.name).read_text()
|
||||
|
||||
# Test 1: No direct JavaScript variable assignments from Python
|
||||
problematic_patterns = [
|
||||
'const markdownContent = "', # Old way
|
||||
'const markdownContentWithDogtag = "', # Old way
|
||||
'var markdownContent = "',
|
||||
'let markdownContent = "'
|
||||
]
|
||||
|
||||
for pattern in problematic_patterns:
|
||||
assert pattern not in html_content, f"Found problematic pattern: {pattern}"
|
||||
|
||||
# Test 2: Configuration should be in JSON script tag only
|
||||
config_sections = html_content.count('markitect-config')
|
||||
assert config_sections >= 2, f"Expected at least 2 config references (opening and closing), found {config_sections}"
|
||||
|
||||
# Test 3: JavaScript files should be embedded inline (no external src attributes)
|
||||
js_components = [
|
||||
'config-loader',
|
||||
'section-manager',
|
||||
'dom-renderer'
|
||||
]
|
||||
|
||||
for component in js_components:
|
||||
# Check that the component JavaScript is embedded, not referenced externally
|
||||
assert f'src="js/' not in html_content, "Found external JavaScript references - should be embedded"
|
||||
|
||||
# Check that components are embedded inline
|
||||
assert '{js_config_loader}' not in html_content, "Template placeholder not replaced"
|
||||
assert 'class MarkitectConfig' in html_content, "Config loader not embedded"
|
||||
assert 'class SectionManager' in html_content, "Section manager not embedded"
|
||||
|
||||
def test_configuration_interface_completeness(self):
|
||||
"""Test that all required data is passed through the configuration interface"""
|
||||
test_markdown = "# Config Test\n\nTesting configuration completeness."
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as md_file:
|
||||
md_file.write(test_markdown)
|
||||
md_file.flush()
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as html_file:
|
||||
result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=html_file.name,
|
||||
edit_mode=True,
|
||||
editor_theme='dark',
|
||||
keyboard_shortcuts=False
|
||||
)
|
||||
|
||||
assert result['success'] is True
|
||||
html_content = Path(html_file.name).read_text()
|
||||
|
||||
config_json = self.extract_config_json(html_content)
|
||||
config = json.loads(config_json)
|
||||
|
||||
# Test configuration completeness
|
||||
expected_config = {
|
||||
'markdownContent': test_markdown,
|
||||
'mode': 'edit',
|
||||
'theme': 'dark',
|
||||
'keyboardShortcuts': False,
|
||||
'autosave': False,
|
||||
'sections': True,
|
||||
'base64References': {}
|
||||
}
|
||||
|
||||
for key, expected_value in expected_config.items():
|
||||
assert key in config, f"Configuration missing key: {key}"
|
||||
if key == 'markdownContent':
|
||||
assert config[key] == expected_value, f"Configuration {key} value mismatch"
|
||||
|
||||
def test_insert_mode_configuration(self):
|
||||
"""Test insert mode specific configuration"""
|
||||
test_markdown = "# Insert Mode Test"
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as md_file:
|
||||
md_file.write(test_markdown)
|
||||
md_file.flush()
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as html_file:
|
||||
result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=html_file.name,
|
||||
insert_mode=True
|
||||
)
|
||||
|
||||
assert result['success'] is True
|
||||
html_content = Path(html_file.name).read_text()
|
||||
|
||||
# Check body class
|
||||
assert 'class="markitect-insert-mode"' in html_content
|
||||
|
||||
# Check configuration
|
||||
config_json = self.extract_config_json(html_content)
|
||||
config = json.loads(config_json)
|
||||
|
||||
assert config['mode'] == 'insert'
|
||||
assert 'restrictedHeadingLevels' in config
|
||||
assert config['restrictedHeadingLevels'] == [1, 2, 3]
|
||||
|
||||
def test_static_vs_edit_mode_separation(self):
|
||||
"""Test that static mode and edit mode use different templates"""
|
||||
test_markdown = "# Mode Test\n\nTesting template separation."
|
||||
|
||||
# Test static mode
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as md_file:
|
||||
md_file.write(test_markdown)
|
||||
md_file.flush()
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as static_file:
|
||||
static_result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=static_file.name,
|
||||
edit_mode=False
|
||||
)
|
||||
|
||||
static_content = Path(static_file.name).read_text()
|
||||
|
||||
# Static mode should NOT have configuration interface
|
||||
assert 'markitect-config' not in static_content
|
||||
assert 'application/json' not in static_content
|
||||
|
||||
# Test edit mode
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as edit_file:
|
||||
edit_result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=edit_file.name,
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
edit_content = Path(edit_file.name).read_text()
|
||||
|
||||
# Edit mode should HAVE configuration interface
|
||||
assert 'markitect-config' in edit_content
|
||||
assert 'application/json' in edit_content
|
||||
|
||||
# Helper methods
|
||||
|
||||
def extract_config_json(self, html_content):
|
||||
"""Extract JSON configuration from HTML"""
|
||||
try:
|
||||
# Find the config script tag
|
||||
start_marker = 'id="markitect-config" type="application/json">'
|
||||
end_marker = '</script>'
|
||||
|
||||
start_pos = html_content.find(start_marker)
|
||||
if start_pos == -1:
|
||||
return None
|
||||
|
||||
start_pos += len(start_marker)
|
||||
end_pos = html_content.find(end_marker, start_pos)
|
||||
|
||||
if end_pos == -1:
|
||||
return None
|
||||
|
||||
config_json = html_content[start_pos:end_pos].strip()
|
||||
return config_json
|
||||
|
||||
except Exception as e:
|
||||
print(f"Failed to extract config JSON: {e}")
|
||||
return None
|
||||
@@ -1,246 +0,0 @@
|
||||
"""
|
||||
Tests for Issue #132: Basic HTML Generation and Rendering
|
||||
|
||||
This module tests the core functionality of the md-render command for
|
||||
client-side markdown rendering with JavaScript.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
import tempfile
|
||||
import os
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch, MagicMock
|
||||
import json
|
||||
import re
|
||||
|
||||
# Add project root to path for imports
|
||||
import sys
|
||||
project_root = Path(__file__).parent.parent.parent.parent
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
from markitect.plugins.builtin.markdown_commands import MarkdownCommandsPlugin
|
||||
|
||||
|
||||
class TestIssue132BasicRendering:
|
||||
"""Test basic HTML generation and markdown rendering functionality."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up test environment."""
|
||||
self.plugin = MarkdownCommandsPlugin()
|
||||
self.plugin.initialize()
|
||||
|
||||
# Create temporary directory for test outputs
|
||||
self.temp_dir = tempfile.mkdtemp()
|
||||
|
||||
def teardown_method(self):
|
||||
"""Clean up test environment."""
|
||||
# Clean up temporary files
|
||||
import shutil
|
||||
shutil.rmtree(self.temp_dir, ignore_errors=True)
|
||||
|
||||
def test_md_render_command_exists(self):
|
||||
"""Test that md-render command is registered in plugin - Issue #132."""
|
||||
commands = self.plugin.get_commands()
|
||||
|
||||
# Should include md-render command
|
||||
assert 'md-render' in commands
|
||||
|
||||
# Command should be callable
|
||||
md_render_cmd = commands['md-render']
|
||||
assert callable(md_render_cmd)
|
||||
|
||||
def test_generate_basic_html_from_simple_markdown(self):
|
||||
"""Test generating HTML from simple markdown content - Issue #132."""
|
||||
# Create test markdown content
|
||||
markdown_content = """# Test Document
|
||||
|
||||
This is a **test** document with some *italic* text and a [link](https://example.com).
|
||||
|
||||
## Section 2
|
||||
|
||||
- List item 1
|
||||
- List item 2
|
||||
- List item 3
|
||||
"""
|
||||
|
||||
# Create temporary input file
|
||||
input_file = Path(self.temp_dir) / "test.md"
|
||||
input_file.write_text(markdown_content)
|
||||
|
||||
output_file = Path(self.temp_dir) / "output.html"
|
||||
|
||||
# Test actual command execution
|
||||
from markitect.plugins.builtin.markdown_commands import md_render_command
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(md_render_command, [str(input_file), '--output', str(output_file), '--nodogtag'])
|
||||
|
||||
# Should execute successfully
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
# Should generate HTML file with content
|
||||
html_content = output_file.read_text()
|
||||
assert '<!DOCTYPE html>' in html_content
|
||||
assert '<title>Test Document</title>' in html_content
|
||||
|
||||
def test_html_contains_embedded_markdown_payload(self):
|
||||
"""Test that generated HTML contains markdown as JavaScript payload - Issue #132."""
|
||||
markdown_content = "# Simple Test\n\nThis is test content."
|
||||
|
||||
input_file = Path(self.temp_dir) / "simple.md"
|
||||
input_file.write_text(markdown_content)
|
||||
|
||||
output_file = Path(self.temp_dir) / "simple.html"
|
||||
|
||||
# Test actual rendering
|
||||
from markitect.plugins.builtin.markdown_commands import md_render_command
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(md_render_command, [str(input_file), '--output', str(output_file), '--nodogtag'])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should contain JavaScript with embedded markdown
|
||||
assert 'const markdownContent =' in html_content
|
||||
assert json.dumps(markdown_content) in html_content
|
||||
|
||||
# Should contain script tag for rendering
|
||||
assert '<script' in html_content
|
||||
assert 'marked' in html_content.lower()
|
||||
|
||||
def test_html_includes_javascript_markdown_parser(self):
|
||||
"""Test that generated HTML includes JavaScript markdown parser - Issue #132."""
|
||||
markdown_content = "# Parser Test\n\nTesting parser inclusion."
|
||||
|
||||
input_file = Path(self.temp_dir) / "parser_test.md"
|
||||
input_file.write_text(markdown_content)
|
||||
|
||||
output_file = Path(self.temp_dir) / "parser_test.html"
|
||||
|
||||
# Test actual rendering
|
||||
from markitect.plugins.builtin.markdown_commands import md_render_command
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(md_render_command, [str(input_file), '--output', str(output_file), '--nodogtag'])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should include markdown parser (marked.js or similar)
|
||||
assert any(parser in html_content.lower() for parser in ['marked', 'markdown-it', 'showdown'])
|
||||
|
||||
# Should include rendering logic
|
||||
assert 'DOMContentLoaded' in html_content or 'window.onload' in html_content
|
||||
|
||||
def test_generated_html_is_valid_structure(self):
|
||||
"""Test that generated HTML has valid document structure - Issue #132."""
|
||||
markdown_content = "# Structure Test\n\nTesting HTML structure."
|
||||
|
||||
input_file = Path(self.temp_dir) / "structure.md"
|
||||
input_file.write_text(markdown_content)
|
||||
|
||||
output_file = Path(self.temp_dir) / "structure.html"
|
||||
|
||||
# Test actual rendering
|
||||
from markitect.plugins.builtin.markdown_commands import md_render_command
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(md_render_command, [str(input_file), '--output', str(output_file), '--nodogtag'])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Valid HTML5 document structure
|
||||
assert html_content.startswith('<!DOCTYPE html>')
|
||||
assert '<html' in html_content
|
||||
assert '<head>' in html_content
|
||||
assert '<body>' in html_content
|
||||
assert '</html>' in html_content
|
||||
|
||||
# Should have content div for rendering
|
||||
assert 'id="markdown-content"' in html_content
|
||||
|
||||
def test_handles_empty_markdown_file(self):
|
||||
"""Test behavior with empty markdown file - Issue #132."""
|
||||
# Create empty markdown file
|
||||
input_file = Path(self.temp_dir) / "empty.md"
|
||||
input_file.write_text("")
|
||||
|
||||
output_file = Path(self.temp_dir) / "empty.html"
|
||||
|
||||
# Test actual rendering
|
||||
from markitect.plugins.builtin.markdown_commands import md_render_command
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(md_render_command, [str(input_file), '--output', str(output_file), '--nodogtag'])
|
||||
|
||||
# Should handle empty file gracefully
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
# Should still generate valid HTML structure
|
||||
assert '<!DOCTYPE html>' in html_content
|
||||
assert 'const markdownContent = "";' in html_content
|
||||
|
||||
def test_handles_markdown_with_code_blocks(self):
|
||||
"""Test handling markdown with code blocks - Issue #132."""
|
||||
markdown_content = """# Code Test
|
||||
|
||||
Here's some Python code:
|
||||
|
||||
```python
|
||||
def hello_world():
|
||||
print("Hello, World!")
|
||||
return True
|
||||
```
|
||||
|
||||
And some inline `code` too.
|
||||
"""
|
||||
|
||||
input_file = Path(self.temp_dir) / "code_test.md"
|
||||
input_file.write_text(markdown_content)
|
||||
|
||||
output_file = Path(self.temp_dir) / "code_test.html"
|
||||
|
||||
# Test actual rendering with code blocks
|
||||
from markitect.plugins.builtin.markdown_commands import md_render_command
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(md_render_command, [str(input_file), '--output', str(output_file), '--nodogtag'])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should properly escape code content in JavaScript
|
||||
assert 'def hello_world' in html_content
|
||||
# Should handle backticks and quotes properly
|
||||
assert json.dumps(markdown_content) in html_content
|
||||
|
||||
def test_cli_command_interface_exists(self):
|
||||
"""Test that md-render CLI command interface exists - Issue #132."""
|
||||
from markitect.cli import cli
|
||||
|
||||
# Should have md-render command registered
|
||||
assert 'md-render' in cli.commands
|
||||
|
||||
cmd = cli.commands['md-render']
|
||||
assert cmd.name == 'md-render'
|
||||
assert cmd.help is not None
|
||||
assert 'markdown' in cmd.help.lower()
|
||||
@@ -1,402 +0,0 @@
|
||||
"""
|
||||
Tests for Issue #132: Template System and CSS Injection
|
||||
|
||||
This module tests template selection and custom CSS injection functionality
|
||||
for client-side markdown rendering.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
import tempfile
|
||||
import os
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch, MagicMock
|
||||
import json
|
||||
|
||||
# Add project root to path for imports
|
||||
import sys
|
||||
project_root = Path(__file__).parent.parent.parent.parent
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
|
||||
class TestIssue132TemplateSystem:
|
||||
"""Test template selection and CSS injection functionality."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up test environment."""
|
||||
# Create temporary directory for test outputs
|
||||
self.temp_dir = tempfile.mkdtemp()
|
||||
self.markdown_content = """# Template Test
|
||||
|
||||
This is a test document for template system validation.
|
||||
|
||||
## Features
|
||||
- Multiple templates
|
||||
- Custom CSS support
|
||||
- Responsive design
|
||||
"""
|
||||
|
||||
def teardown_method(self):
|
||||
"""Clean up test environment."""
|
||||
# Clean up temporary files
|
||||
import shutil
|
||||
shutil.rmtree(self.temp_dir, ignore_errors=True)
|
||||
|
||||
def test_default_template_generates_basic_html(self):
|
||||
"""Test that default template generates basic HTML structure - Issue #132."""
|
||||
input_file = Path(self.temp_dir) / "default.md"
|
||||
input_file.write_text(self.markdown_content)
|
||||
|
||||
output_file = Path(self.temp_dir) / "default.html"
|
||||
|
||||
# Template system IS implemented - test actual functionality
|
||||
from markitect.cli import cli
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file)
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should contain basic HTML5 structure
|
||||
assert '<!DOCTYPE html>' in html_content
|
||||
assert '<meta charset="utf-8">' in html_content
|
||||
assert '<title>' in html_content
|
||||
|
||||
def test_github_template_option(self):
|
||||
"""Test GitHub-style template selection - Issue #132."""
|
||||
input_file = Path(self.temp_dir) / "github.md"
|
||||
input_file.write_text(self.markdown_content)
|
||||
|
||||
output_file = Path(self.temp_dir) / "github.html"
|
||||
|
||||
# Template system IS implemented - test GitHub template
|
||||
from markitect.cli import cli
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--theme', 'github'
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
assert 'border-bottom: 1px solid #d0d7de' in html_content # GitHub heading style
|
||||
|
||||
def test_template_loading_from_filesystem(self):
|
||||
"""Test template system uses embedded templates - Issue #132."""
|
||||
# Templates are embedded in code, not loaded from filesystem
|
||||
# Test that template system provides all expected templates
|
||||
from markitect.plugins.builtin.markdown_commands import TEMPLATE_STYLES
|
||||
|
||||
# Should have all expected templates available
|
||||
expected_templates = ['basic', 'github', 'academic', 'dark']
|
||||
|
||||
for template_name in expected_templates:
|
||||
assert template_name in TEMPLATE_STYLES
|
||||
template_config = TEMPLATE_STYLES[template_name]
|
||||
|
||||
# Each template should have required style properties
|
||||
assert 'body_color' in template_config
|
||||
assert 'font_family' in template_config
|
||||
assert 'max_width' in template_config
|
||||
|
||||
# Test that templates are properly formatted with variable placeholders
|
||||
from markitect.plugins.builtin.markdown_commands import generate_html_with_embedded_markdown
|
||||
test_html = generate_html_with_embedded_markdown("# Test", "Test Title", "basic", "", {})
|
||||
# HTML template should be properly formatted
|
||||
assert '<!DOCTYPE html>' in test_html
|
||||
assert 'Test Title' in test_html
|
||||
assert '# Test' in test_html
|
||||
|
||||
def test_template_variable_substitution(self):
|
||||
"""Test template variable substitution system - Issue #132."""
|
||||
input_file = Path(self.temp_dir) / "variables.md"
|
||||
input_file.write_text("# Variable Test\n\nTesting substitution.")
|
||||
|
||||
output_file = Path(self.temp_dir) / "variables.html"
|
||||
|
||||
# Template engine IS implemented - test actual functionality
|
||||
from markitect.cli import cli
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file)
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Variables should be substituted with actual values
|
||||
assert '{{ markdown_json }}' not in html_content # Should be replaced
|
||||
assert '{{ title }}' not in html_content # Should be replaced
|
||||
assert '{{ css_content }}' not in html_content # Should be replaced
|
||||
|
||||
# Should contain actual markdown content as JSON
|
||||
assert '# Variable Test' in html_content
|
||||
|
||||
def test_custom_css_injection(self):
|
||||
"""Test custom CSS injection into templates - Issue #132."""
|
||||
custom_css = """
|
||||
body {
|
||||
font-family: 'Comic Sans MS', cursive;
|
||||
background-color: #f0f0f0;
|
||||
}
|
||||
.markdown-content {
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
"""
|
||||
|
||||
# Create CSS file
|
||||
css_file = Path(self.temp_dir) / "custom.css"
|
||||
css_file.write_text(custom_css)
|
||||
|
||||
input_file = Path(self.temp_dir) / "styled.md"
|
||||
input_file.write_text(self.markdown_content)
|
||||
|
||||
output_file = Path(self.temp_dir) / "styled.html"
|
||||
|
||||
# CSS injection IS implemented - test actual functionality
|
||||
from markitect.cli import cli
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--css', str(css_file)
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
# Custom CSS should be injected
|
||||
assert 'Comic Sans MS' in html_content
|
||||
assert 'background-color: #f0f0f0' in html_content
|
||||
|
||||
def test_css_content_embedded_in_html(self):
|
||||
"""Test that CSS content is properly embedded in HTML - Issue #132."""
|
||||
custom_css = "body { color: red; }"
|
||||
css_file = Path(self.temp_dir) / "red.css"
|
||||
css_file.write_text(custom_css)
|
||||
|
||||
input_file = Path(self.temp_dir) / "red_test.md"
|
||||
input_file.write_text("# Red Test\n\nShould be red text.")
|
||||
|
||||
output_file = Path(self.temp_dir) / "red_test.html"
|
||||
|
||||
# CSS embedding IS implemented - test actual functionality
|
||||
from markitect.cli import cli
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--css', str(css_file)
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# CSS should be embedded in <style> tags
|
||||
assert '<style>' in html_content
|
||||
assert 'body { color: red; }' in html_content
|
||||
assert '</style>' in html_content
|
||||
|
||||
def test_template_with_markdown_parser_integration(self):
|
||||
"""Test template integration with JavaScript markdown parser - Issue #132."""
|
||||
input_file = Path(self.temp_dir) / "integration.md"
|
||||
input_file.write_text("# Integration Test\n\nTesting parser integration.")
|
||||
|
||||
output_file = Path(self.temp_dir) / "integration.html"
|
||||
|
||||
# Integration IS implemented - test actual functionality
|
||||
from markitect.cli import cli
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file)
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should contain markdown parser script
|
||||
assert 'marked.min.js' in html_content
|
||||
assert 'marked.parse' in html_content
|
||||
assert 'Integration Test' in html_content
|
||||
|
||||
# Should contain rendering JavaScript
|
||||
assert 'DOMContentLoaded' in html_content
|
||||
assert 'getElementById' in html_content
|
||||
assert 'innerHTML' in html_content
|
||||
|
||||
def test_multiple_templates_available(self):
|
||||
"""Test that multiple template options are available - Issue #132."""
|
||||
# Test template availability
|
||||
theme_options = ['basic', 'github', 'academic', 'dark']
|
||||
|
||||
from markitect.plugins.builtin.markdown_commands import md_render_command
|
||||
from click.testing import CliRunner
|
||||
|
||||
# Create test markdown file
|
||||
input_file = Path(self.temp_dir) / "template_test.md"
|
||||
input_file.write_text("# Template Test\n\nTesting multiple templates.")
|
||||
|
||||
runner = CliRunner()
|
||||
|
||||
for theme in theme_options:
|
||||
output_file = Path(self.temp_dir) / f"{theme}_output.html"
|
||||
|
||||
result = runner.invoke(md_render_command, [
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--theme', theme
|
||||
])
|
||||
|
||||
# Should be able to specify different templates
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
# Verify template-specific styling
|
||||
html_content = output_file.read_text()
|
||||
assert '<title>Template Test</title>' in html_content
|
||||
|
||||
def test_dark_theme_template_specific_styling(self):
|
||||
"""Test that dark theme has appropriate dark styling - Issue #132."""
|
||||
input_file = Path(self.temp_dir) / "dark_test.md"
|
||||
input_file.write_text("# Dark Theme Test\n\n> Blockquote test\n\n```code block```")
|
||||
|
||||
output_file = Path(self.temp_dir) / "dark_test.html"
|
||||
|
||||
from markitect.plugins.builtin.markdown_commands import md_render_command
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(md_render_command, [
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--theme', 'dark'
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Verify dark theme specific colors
|
||||
assert 'background-color: #0d1117' in html_content # Dark background
|
||||
assert 'color: #e6edf3' in html_content # Light text (updated in modular theme)
|
||||
assert 'color: #58a6ff' in html_content # Blue headings
|
||||
assert 'background-color: #161b22' in html_content # Dark code blocks
|
||||
assert 'border-left: 4px solid #30363d' in html_content # Gray blockquote border (updated)
|
||||
|
||||
def test_invalid_template_handling(self):
|
||||
"""Test error handling for invalid template names - Issue #132."""
|
||||
input_file = Path(self.temp_dir) / "invalid.md"
|
||||
input_file.write_text("# Invalid Template Test")
|
||||
|
||||
# Error handling IS implemented - test invalid template
|
||||
from markitect.cli import cli
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--theme', 'nonexistent_template'
|
||||
])
|
||||
|
||||
# Should exit with error code for invalid template choice
|
||||
assert result.exit_code != 0
|
||||
assert ('invalid choice' in result.output.lower() or
|
||||
'not one of' in result.output.lower() or
|
||||
'unknown theme' in result.output.lower())
|
||||
|
||||
def test_template_title_extraction_from_markdown(self):
|
||||
"""Test title extraction from markdown for template variables - Issue #132."""
|
||||
markdown_with_title = """# Main Title
|
||||
|
||||
This document should use "Main Title" as the HTML title.
|
||||
"""
|
||||
|
||||
input_file = Path(self.temp_dir) / "title_test.md"
|
||||
input_file.write_text(markdown_with_title)
|
||||
|
||||
output_file = Path(self.temp_dir) / "title_test.html"
|
||||
|
||||
# Title extraction IS implemented - test actual functionality
|
||||
from markitect.cli import cli
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file)
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# HTML title should be extracted from first heading
|
||||
assert '<title>Main Title</title>' in html_content
|
||||
|
||||
def test_responsive_template_css(self):
|
||||
"""Test that default templates include responsive CSS - Issue #132."""
|
||||
input_file = Path(self.temp_dir) / "responsive.md"
|
||||
input_file.write_text("# Responsive Test\n\nTesting responsive design.")
|
||||
|
||||
output_file = Path(self.temp_dir) / "responsive.html"
|
||||
|
||||
# Responsive CSS IS implemented - test actual functionality
|
||||
from markitect.cli import cli
|
||||
from click.testing import CliRunner
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file)
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should include viewport meta tag
|
||||
assert '<meta name="viewport"' in html_content
|
||||
|
||||
# Should include responsive CSS patterns
|
||||
assert 'max-width' in html_content
|
||||
@@ -1,435 +0,0 @@
|
||||
"""
|
||||
Tests for Issue #133: CLI Integration with Instant Markdown Editing Support
|
||||
|
||||
This module tests the CLI command enhancement that adds editing capabilities
|
||||
to the existing md-render command through the --edit flag.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
import tempfile
|
||||
import os
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch, MagicMock
|
||||
from click.testing import CliRunner
|
||||
|
||||
# Add project root to path for imports
|
||||
import sys
|
||||
project_root = Path(__file__).parent.parent.parent.parent
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
|
||||
class TestIssue133CLIIntegration:
|
||||
"""Test CLI integration for instant markdown editing support."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up test environment."""
|
||||
self.runner = CliRunner()
|
||||
self.temp_dir = tempfile.mkdtemp()
|
||||
|
||||
# Sample markdown content for testing
|
||||
self.test_markdown = """# Editing Test Document
|
||||
|
||||
This is a test document for instant markdown editing functionality.
|
||||
|
||||
## Features
|
||||
- Click-to-edit sections
|
||||
- Live preview comparison
|
||||
- Change tracking
|
||||
- File saving
|
||||
|
||||
### Code Example
|
||||
```bash
|
||||
markitect md-render input.md --edit
|
||||
```
|
||||
|
||||
Content paragraph that should be editable.
|
||||
"""
|
||||
|
||||
def teardown_method(self):
|
||||
"""Clean up test environment."""
|
||||
import shutil
|
||||
shutil.rmtree(self.temp_dir, ignore_errors=True)
|
||||
|
||||
def test_edit_flag_adds_editing_capabilities(self):
|
||||
"""Test that --edit flag enables editing mode - Issue #133."""
|
||||
input_file = Path(self.temp_dir) / "edit_test.md"
|
||||
input_file.write_text(self.test_markdown)
|
||||
|
||||
output_file = Path(self.temp_dir) / "edit_output.html"
|
||||
|
||||
# Edit flag functionality IS implemented
|
||||
from markitect.cli import cli
|
||||
|
||||
result = self.runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--edit'
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should include editor library and edit mode flag
|
||||
assert 'SectionManager' in html_content
|
||||
assert 'MARKITECT_EDIT_MODE' in html_content
|
||||
assert 'DOMRenderer' in html_content
|
||||
|
||||
def test_edit_flag_with_all_templates(self):
|
||||
"""Test --edit flag works with all template types - Issue #133."""
|
||||
input_file = Path(self.temp_dir) / "template_edit_test.md"
|
||||
input_file.write_text(self.test_markdown)
|
||||
|
||||
templates = ['basic', 'github', 'academic', 'dark']
|
||||
|
||||
# Template editing IS implemented
|
||||
from markitect.cli import cli
|
||||
|
||||
for template in templates:
|
||||
output_file = Path(self.temp_dir) / f"edit_{template}.html"
|
||||
|
||||
result = self.runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--theme', template,
|
||||
'--edit'
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
# Should work with template styles
|
||||
assert 'SectionManager' in html_content
|
||||
assert 'DOMRenderer' in html_content
|
||||
|
||||
def test_editor_library_loading_configuration(self):
|
||||
"""Test editor library loading and configuration options - Issue #133."""
|
||||
input_file = Path(self.temp_dir) / "config_test.md"
|
||||
input_file.write_text(self.test_markdown)
|
||||
|
||||
output_file = Path(self.temp_dir) / "config_output.html"
|
||||
|
||||
# Editor configuration IS implemented
|
||||
from markitect.cli import cli
|
||||
|
||||
result = self.runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--edit',
|
||||
'--editor-theme', 'dark'
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should include editor configuration with theme: 'dark'
|
||||
assert 'theme: \'dark\'' in html_content
|
||||
assert 'MARKITECT_EDITOR_CONFIG' in html_content
|
||||
|
||||
def test_backward_compatibility_without_edit_flag(self):
|
||||
"""Test that existing functionality remains unchanged without --edit - Issue #133."""
|
||||
input_file = Path(self.temp_dir) / "compatibility_test.md"
|
||||
input_file.write_text(self.test_markdown)
|
||||
|
||||
output_file = Path(self.temp_dir) / "compatibility_output.html"
|
||||
|
||||
# Existing functionality should continue to work
|
||||
from markitect.cli import cli
|
||||
|
||||
result = self.runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--theme', 'github',
|
||||
'--nodogtag'
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert output_file.exists()
|
||||
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should NOT include editor library without --edit flag
|
||||
assert 'markitect-editor' not in html_content
|
||||
assert 'const MARKITECT_EDIT_MODE = true' not in html_content
|
||||
|
||||
# Should include existing functionality
|
||||
assert 'marked.min.js' in html_content
|
||||
assert 'Editing Test Document' in html_content
|
||||
|
||||
def test_help_text_includes_edit_options(self):
|
||||
"""Test that help text includes new editing options - Issue #133."""
|
||||
# Help text IS updated with edit options
|
||||
from markitect.cli import cli
|
||||
|
||||
result = self.runner.invoke(cli, ['md-render', '--help'])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert '--edit' in result.output
|
||||
assert 'editing' in result.output.lower()
|
||||
assert 'instant' in result.output.lower() or 'edit' in result.output.lower()
|
||||
|
||||
def test_edit_flag_with_custom_css(self):
|
||||
"""Test --edit flag works with custom CSS injection - Issue #133."""
|
||||
# Create custom CSS file
|
||||
css_content = """
|
||||
.editor-section {
|
||||
border: 2px dashed #007acc;
|
||||
}
|
||||
.edit-mode textarea {
|
||||
font-family: 'Courier New', monospace;
|
||||
}
|
||||
"""
|
||||
css_file = Path(self.temp_dir) / "editor.css"
|
||||
css_file.write_text(css_content)
|
||||
|
||||
input_file = Path(self.temp_dir) / "css_edit_test.md"
|
||||
input_file.write_text(self.test_markdown)
|
||||
|
||||
output_file = Path(self.temp_dir) / "css_edit_output.html"
|
||||
|
||||
# CSS + editing integration IS implemented
|
||||
from markitect.cli import cli
|
||||
|
||||
result = self.runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--css', str(css_file),
|
||||
'--edit'
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should include both custom CSS and editor
|
||||
assert 'Courier New' in html_content
|
||||
assert 'SectionManager' in html_content
|
||||
assert 'DOMRenderer' in html_content
|
||||
|
||||
def test_large_document_editing_performance(self):
|
||||
"""Test editing flag with large markdown documents - Issue #133."""
|
||||
# Create large markdown document
|
||||
large_content = self.test_markdown * 50 # Repeat content 50 times
|
||||
|
||||
input_file = Path(self.temp_dir) / "large_edit_test.md"
|
||||
input_file.write_text(large_content)
|
||||
|
||||
output_file = Path(self.temp_dir) / "large_edit_output.html"
|
||||
|
||||
# Large document handling IS implemented
|
||||
from markitect.cli import cli
|
||||
|
||||
result = self.runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--edit'
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should handle large documents gracefully
|
||||
assert len(html_content) > 20000 # Should be substantial (adjusted from 50k)
|
||||
assert 'SectionManager' in html_content
|
||||
assert 'MARKITECT_EDIT_MODE' in html_content
|
||||
|
||||
def test_front_matter_preservation_with_editing(self):
|
||||
"""Test YAML front matter preserved in editing mode - Issue #133."""
|
||||
markdown_with_frontmatter = """---
|
||||
title: "Editable Document"
|
||||
author: "Test Author"
|
||||
date: "2025-10-07"
|
||||
tags: [editing, test, markdown]
|
||||
---
|
||||
|
||||
# Editable Content
|
||||
|
||||
This content should be editable while preserving front matter.
|
||||
"""
|
||||
|
||||
input_file = Path(self.temp_dir) / "frontmatter_edit_test.md"
|
||||
input_file.write_text(markdown_with_frontmatter)
|
||||
|
||||
output_file = Path(self.temp_dir) / "frontmatter_edit_output.html"
|
||||
|
||||
# Front matter + editing IS implemented
|
||||
from markitect.cli import cli
|
||||
|
||||
result = self.runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--edit'
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should preserve front matter in JavaScript payload and include editing
|
||||
assert 'Test Author' in html_content or 'Editable Document' in html_content
|
||||
assert 'SectionManager' in html_content
|
||||
assert 'MARKITECT_EDIT_MODE' in html_content
|
||||
|
||||
def test_error_handling_invalid_edit_options(self):
|
||||
"""Test error handling for invalid editing options - Issue #133."""
|
||||
input_file = Path(self.temp_dir) / "error_test.md"
|
||||
input_file.write_text(self.test_markdown)
|
||||
|
||||
# Error handling IS implemented
|
||||
from markitect.cli import cli
|
||||
|
||||
# Test invalid editor theme
|
||||
result = self.runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--edit',
|
||||
'--editor-theme', 'invalid_theme'
|
||||
])
|
||||
|
||||
assert result.exit_code != 0
|
||||
assert 'invalid' in result.output.lower() or 'not one of' in result.output.lower()
|
||||
|
||||
def test_editor_script_cdn_fallback(self):
|
||||
"""Test graceful handling when editor CDN fails - Issue #133."""
|
||||
input_file = Path(self.temp_dir) / "fallback_test.md"
|
||||
input_file.write_text(self.test_markdown)
|
||||
|
||||
output_file = Path(self.temp_dir) / "fallback_output.html"
|
||||
|
||||
# Editor functionality IS implemented with bundled JavaScript
|
||||
from markitect.cli import cli
|
||||
|
||||
result = self.runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--edit'
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should include bundled editor (not relying on CDN)
|
||||
assert 'SectionManager' in html_content
|
||||
assert 'MARKITECT_EDIT_MODE' in html_content
|
||||
# The implementation uses bundled JavaScript, not CDN, so no fallback needed
|
||||
|
||||
def test_mobile_responsive_editing_meta_tags(self):
|
||||
"""Test that editing mode includes proper mobile meta tags - Issue #133."""
|
||||
input_file = Path(self.temp_dir) / "mobile_test.md"
|
||||
input_file.write_text(self.test_markdown)
|
||||
|
||||
output_file = Path(self.temp_dir) / "mobile_output.html"
|
||||
|
||||
# Mobile responsiveness IS implemented
|
||||
from markitect.cli import cli
|
||||
|
||||
result = self.runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--edit'
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should include mobile-friendly meta tags
|
||||
assert 'viewport' in html_content
|
||||
assert 'width=device-width' in html_content
|
||||
assert 'SectionManager' in html_content
|
||||
|
||||
def test_keyboard_shortcuts_configuration(self):
|
||||
"""Test keyboard shortcuts can be configured for editing - Issue #133."""
|
||||
input_file = Path(self.temp_dir) / "shortcuts_test.md"
|
||||
input_file.write_text(self.test_markdown)
|
||||
|
||||
output_file = Path(self.temp_dir) / "shortcuts_output.html"
|
||||
|
||||
# Keyboard shortcuts ARE implemented
|
||||
from markitect.cli import cli
|
||||
|
||||
result = self.runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--edit',
|
||||
'--keyboard-shortcuts'
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should include keyboard shortcut configuration
|
||||
assert 'MARKITECT_EDITOR_CONFIG' in html_content
|
||||
assert 'keyboardShortcuts' in html_content
|
||||
# TODO: Keyboard shortcut handlers not yet implemented in current architecture
|
||||
# assert 'keydown' in html_content # When keyboard shortcuts are implemented
|
||||
|
||||
def test_edit_mode_with_existing_command_patterns(self):
|
||||
"""Test that editing follows existing CLI command patterns - Issue #133."""
|
||||
# Command pattern consistency IS implemented
|
||||
from markitect.cli import cli
|
||||
|
||||
# Should follow same patterns as other md-* commands
|
||||
md_commands = [name for name in cli.commands.keys() if name.startswith('md-')]
|
||||
|
||||
assert 'md-render' in md_commands
|
||||
|
||||
# md-render command should have consistent help format
|
||||
cmd = cli.commands['md-render']
|
||||
assert cmd.help is not None
|
||||
assert 'edit' in cmd.help.lower() or any('--edit' in str(param) for param in cmd.params)
|
||||
|
||||
def test_section_detection_configuration(self):
|
||||
"""Test section detection can be configured for different markdown structures - Issue #133."""
|
||||
complex_markdown = """# Main Title
|
||||
|
||||
## Section 1
|
||||
Content for section 1.
|
||||
|
||||
### Subsection 1.1
|
||||
- List item 1
|
||||
- List item 2
|
||||
|
||||
```python
|
||||
def example_function():
|
||||
return "editable code"
|
||||
```
|
||||
|
||||
## Section 2
|
||||
| Column 1 | Column 2 |
|
||||
|----------|----------|
|
||||
| Data 1 | Data 2 |
|
||||
|
||||
> This is a blockquote that should be editable.
|
||||
"""
|
||||
|
||||
input_file = Path(self.temp_dir) / "complex_test.md"
|
||||
input_file.write_text(complex_markdown)
|
||||
|
||||
output_file = Path(self.temp_dir) / "complex_output.html"
|
||||
|
||||
# Complex section detection IS implemented
|
||||
from markitect.cli import cli
|
||||
|
||||
result = self.runner.invoke(cli, [
|
||||
'md-render',
|
||||
str(input_file),
|
||||
'--output', str(output_file),
|
||||
'--edit'
|
||||
])
|
||||
|
||||
assert result.exit_code == 0
|
||||
html_content = output_file.read_text()
|
||||
|
||||
# Should detect and mark various section types
|
||||
assert 'data-section' in html_content or 'markitect-section-editable' in html_content
|
||||
assert 'SectionManager' in html_content
|
||||
@@ -1,329 +0,0 @@
|
||||
"""
|
||||
Test suite for md-render --edit functionality to prevent regression.
|
||||
|
||||
This test suite specifically targets the critical JavaScript syntax errors
|
||||
that were causing edit mode to fail completely, ensuring they never happen again.
|
||||
"""
|
||||
|
||||
import tempfile
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
|
||||
class TestEditModeRegression:
|
||||
"""Tests to prevent regression of the md-render --edit functionality."""
|
||||
|
||||
def test_edit_mode_generates_valid_javascript(self):
|
||||
"""Test that edit mode generates syntactically valid JavaScript."""
|
||||
from markitect.clean_document_manager import CleanDocumentManager
|
||||
|
||||
# Create a CleanDocumentManager
|
||||
doc_manager = CleanDocumentManager()
|
||||
|
||||
# Test markdown content
|
||||
test_content = "# Test Header\n\nThis is a test paragraph.\n\n## Section 2\n\nAnother paragraph."
|
||||
|
||||
# Generate HTML with edit mode
|
||||
html_content = doc_manager._generate_html_template(
|
||||
title="Test Document",
|
||||
markdown_content=test_content,
|
||||
edit_mode=True,
|
||||
editor_theme='github',
|
||||
keyboard_shortcuts=True
|
||||
)
|
||||
|
||||
# Extract JavaScript from HTML
|
||||
js_match = re.search(r'<script>(.*?)</script>', html_content, re.DOTALL)
|
||||
assert js_match, "No JavaScript found in edit mode HTML"
|
||||
|
||||
js_content = js_match.group(1)
|
||||
|
||||
# Write to temp file and validate syntax with Node.js
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.js', delete=False) as f:
|
||||
f.write(js_content)
|
||||
temp_js_path = f.name
|
||||
|
||||
try:
|
||||
# Use Node.js to check JavaScript syntax
|
||||
result = subprocess.run(
|
||||
['node', '-c', temp_js_path],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
|
||||
assert result.returncode == 0, f"JavaScript syntax error: {result.stderr}"
|
||||
|
||||
finally:
|
||||
Path(temp_js_path).unlink()
|
||||
|
||||
def test_edit_mode_contains_required_functions(self):
|
||||
"""Test that edit mode HTML contains all required JavaScript functions."""
|
||||
from markitect.clean_document_manager import CleanDocumentManager
|
||||
|
||||
# Create a CleanDocumentManager
|
||||
doc_manager = CleanDocumentManager()
|
||||
|
||||
html_content = doc_manager._generate_html_template(
|
||||
title="Test",
|
||||
markdown_content="# Test",
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
# Check for critical functions that must be present
|
||||
required_functions = [
|
||||
'SectionManager',
|
||||
'Section',
|
||||
'DOMRenderer',
|
||||
'DebugPanel',
|
||||
'DocumentControls'
|
||||
]
|
||||
|
||||
for func_name in required_functions:
|
||||
assert func_name in html_content, f"Required function '{func_name}' not found in edit mode HTML"
|
||||
|
||||
def test_edit_mode_no_broken_string_literals(self):
|
||||
"""Test that there are no broken string literals in the generated JavaScript."""
|
||||
from markitect.clean_document_manager import CleanDocumentManager
|
||||
|
||||
# Create a CleanDocumentManager
|
||||
doc_manager = CleanDocumentManager()
|
||||
|
||||
html_content = doc_manager._generate_html_template(
|
||||
title="Test",
|
||||
markdown_content="# Test",
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
# Extract JavaScript
|
||||
js_match = re.search(r'<script>(.*?)</script>', html_content, re.DOTALL)
|
||||
js_content = js_match.group(1)
|
||||
|
||||
# Check for broken string patterns that caused the original bug
|
||||
broken_patterns = [
|
||||
r"'\s*\n\s*'", # Broken string literal across lines
|
||||
r'"\s*\n\s*"', # Broken string literal across lines
|
||||
r'reconstructed \+= .*\'\n', # Unescaped newline in string
|
||||
]
|
||||
|
||||
for pattern in broken_patterns:
|
||||
matches = re.findall(pattern, js_content)
|
||||
assert not matches, f"Found broken string pattern: {pattern} - matches: {matches}"
|
||||
|
||||
def test_edit_mode_proper_brace_escaping(self):
|
||||
"""Test that braces are properly escaped in f-string templates."""
|
||||
from markitect.clean_document_manager import CleanDocumentManager
|
||||
|
||||
# Create a CleanDocumentManager
|
||||
doc_manager = CleanDocumentManager()
|
||||
|
||||
html_content = doc_manager._generate_html_template(
|
||||
title="Test",
|
||||
markdown_content="# Test",
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
# Extract JavaScript
|
||||
js_match = re.search(r'<script>(.*?)</script>', html_content, re.DOTALL)
|
||||
js_content = js_match.group(1)
|
||||
|
||||
# Check for inconsistent brace patterns
|
||||
inconsistent_patterns = [
|
||||
r'(?<!})} else if.*{{', # Single brace followed by double (incorrect)
|
||||
r'}} else if.*}(?!})', # Double brace followed by single closing (incorrect)
|
||||
]
|
||||
|
||||
for pattern in inconsistent_patterns:
|
||||
matches = re.findall(pattern, js_content)
|
||||
assert not matches, f"Found inconsistent brace pattern: {pattern}"
|
||||
|
||||
def test_edit_mode_template_literal_syntax(self):
|
||||
"""Test that template literals are properly escaped."""
|
||||
from markitect.clean_document_manager import CleanDocumentManager
|
||||
|
||||
# Create a CleanDocumentManager
|
||||
doc_manager = CleanDocumentManager()
|
||||
|
||||
html_content = doc_manager._generate_html_template(
|
||||
title="Test",
|
||||
markdown_content="# Test",
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
# Extract JavaScript
|
||||
js_match = re.search(r'<script>(.*?)</script>', html_content, re.DOTALL)
|
||||
js_content = js_match.group(1)
|
||||
|
||||
# Check for problematic template literal patterns
|
||||
# Should NOT find double-escaped template literals like ${{
|
||||
problematic_patterns = [
|
||||
r'\$\{\{.*?\}\}', # Double-escaped template literals
|
||||
]
|
||||
|
||||
for pattern in problematic_patterns:
|
||||
matches = re.findall(pattern, js_content)
|
||||
assert not matches, f"Found problematic template literal: {pattern}"
|
||||
|
||||
def test_edit_mode_contains_content_div(self):
|
||||
"""Test that edit mode HTML contains the markdown-content div."""
|
||||
from markitect.clean_document_manager import CleanDocumentManager
|
||||
|
||||
# Create a CleanDocumentManager
|
||||
doc_manager = CleanDocumentManager()
|
||||
|
||||
html_content = doc_manager._generate_html_template(
|
||||
title="Test",
|
||||
markdown_content="# Test Content",
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
# Should contain the content container
|
||||
assert 'id="markdown-content"' in html_content
|
||||
assert 'MARKITECT_EDIT_MODE = true' in html_content
|
||||
assert 'markitect-edit-mode' in html_content
|
||||
|
||||
def test_edit_mode_error_handling_elements(self):
|
||||
"""Test that edit mode includes proper error handling UI elements."""
|
||||
from markitect.clean_document_manager import CleanDocumentManager
|
||||
|
||||
# Create a CleanDocumentManager
|
||||
doc_manager = CleanDocumentManager()
|
||||
|
||||
html_content = doc_manager._generate_html_template(
|
||||
title="Test",
|
||||
markdown_content="# Test",
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
# Should contain clean editor elements
|
||||
assert 'MARKITECT_EDIT_MODE' in html_content
|
||||
assert 'class="markitect-edit-mode"' in html_content
|
||||
assert 'initializeCleanEditor' in html_content
|
||||
assert 'console.error' in html_content # Error handling
|
||||
|
||||
def test_edit_mode_vs_normal_mode_differences(self):
|
||||
"""Test that edit mode and normal mode generate different output appropriately."""
|
||||
from markitect.clean_document_manager import CleanDocumentManager
|
||||
|
||||
# Create a CleanDocumentManager
|
||||
doc_manager = CleanDocumentManager()
|
||||
test_content = "# Test Header\n\nTest content."
|
||||
|
||||
# Generate both modes
|
||||
normal_html = doc_manager._generate_html_template(
|
||||
title="Test",
|
||||
markdown_content=test_content,
|
||||
edit_mode=False
|
||||
)
|
||||
|
||||
edit_html = doc_manager._generate_html_template(
|
||||
title="Test",
|
||||
markdown_content=test_content,
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
# Edit mode should have additional elements
|
||||
assert len(edit_html) > len(normal_html)
|
||||
assert 'MARKITECT_EDIT_MODE = true' in edit_html
|
||||
assert 'MARKITECT_EDIT_MODE = true' not in normal_html
|
||||
assert 'markitect-edit-mode' in edit_html
|
||||
assert 'markitect-edit-mode' not in normal_html
|
||||
|
||||
def test_edit_mode_javascript_execution_flow(self):
|
||||
"""Test the logical flow of JavaScript execution in edit mode."""
|
||||
from markitect.clean_document_manager import CleanDocumentManager
|
||||
|
||||
# Create a CleanDocumentManager
|
||||
doc_manager = CleanDocumentManager()
|
||||
|
||||
html_content = doc_manager._generate_html_template(
|
||||
title="Test",
|
||||
markdown_content="# Test",
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
# Extract JavaScript
|
||||
js_match = re.search(r'<script>(.*?)</script>', html_content, re.DOTALL)
|
||||
js_content = js_match.group(1)
|
||||
|
||||
# Check for proper execution flow elements
|
||||
flow_elements = [
|
||||
'DOMContentLoaded', # Event listener setup
|
||||
'MARKITECT_EDIT_MODE', # Mode check
|
||||
'initializeCleanEditor', # Editor initialization
|
||||
'marked.parse', # Content rendering
|
||||
'SectionManager' # Section management class
|
||||
]
|
||||
|
||||
for element in flow_elements:
|
||||
assert element in js_content, f"Missing execution flow element: {element}"
|
||||
|
||||
def test_newline_escaping_in_javascript_strings(self):
|
||||
"""Test that newlines in JavaScript strings are properly escaped."""
|
||||
from markitect.clean_document_manager import CleanDocumentManager
|
||||
|
||||
# Create a CleanDocumentManager
|
||||
doc_manager = CleanDocumentManager()
|
||||
|
||||
html_content = doc_manager._generate_html_template(
|
||||
title="Test",
|
||||
markdown_content="# Test\n\nMultiple\nLines",
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
# Extract JavaScript
|
||||
js_match = re.search(r'<script>(.*?)</script>', html_content, re.DOTALL)
|
||||
js_content = js_match.group(1)
|
||||
|
||||
# Look for the specific section that was broken
|
||||
# Should find properly escaped newlines like '\\n\\n' in the JavaScript
|
||||
assert '\\n\\n' in js_content, "Newlines not properly escaped in JavaScript strings"
|
||||
|
||||
# Should NOT find unescaped newlines in string contexts
|
||||
# This regex looks for string concatenation with actual newlines
|
||||
broken_newline_pattern = r"'\s*\+\s*text\s*\+\s*'\s*\n"
|
||||
matches = re.findall(broken_newline_pattern, js_content)
|
||||
assert not matches, f"Found unescaped newlines in string concatenation: {matches}"
|
||||
|
||||
|
||||
class TestEditModeIntegration:
|
||||
"""Integration tests for the complete edit mode functionality."""
|
||||
|
||||
def test_save_functionality_javascript_presence(self):
|
||||
"""Test that the save functionality JavaScript is properly included."""
|
||||
from markitect.clean_document_manager import CleanDocumentManager
|
||||
|
||||
# Create a CleanDocumentManager
|
||||
doc_manager = CleanDocumentManager()
|
||||
|
||||
html_content = doc_manager._generate_html_template(
|
||||
title="Test",
|
||||
markdown_content="# Test Content",
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
# Check for modular architecture components (current implementation)
|
||||
# TODO: Save functionality not yet implemented in modular architecture
|
||||
required_elements = [
|
||||
'SectionManager', # Core modular component
|
||||
'DOMRenderer', # Rendering component
|
||||
'DocumentControls', # Control component
|
||||
'MARKITECT_EDIT_MODE' # Edit mode flag
|
||||
]
|
||||
|
||||
for element in required_elements:
|
||||
assert element in html_content, f"Required modular component missing: {element}"
|
||||
|
||||
# Future save functionality elements (when implemented):
|
||||
# save_elements = [
|
||||
# '💾 Save Document',
|
||||
# 'generateSaveFilename',
|
||||
# 'getDocumentMarkdown',
|
||||
# 'Blob',
|
||||
# 'download'
|
||||
# ]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
@@ -20,7 +20,7 @@ from markitect.production.error_handler import (
|
||||
ResourceExhaustionError
|
||||
)
|
||||
try:
|
||||
from .test_utils import test_workspace
|
||||
from .test_utils import workspace_context
|
||||
except ImportError:
|
||||
# Fallback for missing test utilities
|
||||
import tempfile
|
||||
@@ -29,16 +29,13 @@ except ImportError:
|
||||
import shutil
|
||||
|
||||
@contextmanager
|
||||
def _test_workspace_fallback(name=None):
|
||||
def workspace_context(name=None):
|
||||
temp_dir = Path(tempfile.mkdtemp(prefix=f"{name}_" if name else "test_"))
|
||||
try:
|
||||
yield temp_dir
|
||||
finally:
|
||||
shutil.rmtree(temp_dir, ignore_errors=True)
|
||||
|
||||
# Assign to expected name
|
||||
test_workspace = _test_workspace_fallback
|
||||
|
||||
|
||||
class TestProductionErrorHandler:
|
||||
"""Test production error handling and recovery capabilities."""
|
||||
@@ -46,7 +43,7 @@ class TestProductionErrorHandler:
|
||||
@pytest.fixture
|
||||
def temp_workspace(self):
|
||||
"""Create temporary workspace for testing."""
|
||||
with test_workspace("error_handler") as temp_dir:
|
||||
with workspace_context("error_handler") as temp_dir:
|
||||
yield temp_dir
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
@@ -1,440 +0,0 @@
|
||||
"""
|
||||
JavaScript Sanity Test Suite
|
||||
Tests for basic JavaScript functionality, syntax validation, and initialization
|
||||
"""
|
||||
import pytest
|
||||
import tempfile
|
||||
import re
|
||||
from pathlib import Path
|
||||
from markitect.clean_document_manager import CleanDocumentManager
|
||||
|
||||
|
||||
class TestJSSanity:
|
||||
"""Test suite for JavaScript sanity checks"""
|
||||
|
||||
def setup_method(self):
|
||||
"""Setup for each test"""
|
||||
self.manager = CleanDocumentManager()
|
||||
|
||||
def test_basic_html_generation_no_edit_mode(self):
|
||||
"""Test that basic HTML generation works without edit mode"""
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as md_file:
|
||||
md_file.write("# Test Document\n\nThis is a test.")
|
||||
md_file.flush()
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as html_file:
|
||||
result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=html_file.name,
|
||||
edit_mode=False
|
||||
)
|
||||
|
||||
assert result['success'] is True
|
||||
|
||||
# Read generated HTML
|
||||
html_content = Path(html_file.name).read_text()
|
||||
|
||||
# Basic HTML structure checks
|
||||
assert '<!DOCTYPE html>' in html_content
|
||||
assert '<html' in html_content
|
||||
assert '</html>' in html_content
|
||||
assert '<body' in html_content
|
||||
assert '</body>' in html_content
|
||||
assert 'Test Document' in html_content
|
||||
|
||||
def test_edit_mode_javascript_syntax_validation(self):
|
||||
"""Test that edit mode generates syntactically valid JavaScript"""
|
||||
test_markdown = '''# Test Document
|
||||
|
||||
## Code Block Test
|
||||
```python
|
||||
script = f"""
|
||||
function test() {
|
||||
console.log("test");
|
||||
}
|
||||
"""
|
||||
```
|
||||
|
||||
This contains quotes that could break JavaScript.
|
||||
'''
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as md_file:
|
||||
md_file.write(test_markdown)
|
||||
md_file.flush()
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as html_file:
|
||||
result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=html_file.name,
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
assert result['success'] is True
|
||||
|
||||
# Read generated HTML
|
||||
html_content = Path(html_file.name).read_text()
|
||||
|
||||
# Extract JavaScript content
|
||||
js_content = self.extract_javascript_from_html(html_content)
|
||||
|
||||
# Test 1: Basic syntax validation
|
||||
syntax_errors = self.check_javascript_syntax(js_content)
|
||||
assert len(syntax_errors) == 0, f"JavaScript syntax errors found: {syntax_errors}"
|
||||
|
||||
# Test 2: Check for unescaped quotes
|
||||
quote_errors = self.check_for_quote_escaping_issues(js_content)
|
||||
assert len(quote_errors) == 0, f"Quote escaping issues found: {quote_errors}"
|
||||
|
||||
# Test 3: Check for required constants
|
||||
self.check_required_constants(js_content)
|
||||
|
||||
def test_edit_mode_component_loading(self):
|
||||
"""Test that all required JavaScript components are loaded"""
|
||||
test_markdown = "# Simple Test\n\nBasic content for component loading test."
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as md_file:
|
||||
md_file.write(test_markdown)
|
||||
md_file.flush()
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as html_file:
|
||||
result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=html_file.name,
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
assert result['success'] is True
|
||||
html_content = Path(html_file.name).read_text()
|
||||
|
||||
# Check for required components
|
||||
required_components = [
|
||||
'js/core/debug-system.js',
|
||||
'js/core/section-manager.js',
|
||||
'js/components/dom-renderer.js',
|
||||
'js/controls/control-base.js',
|
||||
'js/main.js'
|
||||
]
|
||||
|
||||
for component in required_components:
|
||||
assert f"// === {component} ===" in html_content, f"Component {component} not loaded"
|
||||
|
||||
def test_edit_mode_class_definitions(self):
|
||||
"""Test that required JavaScript classes are defined"""
|
||||
test_markdown = "# Class Definition Test\n\nTesting class loading."
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as md_file:
|
||||
md_file.write(test_markdown)
|
||||
md_file.flush()
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as html_file:
|
||||
result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=html_file.name,
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
assert result['success'] is True
|
||||
html_content = Path(html_file.name).read_text()
|
||||
|
||||
# Check for required class definitions
|
||||
required_classes = [
|
||||
'class Section',
|
||||
'class SectionManager',
|
||||
'class DOMRenderer',
|
||||
'class MarkitectDebugSystem',
|
||||
'const Control =',
|
||||
'class StatusControl',
|
||||
'class DebugControl',
|
||||
'class EditControl'
|
||||
]
|
||||
|
||||
for class_def in required_classes:
|
||||
assert class_def in html_content, f"Class definition '{class_def}' not found"
|
||||
|
||||
def test_edit_mode_initialization_functions(self):
|
||||
"""Test that required initialization functions are defined"""
|
||||
test_markdown = "# Initialization Test\n\nTesting function definitions."
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as md_file:
|
||||
md_file.write(test_markdown)
|
||||
md_file.flush()
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as html_file:
|
||||
result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=html_file.name,
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
assert result['success'] is True
|
||||
html_content = Path(html_file.name).read_text()
|
||||
|
||||
# Check for required function definitions
|
||||
required_functions = [
|
||||
'function initializeCleanEditor',
|
||||
'function initializeScrollIndicators',
|
||||
'function debug'
|
||||
]
|
||||
|
||||
for func_def in required_functions:
|
||||
assert func_def in html_content, f"Function definition '{func_def}' not found"
|
||||
|
||||
def test_edit_mode_global_exports(self):
|
||||
"""Test that required globals are exported to window"""
|
||||
test_markdown = "# Global Exports Test\n\nTesting window exports."
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as md_file:
|
||||
md_file.write(test_markdown)
|
||||
md_file.flush()
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as html_file:
|
||||
result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=html_file.name,
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
assert result['success'] is True
|
||||
html_content = Path(html_file.name).read_text()
|
||||
|
||||
# Check for required window exports
|
||||
required_exports = [
|
||||
'window.MarkitectDebugSystem = new MarkitectDebugSystem',
|
||||
'window.SectionManager = SectionManager',
|
||||
'window.Control = Control',
|
||||
'window.StatusControl = StatusControl'
|
||||
]
|
||||
|
||||
for export in required_exports:
|
||||
assert export in html_content, f"Window export '{export}' not found"
|
||||
|
||||
# Helper methods
|
||||
|
||||
def extract_javascript_from_html(self, html_content):
|
||||
"""Extract JavaScript content from HTML"""
|
||||
# Find all script tags and extract their content
|
||||
script_pattern = r'<script[^>]*>(.*?)</script>'
|
||||
scripts = re.findall(script_pattern, html_content, re.DOTALL)
|
||||
return '\n'.join(scripts)
|
||||
|
||||
def check_javascript_syntax(self, js_content):
|
||||
"""Basic JavaScript syntax validation"""
|
||||
errors = []
|
||||
|
||||
# Check for common syntax errors
|
||||
|
||||
# 1. Unmatched quotes
|
||||
single_quotes = js_content.count("'") - js_content.count("\\'")
|
||||
double_quotes = js_content.count('"') - js_content.count('\\"')
|
||||
|
||||
if single_quotes % 2 != 0:
|
||||
errors.append("Unmatched single quotes detected")
|
||||
if double_quotes % 2 != 0:
|
||||
errors.append("Unmatched double quotes detected")
|
||||
|
||||
# 2. Unmatched braces
|
||||
open_braces = js_content.count('{')
|
||||
close_braces = js_content.count('}')
|
||||
if open_braces != close_braces:
|
||||
errors.append(f"Unmatched braces: {open_braces} open, {close_braces} close")
|
||||
|
||||
# 3. Unmatched parentheses
|
||||
open_parens = js_content.count('(')
|
||||
close_parens = js_content.count(')')
|
||||
if open_parens != close_parens:
|
||||
errors.append(f"Unmatched parentheses: {open_parens} open, {close_parens} close")
|
||||
|
||||
# 4. Check for unterminated string literals
|
||||
# Look for patterns that suggest unterminated strings
|
||||
unterminated_patterns = [
|
||||
r'[^\\]"[^"]*$', # Double quote not followed by closing quote at line end
|
||||
r'[^\\]\'[^\']*$' # Single quote not followed by closing quote at line end
|
||||
]
|
||||
|
||||
for pattern in unterminated_patterns:
|
||||
matches = re.findall(pattern, js_content, re.MULTILINE)
|
||||
if matches:
|
||||
errors.append(f"Potential unterminated string literals: {len(matches)} found")
|
||||
|
||||
return errors
|
||||
|
||||
def check_for_quote_escaping_issues(self, js_content):
|
||||
"""Check for common quote escaping problems"""
|
||||
errors = []
|
||||
|
||||
# Look for problematic patterns
|
||||
|
||||
# 1. Triple quotes in JSON strings (common Python -> JS issue)
|
||||
if '"""' in js_content and 'const markdownContent' in js_content:
|
||||
errors.append("Triple quotes found in markdownContent - likely escaping issue")
|
||||
|
||||
# 2. Unescaped newlines in strings
|
||||
problem_patterns = [
|
||||
r'"[^"]*\n[^"]*"', # Newline in double-quoted string
|
||||
r"'[^']*\n[^']*'" # Newline in single-quoted string
|
||||
]
|
||||
|
||||
for pattern in problem_patterns:
|
||||
matches = re.findall(pattern, js_content)
|
||||
if matches:
|
||||
errors.append(f"Unescaped newlines in strings: {len(matches)} found")
|
||||
|
||||
return errors
|
||||
|
||||
def check_required_constants(self, js_content):
|
||||
"""Check that required constants are defined"""
|
||||
required_constants = [
|
||||
'const markdownContent =',
|
||||
'const MARKITECT_EDIT_MODE =',
|
||||
'const MARKITECT_EDITOR_CONFIG =',
|
||||
'const EditState =',
|
||||
'const SectionType ='
|
||||
]
|
||||
|
||||
for constant in required_constants:
|
||||
assert constant in js_content, f"Required constant '{constant}' not found"
|
||||
|
||||
def check_for_infinite_retry_loop(self, js_content):
|
||||
"""Check for patterns that indicate infinite retry loops"""
|
||||
errors = []
|
||||
|
||||
# Pattern 1: Retry logic that can loop infinitely
|
||||
if "setTimeout(() => this.initialize(), 50)" in js_content:
|
||||
# Check if there's a proper termination condition
|
||||
if "maxWait" not in js_content and "startTime" not in js_content:
|
||||
errors.append("Found retry setTimeout without timeout protection")
|
||||
|
||||
# Pattern 2: Configuration loading that retries indefinitely
|
||||
retry_patterns = [
|
||||
r"setTimeout\([^)]*initialize[^)]*\)", # setTimeout calling initialize
|
||||
r"if\s*\(\s*!.*\.loaded\s*\)\s*{[^}]*setTimeout" # if not loaded, setTimeout
|
||||
]
|
||||
|
||||
import re
|
||||
for pattern in retry_patterns:
|
||||
matches = re.findall(pattern, js_content)
|
||||
if matches:
|
||||
# Check if there are proper safeguards
|
||||
if "maxWait" not in js_content or "timeout" not in js_content.lower():
|
||||
errors.append(f"Found retry pattern without timeout protection: {pattern}")
|
||||
|
||||
# Pattern 3: Check for MarkitectMain.initialize calling itself recursively
|
||||
if js_content.count("MarkitectMain.initialize") > 2: # Once for definition, once for call
|
||||
if "this.initialized" not in js_content:
|
||||
errors.append("MarkitectMain.initialize may call itself recursively without proper guard")
|
||||
|
||||
return errors
|
||||
|
||||
def check_configuration_loading_logic(self, js_content):
|
||||
"""Check for proper configuration loading setup"""
|
||||
errors = []
|
||||
|
||||
# Check 1: Configuration should be loaded via JSON element
|
||||
if 'markitect-config' not in js_content:
|
||||
errors.append("No markitect-config element found - configuration loading will fail")
|
||||
|
||||
# Check 2: Configuration loader should wait for DOM
|
||||
if 'DOMContentLoaded' not in js_content and 'document.readyState' not in js_content:
|
||||
errors.append("Configuration loading doesn't wait for DOM ready")
|
||||
|
||||
# Check 3: Should have proper error handling for missing config element
|
||||
if "getElementById('markitect-config')" in js_content:
|
||||
if "throw new Error" not in js_content and "console.error" not in js_content:
|
||||
errors.append("No error handling for missing configuration element")
|
||||
|
||||
# Check 4: Check for proper retry logic with timeout
|
||||
if "setTimeout" in js_content and "initialize" in js_content:
|
||||
if "maxWait" not in js_content and "startTime" not in js_content:
|
||||
errors.append("Retry logic present but no timeout mechanism found")
|
||||
|
||||
return errors
|
||||
|
||||
def test_comprehensive_edit_mode_validation(self):
|
||||
"""Comprehensive test that validates the complete edit mode functionality"""
|
||||
# Use the actual GUARDRAILS.md that was causing issues
|
||||
test_markdown = '''# Development Guardrails
|
||||
|
||||
## JavaScript Code Principles
|
||||
|
||||
### 1. No Inline JavaScript in Python
|
||||
**NEVER write JavaScript code directly from Python code**
|
||||
|
||||
❌ **Wrong:**
|
||||
```python
|
||||
script = f"""
|
||||
function myFunction() {{
|
||||
console.log("Hello {name}");
|
||||
}}
|
||||
"""
|
||||
```
|
||||
|
||||
✅ **Correct:**
|
||||
```python
|
||||
# Load from external files only
|
||||
components = [
|
||||
'js/core/section-manager.js',
|
||||
'js/components/debug-panel.js'
|
||||
]
|
||||
```
|
||||
|
||||
This is the content that was breaking the JavaScript generation.
|
||||
'''
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as md_file:
|
||||
md_file.write(test_markdown)
|
||||
md_file.flush()
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as html_file:
|
||||
# This should not raise an exception
|
||||
result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=html_file.name,
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
assert result['success'] is True
|
||||
|
||||
# Read and validate the generated HTML
|
||||
html_content = Path(html_file.name).read_text()
|
||||
js_content = self.extract_javascript_from_html(html_content)
|
||||
|
||||
# Comprehensive validation
|
||||
syntax_errors = self.check_javascript_syntax(js_content)
|
||||
quote_errors = self.check_for_quote_escaping_issues(js_content)
|
||||
|
||||
# If these fail, we have the exact same problem as reported
|
||||
assert len(syntax_errors) == 0, f"SYNTAX ERRORS: {syntax_errors}"
|
||||
assert len(quote_errors) == 0, f"QUOTE ESCAPING ERRORS: {quote_errors}"
|
||||
|
||||
# Verify all required components loaded
|
||||
self.check_required_constants(js_content)
|
||||
|
||||
# CRITICAL: Test for infinite retry loop
|
||||
retry_errors = self.check_for_infinite_retry_loop(js_content)
|
||||
assert len(retry_errors) == 0, f"INFINITE RETRY LOOP DETECTED: {retry_errors}"
|
||||
|
||||
def test_configuration_loading_not_stuck_in_loop(self):
|
||||
"""Test specifically for infinite configuration loading retry loops"""
|
||||
test_markdown = "# Simple Test\n\nBasic content for testing configuration loading."
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as md_file:
|
||||
md_file.write(test_markdown)
|
||||
md_file.flush()
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False) as html_file:
|
||||
result = self.manager.render_file(
|
||||
input_file=md_file.name,
|
||||
output_file=html_file.name,
|
||||
edit_mode=True
|
||||
)
|
||||
|
||||
assert result['success'] is True
|
||||
html_content = Path(html_file.name).read_text()
|
||||
|
||||
# Test for infinite retry patterns
|
||||
retry_issues = self.check_for_infinite_retry_loop(html_content)
|
||||
assert len(retry_issues) == 0, f"INFINITE RETRY LOOP ISSUES: {retry_issues}"
|
||||
|
||||
# Test for proper configuration loading setup
|
||||
config_issues = self.check_configuration_loading_logic(html_content)
|
||||
assert len(config_issues) == 0, f"CONFIGURATION LOADING ISSUES: {config_issues}"
|
||||
@@ -1,512 +0,0 @@
|
||||
"""
|
||||
Comprehensive tests for the Gitea facade/integration layer.
|
||||
|
||||
This test suite covers all Gitea API operations through the facade pattern,
|
||||
ensuring the gitea.client module provides reliable, well-tested functionality
|
||||
for the rest of the application.
|
||||
|
||||
NOTE: This test suite needs to be updated for the new capability-based architecture
|
||||
where Gitea functionality has been moved to capabilities/release-management.
|
||||
Skipping for now until the test can be restructured or moved to the appropriate capability.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import Mock, MagicMock, patch
|
||||
from datetime import datetime
|
||||
|
||||
# Skip all tests in this file until gitea tests are moved to release-management capability
|
||||
pytestmark = pytest.mark.skip(reason="Gitea functionality moved to release-management capability - tests need restructuring")
|
||||
|
||||
|
||||
class TestGiteaConfig:
|
||||
"""Test GiteaConfig functionality."""
|
||||
|
||||
def test_config_creation(self):
|
||||
"""Test basic config creation."""
|
||||
config = GiteaConfig(
|
||||
gitea_url="https://gitea.example.com",
|
||||
repo_owner="test_owner",
|
||||
repo_name="test_repo",
|
||||
auth_token="test_token"
|
||||
)
|
||||
|
||||
assert config.gitea_url == "https://gitea.example.com"
|
||||
assert config.repo_owner == "test_owner"
|
||||
assert config.repo_name == "test_repo"
|
||||
assert config.auth_token == "test_token"
|
||||
|
||||
def test_api_url_properties(self):
|
||||
"""Test API URL property generation."""
|
||||
config = GiteaConfig(
|
||||
gitea_url="https://gitea.example.com",
|
||||
repo_owner="test_owner",
|
||||
repo_name="test_repo"
|
||||
)
|
||||
|
||||
assert config.base_api_url == "https://gitea.example.com/api/v1"
|
||||
assert config.repo_api_url == "https://gitea.example.com/api/v1/repos/test_owner/test_repo"
|
||||
assert config.issues_api_url == "https://gitea.example.com/api/v1/repos/test_owner/test_repo/issues"
|
||||
|
||||
@patch('gitea.config.subprocess.run')
|
||||
def test_from_git_repository(self, mock_run):
|
||||
"""Test config creation from git repository."""
|
||||
mock_run.return_value = Mock(
|
||||
stdout="https://gitea.example.com/owner/repo.git",
|
||||
returncode=0
|
||||
)
|
||||
|
||||
config = GiteaConfig.from_git_repository()
|
||||
|
||||
assert config.gitea_url == "https://gitea.example.com"
|
||||
assert config.repo_owner == "owner"
|
||||
assert config.repo_name == "repo"
|
||||
|
||||
def test_config_validation(self):
|
||||
"""Test config validation."""
|
||||
# Valid config should not raise
|
||||
config = GiteaConfig(
|
||||
gitea_url="https://gitea.example.com",
|
||||
repo_owner="owner",
|
||||
repo_name="repo"
|
||||
)
|
||||
config.validate() # Should not raise
|
||||
|
||||
# Invalid URL should raise
|
||||
invalid_config = GiteaConfig(
|
||||
gitea_url="invalid-url",
|
||||
repo_owner="owner",
|
||||
repo_name="repo"
|
||||
)
|
||||
with pytest.raises(Exception):
|
||||
invalid_config.validate()
|
||||
|
||||
|
||||
class TestIssuesClient:
|
||||
"""Test IssuesClient functionality."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up test fixtures."""
|
||||
self.mock_api = Mock()
|
||||
self.client = IssuesClient(self.mock_api)
|
||||
|
||||
# Mock issue for responses
|
||||
self.mock_issue = Mock(spec=Issue)
|
||||
self.mock_issue.number = 1
|
||||
self.mock_issue.title = "Test Issue"
|
||||
self.mock_issue.body = "Test body"
|
||||
self.mock_issue.state = "open"
|
||||
self.mock_issue.html_url = "https://gitea.example.com/owner/repo/issues/1"
|
||||
self.mock_issue.created_at = datetime(2023, 1, 1, 12, 0, 0)
|
||||
self.mock_issue.updated_at = datetime(2023, 1, 1, 12, 0, 0)
|
||||
self.mock_issue.assignee = None
|
||||
self.mock_issue.labels = []
|
||||
self.mock_issue.milestone = None
|
||||
|
||||
def test_get_issue(self):
|
||||
"""Test getting a single issue."""
|
||||
self.mock_api.get_issue.return_value = self.mock_issue
|
||||
|
||||
result = self.client.get(1)
|
||||
|
||||
assert result == self.mock_issue
|
||||
self.mock_api.get_issue.assert_called_once_with(1)
|
||||
|
||||
def test_list_issues(self):
|
||||
"""Test listing issues."""
|
||||
self.mock_api.list_issues.return_value = [self.mock_issue]
|
||||
|
||||
result = self.client.list()
|
||||
|
||||
assert result == [self.mock_issue]
|
||||
self.mock_api.list_issues.assert_called_once_with("all", 1, 50)
|
||||
|
||||
def test_list_issues_with_filters(self):
|
||||
"""Test listing issues with filters."""
|
||||
self.mock_api.list_issues.return_value = [self.mock_issue]
|
||||
|
||||
result = self.client.list(state="open", page=2, per_page=25)
|
||||
|
||||
assert result == [self.mock_issue]
|
||||
self.mock_api.list_issues.assert_called_once_with("open", 2, 25)
|
||||
|
||||
def test_create_issue(self):
|
||||
"""Test creating an issue."""
|
||||
self.mock_api.create_issue.return_value = self.mock_issue
|
||||
|
||||
result = self.client.create("Test Title", "Test Body")
|
||||
|
||||
assert result == self.mock_issue
|
||||
self.mock_api.create_issue.assert_called_once()
|
||||
|
||||
def test_create_issue_with_options(self):
|
||||
"""Test creating an issue with optional fields."""
|
||||
self.mock_api.create_issue.return_value = self.mock_issue
|
||||
|
||||
result = self.client.create(
|
||||
"Test Title",
|
||||
"Test Body",
|
||||
assignees=["user1"],
|
||||
milestone=1,
|
||||
labels=["bug", "priority:high"]
|
||||
)
|
||||
|
||||
assert result == self.mock_issue
|
||||
self.mock_api.create_issue.assert_called_once()
|
||||
|
||||
def test_update_issue(self):
|
||||
"""Test updating an issue."""
|
||||
self.mock_api.update_issue.return_value = self.mock_issue
|
||||
|
||||
result = self.client.update(1, title="New Title")
|
||||
|
||||
assert result == self.mock_issue
|
||||
self.mock_api.update_issue.assert_called_once()
|
||||
|
||||
def test_close_issue(self):
|
||||
"""Test closing an issue."""
|
||||
closed_issue = Mock(spec=Issue)
|
||||
closed_issue.state = "closed"
|
||||
self.mock_api.update_issue.return_value = closed_issue
|
||||
|
||||
result = self.client.close(1)
|
||||
|
||||
assert result.state == "closed"
|
||||
self.mock_api.update_issue.assert_called_once()
|
||||
|
||||
def test_reopen_issue(self):
|
||||
"""Test reopening an issue."""
|
||||
opened_issue = Mock(spec=Issue)
|
||||
opened_issue.state = "open"
|
||||
self.mock_api.update_issue.return_value = opened_issue
|
||||
|
||||
result = self.client.reopen(1)
|
||||
|
||||
assert result.state == "open"
|
||||
self.mock_api.update_issue.assert_called_once()
|
||||
|
||||
def test_add_labels(self):
|
||||
"""Test adding labels to an issue."""
|
||||
# Mock getting current issue
|
||||
self.mock_issue.labels = [Mock(name="existing")]
|
||||
self.mock_api.get_issue.return_value = self.mock_issue
|
||||
|
||||
# Mock update result
|
||||
updated_issue = Mock(spec=Issue)
|
||||
updated_issue.labels = [Mock(name="existing"), Mock(name="new")]
|
||||
self.mock_api.update_issue.return_value = updated_issue
|
||||
|
||||
result = self.client.add_labels(1, ["new"])
|
||||
|
||||
assert len(result.labels) == 2
|
||||
self.mock_api.get_issue.assert_called_once_with(1)
|
||||
self.mock_api.update_issue.assert_called_once()
|
||||
|
||||
def test_remove_labels(self):
|
||||
"""Test removing labels from an issue."""
|
||||
# Mock getting current issue
|
||||
label1 = Mock(name="keep")
|
||||
label2 = Mock(name="remove")
|
||||
self.mock_issue.labels = [label1, label2]
|
||||
self.mock_api.get_issue.return_value = self.mock_issue
|
||||
|
||||
# Mock update result
|
||||
updated_issue = Mock(spec=Issue)
|
||||
updated_issue.labels = [label1]
|
||||
self.mock_api.update_issue.return_value = updated_issue
|
||||
|
||||
result = self.client.remove_labels(1, ["remove"])
|
||||
|
||||
assert len(result.labels) == 1
|
||||
self.mock_api.get_issue.assert_called_once_with(1)
|
||||
self.mock_api.update_issue.assert_called_once()
|
||||
|
||||
def test_assign_to_milestone(self):
|
||||
"""Test assigning issue to milestone."""
|
||||
self.mock_api.update_issue.return_value = self.mock_issue
|
||||
|
||||
result = self.client.assign_to_milestone(1, 5)
|
||||
|
||||
assert result == self.mock_issue
|
||||
self.mock_api.update_issue.assert_called_once()
|
||||
|
||||
def test_remove_from_milestone(self):
|
||||
"""Test removing issue from milestone."""
|
||||
self.mock_api.update_issue.return_value = self.mock_issue
|
||||
|
||||
result = self.client.remove_from_milestone(1)
|
||||
|
||||
assert result == self.mock_issue
|
||||
self.mock_api.update_issue.assert_called_once()
|
||||
|
||||
def test_set_labels(self):
|
||||
"""Test replacing all labels on an issue."""
|
||||
self.mock_api.update_issue.return_value = self.mock_issue
|
||||
|
||||
result = self.client.set_labels(1, ["bug", "priority:high"])
|
||||
|
||||
assert result == self.mock_issue
|
||||
self.mock_api.update_issue.assert_called_once()
|
||||
|
||||
def test_update_title(self):
|
||||
"""Test updating only issue title."""
|
||||
self.mock_api.update_issue.return_value = self.mock_issue
|
||||
|
||||
result = self.client.update_title(1, "New Title")
|
||||
|
||||
assert result == self.mock_issue
|
||||
self.mock_api.update_issue.assert_called_once()
|
||||
|
||||
def test_update_body(self):
|
||||
"""Test updating only issue body."""
|
||||
self.mock_api.update_issue.return_value = self.mock_issue
|
||||
|
||||
result = self.client.update_body(1, "New Body")
|
||||
|
||||
assert result == self.mock_issue
|
||||
self.mock_api.update_issue.assert_called_once()
|
||||
|
||||
def test_set_priority(self):
|
||||
"""Test setting issue priority."""
|
||||
# Mock getting current issue
|
||||
self.mock_issue.labels = [Mock(name="bug")]
|
||||
self.mock_api.get_issue.return_value = self.mock_issue
|
||||
self.mock_api.update_issue.return_value = self.mock_issue
|
||||
|
||||
result = self.client.set_priority(1, Priority.HIGH)
|
||||
|
||||
assert result == self.mock_issue
|
||||
self.mock_api.get_issue.assert_called_once_with(1)
|
||||
self.mock_api.update_issue.assert_called_once()
|
||||
|
||||
def test_set_status(self):
|
||||
"""Test setting issue status."""
|
||||
# Mock getting current issue
|
||||
self.mock_issue.labels = [Mock(name="bug")]
|
||||
self.mock_api.get_issue.return_value = self.mock_issue
|
||||
self.mock_api.update_issue.return_value = self.mock_issue
|
||||
|
||||
result = self.client.set_status(1, ProjectState.ACTIVE)
|
||||
|
||||
assert result == self.mock_issue
|
||||
self.mock_api.get_issue.assert_called_once_with(1)
|
||||
self.mock_api.update_issue.assert_called_once()
|
||||
|
||||
def test_to_dict(self):
|
||||
"""Test converting issue to dictionary."""
|
||||
result = self.client.to_dict(self.mock_issue)
|
||||
|
||||
expected_keys = ['number', 'title', 'body', 'state', 'html_url',
|
||||
'created_at', 'updated_at', 'assignee', 'labels', 'milestone']
|
||||
|
||||
assert all(key in result for key in expected_keys)
|
||||
assert result['number'] == 1
|
||||
assert result['title'] == "Test Issue"
|
||||
assert result['state'] == "open"
|
||||
|
||||
|
||||
class TestMilestonesClient:
|
||||
"""Test MilestonesClient functionality."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up test fixtures."""
|
||||
self.mock_api = Mock()
|
||||
self.client = MilestonesClient(self.mock_api)
|
||||
|
||||
self.mock_milestone = Mock(spec=Milestone)
|
||||
self.mock_milestone.id = 1
|
||||
self.mock_milestone.title = "Test Milestone"
|
||||
|
||||
def test_list_milestones(self):
|
||||
"""Test listing milestones."""
|
||||
self.mock_api.list_milestones.return_value = [self.mock_milestone]
|
||||
|
||||
result = self.client.list()
|
||||
|
||||
assert result == [self.mock_milestone]
|
||||
self.mock_api.list_milestones.assert_called_once_with("all")
|
||||
|
||||
def test_list_open_milestones(self):
|
||||
"""Test listing open milestones."""
|
||||
self.mock_api.list_milestones.return_value = [self.mock_milestone]
|
||||
|
||||
result = self.client.list_open()
|
||||
|
||||
assert result == [self.mock_milestone]
|
||||
self.mock_api.list_milestones.assert_called_once_with("open")
|
||||
|
||||
def test_create_milestone(self):
|
||||
"""Test creating a milestone."""
|
||||
self.mock_api.create_milestone.return_value = self.mock_milestone
|
||||
|
||||
result = self.client.create("Test Milestone", "Description")
|
||||
|
||||
assert result == self.mock_milestone
|
||||
self.mock_api.create_milestone.assert_called_once()
|
||||
|
||||
|
||||
class TestLabelsClient:
|
||||
"""Test LabelsClient functionality."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up test fixtures."""
|
||||
self.mock_api = Mock()
|
||||
self.client = LabelsClient(self.mock_api)
|
||||
|
||||
self.mock_label = Mock(spec=Label)
|
||||
self.mock_label.id = 1
|
||||
self.mock_label.name = "bug"
|
||||
|
||||
def test_list_labels(self):
|
||||
"""Test listing labels."""
|
||||
self.mock_api.list_labels.return_value = [self.mock_label]
|
||||
|
||||
result = self.client.list()
|
||||
|
||||
assert result == [self.mock_label]
|
||||
self.mock_api.list_labels.assert_called_once()
|
||||
|
||||
def test_create_label(self):
|
||||
"""Test creating a label."""
|
||||
self.mock_api.create_label.return_value = self.mock_label
|
||||
|
||||
result = self.client.create("bug", "red", "Bug reports")
|
||||
|
||||
assert result == self.mock_label
|
||||
self.mock_api.create_label.assert_called_once()
|
||||
|
||||
|
||||
class TestGiteaClient:
|
||||
"""Test the main GiteaClient facade."""
|
||||
|
||||
@patch('gitea.client.GiteaApiClient')
|
||||
def test_client_initialization(self, mock_api_client):
|
||||
"""Test GiteaClient initialization."""
|
||||
config = GiteaConfig(
|
||||
gitea_url="https://gitea.example.com",
|
||||
repo_owner="test_owner",
|
||||
repo_name="test_repo"
|
||||
)
|
||||
|
||||
client = GiteaClient(config)
|
||||
|
||||
assert isinstance(client.issues, IssuesClient)
|
||||
assert isinstance(client.milestones, MilestonesClient)
|
||||
assert isinstance(client.labels, LabelsClient)
|
||||
mock_api_client.assert_called_once_with(config)
|
||||
|
||||
@patch('gitea.client.GiteaConfig.from_git_repository')
|
||||
@patch('gitea.client.GiteaApiClient')
|
||||
def test_client_auto_config(self, mock_api_client, mock_from_git):
|
||||
"""Test GiteaClient with auto-detected config."""
|
||||
mock_config = Mock()
|
||||
mock_from_git.return_value = mock_config
|
||||
|
||||
client = GiteaClient()
|
||||
|
||||
mock_from_git.assert_called_once()
|
||||
mock_api_client.assert_called_once_with(mock_config)
|
||||
|
||||
|
||||
class TestErrorHandling:
|
||||
"""Test error handling throughout the facade."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up test fixtures."""
|
||||
self.mock_api = Mock()
|
||||
self.client = IssuesClient(self.mock_api)
|
||||
|
||||
def test_gitea_error_propagation(self):
|
||||
"""Test that GiteaError is properly propagated."""
|
||||
self.mock_api.get_issue.side_effect = GiteaError("API Error")
|
||||
|
||||
with pytest.raises(GiteaError):
|
||||
self.client.get(1)
|
||||
|
||||
def test_not_found_error_propagation(self):
|
||||
"""Test that GiteaNotFoundError is properly propagated."""
|
||||
self.mock_api.get_issue.side_effect = GiteaNotFoundError("Issue not found")
|
||||
|
||||
with pytest.raises(GiteaNotFoundError):
|
||||
self.client.get(999)
|
||||
|
||||
def test_auth_error_propagation(self):
|
||||
"""Test that GiteaAuthError is properly propagated."""
|
||||
self.mock_api.create_issue.side_effect = GiteaAuthError("Unauthorized")
|
||||
|
||||
with pytest.raises(GiteaAuthError):
|
||||
self.client.create("Title", "Body")
|
||||
|
||||
|
||||
class TestIntegrationPatterns:
|
||||
"""Test integration patterns and best practices."""
|
||||
|
||||
@patch('gitea.client.GiteaApiClient')
|
||||
def test_consistent_interface(self, mock_api_client):
|
||||
"""Test that the facade provides consistent interfaces."""
|
||||
config = GiteaConfig(gitea_url="https://gitea.example.com",
|
||||
repo_owner="owner", repo_name="repo")
|
||||
client = GiteaClient(config)
|
||||
|
||||
# All sub-clients should be available
|
||||
assert hasattr(client, 'issues')
|
||||
assert hasattr(client, 'milestones')
|
||||
assert hasattr(client, 'labels')
|
||||
|
||||
# All should have consistent method patterns
|
||||
assert hasattr(client.issues, 'list')
|
||||
assert hasattr(client.issues, 'get')
|
||||
assert hasattr(client.issues, 'create')
|
||||
assert hasattr(client.issues, 'update')
|
||||
|
||||
assert hasattr(client.milestones, 'list')
|
||||
assert hasattr(client.milestones, 'create')
|
||||
|
||||
assert hasattr(client.labels, 'list')
|
||||
assert hasattr(client.labels, 'create')
|
||||
|
||||
def test_backward_compatibility_dict_conversion(self):
|
||||
"""Test that to_dict provides backward compatibility."""
|
||||
mock_api = Mock()
|
||||
client = IssuesClient(mock_api)
|
||||
|
||||
# Create a mock issue with all expected attributes
|
||||
mock_issue = Mock(spec=Issue)
|
||||
mock_issue.number = 1
|
||||
mock_issue.title = "Test"
|
||||
mock_issue.body = "Body"
|
||||
mock_issue.state = "open"
|
||||
mock_issue.html_url = "https://example.com"
|
||||
mock_issue.created_at = datetime(2023, 1, 1)
|
||||
mock_issue.updated_at = datetime(2023, 1, 1)
|
||||
mock_issue.assignee = None
|
||||
mock_issue.labels = []
|
||||
mock_issue.milestone = None
|
||||
|
||||
result = client.to_dict(mock_issue)
|
||||
|
||||
# Should contain all expected fields for backward compatibility
|
||||
required_fields = ['number', 'title', 'body', 'state', 'html_url',
|
||||
'created_at', 'updated_at', 'assignee', 'labels', 'milestone']
|
||||
|
||||
for field in required_fields:
|
||||
assert field in result, f"Missing required field: {field}"
|
||||
|
||||
def test_label_operations_consistency(self):
|
||||
"""Test that label operations work consistently."""
|
||||
mock_api = Mock()
|
||||
client = IssuesClient(mock_api)
|
||||
|
||||
# Mock issue with labels
|
||||
mock_issue = Mock()
|
||||
mock_issue.labels = [Mock(name="bug"), Mock(name="priority:high")]
|
||||
mock_api.get_issue.return_value = mock_issue
|
||||
mock_api.update_issue.return_value = mock_issue
|
||||
|
||||
# Test all label operations
|
||||
client.add_labels(1, ["new-label"])
|
||||
client.remove_labels(1, ["old-label"])
|
||||
client.set_labels(1, ["label1", "label2"])
|
||||
|
||||
# Should have made appropriate API calls
|
||||
assert mock_api.get_issue.call_count == 2 # add_labels and remove_labels
|
||||
assert mock_api.update_issue.call_count == 3 # all three operations
|
||||
@@ -38,7 +38,7 @@ def create_test_workspace(prefix: str = "test") -> Path:
|
||||
|
||||
|
||||
@contextmanager
|
||||
def test_workspace(prefix: str = "test"):
|
||||
def workspace_context(prefix: str = "test"):
|
||||
"""Context manager for test workspace that auto-cleans up.
|
||||
|
||||
Args:
|
||||
|
||||
Reference in New Issue
Block a user