10 Commits

Author SHA1 Message Date
ae8f9fef45 feat: add JavaScript learning section with starter lessons and sidebar section headers
Implementation following plan:
- S01: Foundation: schema, section config, and router
- S02: Install CodeMirror JavaScript language support
- S03: Create JavaScript lesson JSON files (variables, DOM, events)
- S04: Register JavaScript lessons in module stores
- S05: Add JavaScript validation logic
- S06: Add JavaScript mode to LessonEngine preview rendering
- S07: Add JavaScript mode to CodeEditor
- S08: Update app.js for JavaScript mode support
- S09: Update navigation HTML and CSS theming for JavaScript section
- S10: Add section grouping headers in sidebar navigation
- S11: Update and write tests
2026-03-28 20:22:50 +01:00
372320b807 Merge pull request 'fix: rewrite flexbox tasks to describe outcomes instead of answers' (#5) from 003-flexbox-task-wording into main 2026-03-28 19:59:24 +01:00
61acd692f4 fix: rewrite flexbox task descriptions to describe outcomes instead of answers (#3)
Replace copy-pasteable CSS declarations in all 6 flexbox lesson tasks with
outcome-oriented descriptions. Update validation error messages to hint at
properties without revealing exact declarations. Add regex validation for
flexbox-6 to accept both flex: 1 and flex-grow: 1.
2026-03-28 19:25:32 +01:00
672a2d28cb docs: add spec, plan, and tasks for flexbox task wording fix (#3) 2026-03-28 19:17:48 +01:00
433379155b fix(security): add Content-Security-Policy meta tag
Restricts script sources to self and known CDNs, connect sources to
self and Supabase, blocks unauthorized resource loading. Allows
unsafe-inline for styles (CodeMirror requirement) and blob: for
sandboxed preview iframes.

Addresses SEC-5 (HIGH) from security audit.
2026-03-28 17:01:32 +01:00
756841f8c2 fix(security): sandbox preview iframes to prevent XSS
Add sandbox='allow-scripts' to all preview iframes. This isolates
user-executed code from the parent page's localStorage (auth tokens),
cookies, and DOM. Switch from document.write() to srcdoc attribute
since sandboxed iframes can't use document.write().

Addresses SEC-1 (critical) from security audit.
2026-03-28 16:38:56 +01:00
c97fce1f29 fix: replace github personas with gitea, add ontology telos 2026-03-28 16:19:15 +01:00
8b6a88ad59 test: add 182 new tests for router, sections, renderer, and validator
Generated by wave test-gen pipeline. Coverage:
- router.js: 0% → ~85% (33 tests, all 7 exports)
- sections.js: 0% → ~90% (29 tests, all 5 exports)
- renderer.js: partial → extended (36 tests, difficulty, feedback, sidebar)
- validator.js: partial → extended (84 tests, all types + edge cases)

Total: 43 → 225 tests
2026-03-28 16:14:52 +01:00
4476d26140 chore: update wave.yaml to latest wave version with corrected project config 2026-03-28 15:58:46 +01:00
f28531fb4c ignore auto claude files 2026-01-27 16:53:47 +01:00
29 changed files with 3144 additions and 128 deletions

View File

@@ -1,15 +1,6 @@
{
"permissions": {
"allow": [
"Bash(git add:*)",
"Bash(git commit:*)",
"Bash(git push:*)",
"Bash(npm run build:*)",
"Bash(grep:*)",
"Bash(npm run format.lessons:*)",
"Bash(xargs:*)",
"Bash(cat:*)",
"Bash(prettier --write:*)"
],
"deny": ["Read(./.env)", "Read(./.env.*)", "Read(./secrets/**)"]
},

11
.gitignore vendored
View File

@@ -8,3 +8,14 @@ coverage
# Claude Code local settings (user-specific)
.claude/settings.local.json
.claude_settings.json
# Auto-Claude
.auto-claude
.worktrees
# Wave ephemeral data
.wave/workspaces
.wave/traces
.wave/artifacts
.wave/output

View File

@@ -0,0 +1,139 @@
{
"$schema": "../schemas/code-crispies-module-schema.json",
"id": "js-variables",
"title": "JS Variables",
"description": "Learn to declare variables with let and const, and work with basic data types in JavaScript.",
"mode": "javascript",
"difficulty": "beginner",
"lessons": [
{
"id": "js-const",
"title": "Constants",
"description": "Use <kbd>const</kbd> to declare a variable that cannot be reassigned. Constants are the default choice for most values in modern JavaScript.",
"task": "Declare a constant named <kbd>name</kbd> with the value <kbd>\"Alice\"</kbd>",
"previewHTML": "<p id=\"out\">Waiting...</p>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; }",
"sandboxCSS": "",
"initialCode": "",
"codePrefix": "",
"codeSuffix": "\ndocument.getElementById('out').textContent = name;",
"solution": "const name = \"Alice\";",
"previewContainer": "preview-area",
"validations": [
{
"type": "contains",
"value": "const",
"message": "Use <kbd>const</kbd> to declare the variable"
},
{
"type": "regex",
"value": "const\\s+name\\s*=",
"message": "Declare a constant called <kbd>name</kbd>"
},
{
"type": "regex",
"value": "\"Alice\"|'Alice'|`Alice`",
"message": "Set the value to <kbd>\"Alice\"</kbd>"
}
]
},
{
"id": "js-let",
"title": "Let Variables",
"description": "Use <kbd>let</kbd> to declare variables that you plan to reassign later. Unlike <kbd>const</kbd>, a <kbd>let</kbd> variable can change its value.",
"task": "Declare a variable <kbd>count</kbd> with <kbd>let</kbd> set to <kbd>0</kbd>, then reassign it to <kbd>5</kbd>",
"previewHTML": "<p id=\"out\">Waiting...</p>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; }",
"sandboxCSS": "",
"initialCode": "",
"codePrefix": "",
"codeSuffix": "\ndocument.getElementById('out').textContent = count;",
"solution": "let count = 0;\ncount = 5;",
"previewContainer": "preview-area",
"validations": [
{
"type": "regex",
"value": "let\\s+count\\s*=\\s*0",
"message": "Start with <kbd>let count = 0;</kbd>"
},
{
"type": "regex",
"value": "count\\s*=\\s*5",
"message": "Reassign count to <kbd>5</kbd>"
}
]
},
{
"id": "js-string",
"title": "Template Literals",
"description": "Template literals use backticks <kbd>`</kbd> and <kbd>${}</kbd> to embed expressions inside strings. This makes building dynamic text much easier than string concatenation.",
"task": "Create a constant <kbd>msg</kbd> using a template literal: <kbd>`Hello, ${name}!`</kbd>",
"previewHTML": "<p id=\"out\">Waiting...</p>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; }",
"sandboxCSS": "",
"initialCode": "",
"codePrefix": "const name = \"World\";\n",
"codeSuffix": "\ndocument.getElementById('out').textContent = msg;",
"solution": "const msg = `Hello, ${name}!`;",
"previewContainer": "preview-area",
"validations": [
{
"type": "regex",
"value": "const\\s+msg\\s*=",
"message": "Declare a constant called <kbd>msg</kbd>"
},
{
"type": "contains",
"value": "${name}",
"message": "Use <kbd>${name}</kbd> inside backticks to embed the variable"
},
{
"type": "regex",
"value": "`.*\\$\\{name\\}.*`",
"message": "Wrap the whole string in backticks <kbd>`</kbd>"
}
]
},
{
"id": "js-array",
"title": "Arrays",
"description": "Arrays store ordered lists of values in square brackets. Access items by index (starting at 0) and use <kbd>.length</kbd> to get the count.",
"task": "Create a constant <kbd>colors</kbd> with an array: <kbd>[\"red\", \"green\", \"blue\"]</kbd>",
"previewHTML": "<p id=\"out\">Waiting...</p>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; }",
"sandboxCSS": "",
"initialCode": "",
"codePrefix": "",
"codeSuffix": "\ndocument.getElementById('out').textContent = colors.join(', ');",
"solution": "const colors = [\"red\", \"green\", \"blue\"];",
"previewContainer": "preview-area",
"validations": [
{
"type": "regex",
"value": "const\\s+colors\\s*=",
"message": "Declare a constant called <kbd>colors</kbd>"
},
{
"type": "contains",
"value": "[",
"message": "Use square brackets <kbd>[</kbd> to create an array"
},
{
"type": "regex",
"value": "(\"red\"|'red'|`red`)",
"message": "Include <kbd>\"red\"</kbd> in the array"
},
{
"type": "regex",
"value": "(\"green\"|'green'|`green`)",
"message": "Include <kbd>\"green\"</kbd> in the array"
},
{
"type": "regex",
"value": "(\"blue\"|'blue'|`blue`)",
"message": "Include <kbd>\"blue\"</kbd> in the array"
}
]
}
]
}

139
lessons/51-js-dom.json Normal file
View File

@@ -0,0 +1,139 @@
{
"$schema": "../schemas/code-crispies-module-schema.json",
"id": "js-dom",
"title": "JS DOM",
"description": "Learn to select and modify HTML elements using JavaScript DOM methods like querySelector and textContent.",
"mode": "javascript",
"difficulty": "beginner",
"lessons": [
{
"id": "js-query",
"title": "querySelector",
"description": "Use <kbd>document.querySelector()</kbd> to find the first element matching a CSS selector. It returns a single element you can then modify.",
"task": "Select the <kbd>h1</kbd> element and store it in a constant called <kbd>title</kbd>",
"previewHTML": "<h1>Hello</h1><p id=\"out\">Waiting...</p>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; }",
"sandboxCSS": "",
"initialCode": "",
"codePrefix": "",
"codeSuffix": "\ndocument.getElementById('out').textContent = title.tagName;",
"solution": "const title = document.querySelector('h1');",
"previewContainer": "preview-area",
"validations": [
{
"type": "contains",
"value": "querySelector",
"message": "Use <kbd>document.querySelector()</kbd> to select an element"
},
{
"type": "regex",
"value": "querySelector\\(['\"`]h1['\"`]\\)",
"message": "Pass <kbd>'h1'</kbd> as the selector"
},
{
"type": "regex",
"value": "const\\s+title\\s*=",
"message": "Store the result in a constant called <kbd>title</kbd>"
}
]
},
{
"id": "js-text",
"title": "textContent",
"description": "The <kbd>textContent</kbd> property lets you read or change the text inside an element. Setting it replaces all existing text.",
"task": "Select the <kbd>.msg</kbd> element and set its <kbd>textContent</kbd> to <kbd>\"Done!\"</kbd>",
"previewHTML": "<p class=\"msg\">Waiting...</p>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; }",
"sandboxCSS": "",
"initialCode": "",
"codePrefix": "",
"codeSuffix": "",
"solution": "document.querySelector('.msg').textContent = \"Done!\";",
"previewContainer": "preview-area",
"validations": [
{
"type": "contains",
"value": "querySelector",
"message": "Use <kbd>querySelector</kbd> to find the element"
},
{
"type": "contains",
"value": "textContent",
"message": "Use the <kbd>textContent</kbd> property to change the text"
},
{
"type": "regex",
"value": "(\"Done!\"|'Done!'|`Done!`)",
"message": "Set the text to <kbd>\"Done!\"</kbd>"
}
]
},
{
"id": "js-style",
"title": "Inline Styles",
"description": "Access the <kbd>style</kbd> property to set inline CSS on an element. CSS properties with dashes become camelCase: <kbd>background-color</kbd> becomes <kbd>backgroundColor</kbd>.",
"task": "Select the <kbd>.box</kbd> element and set its <kbd>style.color</kbd> to <kbd>\"coral\"</kbd>",
"previewHTML": "<p class=\"box\">Style me!</p>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; } .box { font-size: 1.5rem; font-weight: bold; }",
"sandboxCSS": "",
"initialCode": "",
"codePrefix": "",
"codeSuffix": "",
"solution": "document.querySelector('.box').style.color = \"coral\";",
"previewContainer": "preview-area",
"validations": [
{
"type": "contains",
"value": "querySelector",
"message": "Use <kbd>querySelector</kbd> to find the element"
},
{
"type": "contains",
"value": ".style.",
"message": "Use the <kbd>.style</kbd> property to set CSS"
},
{
"type": "regex",
"value": "style\\.color\\s*=",
"message": "Set <kbd>style.color</kbd> on the element"
},
{
"type": "regex",
"value": "(\"coral\"|'coral'|`coral`)",
"message": "Set the color to <kbd>\"coral\"</kbd>"
}
]
},
{
"id": "js-classlist",
"title": "classList",
"description": "The <kbd>classList</kbd> property provides methods to add, remove, or toggle CSS classes on an element without touching other classes.",
"task": "Select the <kbd>.card</kbd> element and add the class <kbd>\"active\"</kbd> using <kbd>classList.add()</kbd>",
"previewHTML": "<div class=\"card\">Toggle me</div>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; } .card { padding: 1rem; border: 2px solid gray; border-radius: 8px; } .active { border-color: coral; background: #fff0ee; }",
"sandboxCSS": "",
"initialCode": "",
"codePrefix": "",
"codeSuffix": "",
"solution": "document.querySelector('.card').classList.add(\"active\");",
"previewContainer": "preview-area",
"validations": [
{
"type": "contains",
"value": "classList",
"message": "Use the <kbd>classList</kbd> property"
},
{
"type": "regex",
"value": "classList\\.add\\(",
"message": "Call <kbd>classList.add()</kbd> to add a class"
},
{
"type": "regex",
"value": "(\"active\"|'active'|`active`)",
"message": "Add the class <kbd>\"active\"</kbd>"
}
]
}
]
}

118
lessons/52-js-events.json Normal file
View File

@@ -0,0 +1,118 @@
{
"$schema": "../schemas/code-crispies-module-schema.json",
"id": "js-events",
"title": "JS Events",
"description": "Learn to respond to user interactions with addEventListener for clicks, input changes, and keyboard events.",
"mode": "javascript",
"difficulty": "beginner",
"lessons": [
{
"id": "js-click",
"title": "Click Events",
"description": "Use <kbd>addEventListener('click', ...)</kbd> to run code when a user clicks an element. The first argument is the event name, the second is a callback function.",
"task": "Add a click listener to the <kbd>.btn</kbd> element that sets the <kbd>.msg</kbd> text to <kbd>\"Clicked!\"</kbd>",
"previewHTML": "<button class=\"btn\">Click me</button><p class=\"msg\">Waiting...</p>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; } .btn { padding: 0.5rem 1rem; border: none; background: steelblue; color: white; border-radius: 4px; cursor: pointer; }",
"sandboxCSS": "",
"initialCode": "",
"codePrefix": "const btn = document.querySelector('.btn');\nconst msg = document.querySelector('.msg');\n\n",
"codeSuffix": "",
"solution": "btn.addEventListener('click', () => {\n msg.textContent = \"Clicked!\";\n});",
"previewContainer": "preview-area",
"validations": [
{
"type": "contains",
"value": "addEventListener",
"message": "Use <kbd>addEventListener</kbd> to listen for events"
},
{
"type": "regex",
"value": "addEventListener\\(['\"`]click['\"`]",
"message": "Listen for the <kbd>'click'</kbd> event"
},
{
"type": "contains",
"value": "textContent",
"message": "Use <kbd>textContent</kbd> to update the text"
},
{
"type": "regex",
"value": "(\"Clicked!\"|'Clicked!'|`Clicked!`)",
"message": "Set the text to <kbd>\"Clicked!\"</kbd>"
}
]
},
{
"id": "js-toggle",
"title": "Toggle Classes",
"description": "Combine events with <kbd>classList.toggle()</kbd> to switch a class on and off. Each click adds the class if missing, or removes it if present.",
"task": "Add a click listener to <kbd>.btn</kbd> that toggles the class <kbd>\"on\"</kbd> on <kbd>.lamp</kbd>",
"previewHTML": "<button class=\"btn\">Toggle</button><div class=\"lamp\">💡</div>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; text-align: center; } .btn { padding: 0.5rem 1rem; border: none; background: steelblue; color: white; border-radius: 4px; cursor: pointer; } .lamp { font-size: 3rem; margin-top: 1rem; opacity: 0.3; transition: opacity 0.3s; } .lamp.on { opacity: 1; }",
"sandboxCSS": "",
"initialCode": "",
"codePrefix": "const btn = document.querySelector('.btn');\nconst lamp = document.querySelector('.lamp');\n\n",
"codeSuffix": "",
"solution": "btn.addEventListener('click', () => {\n lamp.classList.toggle('on');\n});",
"previewContainer": "preview-area",
"validations": [
{
"type": "contains",
"value": "addEventListener",
"message": "Use <kbd>addEventListener</kbd> to listen for events"
},
{
"type": "regex",
"value": "addEventListener\\(['\"`]click['\"`]",
"message": "Listen for the <kbd>'click'</kbd> event"
},
{
"type": "regex",
"value": "classList\\.toggle\\(",
"message": "Use <kbd>classList.toggle()</kbd> to switch the class"
},
{
"type": "regex",
"value": "(\"on\"|'on'|`on`)",
"message": "Toggle the class <kbd>\"on\"</kbd>"
}
]
},
{
"id": "js-input",
"title": "Input Events",
"description": "The <kbd>input</kbd> event fires every time the value of an input field changes. Use <kbd>event.target.value</kbd> to read the current value.",
"task": "Add an input listener to <kbd>.field</kbd> that sets <kbd>.out</kbd> text to the input's value",
"previewHTML": "<input class=\"field\" placeholder=\"Type here...\"><p class=\"out\">Echo: </p>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; } .field { padding: 0.5rem; border: 2px solid #ccc; border-radius: 4px; font-size: 1rem; width: 100%; box-sizing: border-box; }",
"sandboxCSS": "",
"initialCode": "",
"codePrefix": "const field = document.querySelector('.field');\nconst out = document.querySelector('.out');\n\n",
"codeSuffix": "",
"solution": "field.addEventListener('input', (event) => {\n out.textContent = event.target.value;\n});",
"previewContainer": "preview-area",
"validations": [
{
"type": "contains",
"value": "addEventListener",
"message": "Use <kbd>addEventListener</kbd> to listen for events"
},
{
"type": "regex",
"value": "addEventListener\\(['\"`]input['\"`]",
"message": "Listen for the <kbd>'input'</kbd> event"
},
{
"type": "contains",
"value": "textContent",
"message": "Use <kbd>textContent</kbd> to update the output"
},
{
"type": "regex",
"value": "(event|e|evt)\\.target\\.value",
"message": "Read the input value with <kbd>event.target.value</kbd>"
}
]
}
]
}

View File

@@ -9,7 +9,7 @@
"id": "flexbox-1",
"title": "Container",
"description": "Before flexbox, creating even simple layouts required floats, positioning hacks, or table-based layouts. Flexbox (Flexible Box Layout) revolutionized CSS by providing a one-dimensional layout system designed specifically for distributing space and aligning content.<br><br><strong>How it works:</strong> When you set <kbd>display: flex</kbd> on an element, it becomes a <em>flex container</em>. Its direct children automatically become <em>flex items</em> that flow along a main axis (horizontal by default). This single property transforms stacked block elements into a horizontal row.<br><br><strong>The two axes:</strong><br>• <em>Main axis</em> The primary direction items flow (row = left→right)<br>• <em>Cross axis</em> Perpendicular to main (row = top→bottom)<br><br><pre>.nav {\n display: flex;\n}</pre>",
"task": "This navigation menu stacks vertically. Add <kbd>display: flex</kbd> to <kbd>.nav</kbd> to arrange the links horizontally.",
"task": "The navigation links are stacking vertically. Make them display side by side in a horizontal row.",
"previewHTML": "<nav class=\"nav\"><a href=\"#\">Home</a><a href=\"#\">Products</a><a href=\"#\">About</a><a href=\"#\">Contact</a></nav>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; margin: 0; } .nav { background: #1a1a2e; padding: 1rem; } .nav a { color: white; text-decoration: none; padding: 8px 1rem; border-radius: 4px; } .nav a:hover { background: rgba(255,255,255,0.1); }",
"sandboxCSS": "",
@@ -22,7 +22,7 @@
{
"type": "property_value",
"value": { "property": "display", "expected": "flex" },
"message": "Set <kbd>display: flex</kbd>"
"message": "Try changing the display mode to create a flex container"
}
]
},
@@ -30,7 +30,7 @@
"id": "flexbox-2",
"title": "Gap",
"description": "The <kbd>gap</kbd> property adds consistent spacing between flex items without needing margins. It only creates space between items, not around the edges.",
"task": "Add <kbd>gap: 1rem</kbd> to space out the navigation links evenly.",
"task": "The navigation links are crammed together with no breathing room. Add 1rem of spacing between them.",
"previewHTML": "<nav class=\"nav\"><a href=\"#\">Home</a><a href=\"#\">Products</a><a href=\"#\">About</a><a href=\"#\">Contact</a></nav>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; margin: 0; } .nav { background: #1a1a2e; padding: 1rem; display: flex; } .nav a { color: white; text-decoration: none; padding: 8px 1rem; border-radius: 4px; background: rgba(255,255,255,0.1); }",
"sandboxCSS": "",
@@ -43,7 +43,7 @@
{
"type": "property_value",
"value": { "property": "gap", "expected": "1rem" },
"message": "Set <kbd>gap: 1rem</kbd>"
"message": "Use the property that adds spacing between flex items"
}
]
},
@@ -51,7 +51,7 @@
"id": "flexbox-3",
"title": "Justify Content",
"description": "<kbd>justify-content</kbd> distributes items along the main axis. Common values:<br>• <kbd>flex-start</kbd> pack items at the start<br>• <kbd>flex-end</kbd> pack at the end<br>• <kbd>center</kbd> center items<br>• <kbd>space-between</kbd> equal space between items<br>• <kbd>space-around</kbd> equal space around items",
"task": "Push the \"Login\" button to the right by setting <kbd>justify-content: space-between</kbd> on the nav.",
"task": "The Login button should sit on the far right, with the other links staying on the left. Distribute the space between them.",
"previewHTML": "<nav class=\"nav\"><div class=\"links\"><a href=\"#\">Home</a><a href=\"#\">Products</a><a href=\"#\">About</a></div><a href=\"#\" class=\"login\">Login</a></nav>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; margin: 0; } .nav { background: #1a1a2e; padding: 1rem; display: flex; } .links { display: flex; gap: 8px; } .nav a { color: white; text-decoration: none; padding: 8px 1rem; border-radius: 4px; } .nav a:hover { background: rgba(255,255,255,0.1); } .login { background: steelblue; }",
"sandboxCSS": "",
@@ -64,7 +64,7 @@
{
"type": "property_value",
"value": { "property": "justify-content", "expected": "space-between" },
"message": "Set <kbd>justify-content: space-between</kbd>"
"message": "Use the property that distributes items along the main axis"
}
]
},
@@ -72,7 +72,7 @@
"id": "flexbox-4",
"title": "Align Items",
"description": "<kbd>align-items</kbd> controls alignment on the cross axis (vertical when flex-direction is row). Values include:<br>• <kbd>stretch</kbd> stretch to fill (default)<br>• <kbd>flex-start</kbd> align to top<br>• <kbd>flex-end</kbd> align to bottom<br>• <kbd>center</kbd> center vertically",
"task": "The logo and nav links have different heights. Center them vertically with <kbd>align-items: center</kbd>.",
"task": "The logo and nav links sit at different heights. Center them vertically so they line up.",
"previewHTML": "<header class=\"header\"><div class=\"logo\">ACME</div><nav><a href=\"#\">Products</a><a href=\"#\">Pricing</a><a href=\"#\">Docs</a></nav></header>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; margin: 0; } .header { background: white; padding: 1rem 2rem; display: flex; justify-content: space-between; border-bottom: 1px solid #eee; } .logo { font-size: 1.5rem; font-weight: bold; color: steelblue; } nav { display: flex; gap: 1rem; } nav a { color: #333; text-decoration: none; font-size: 0.9rem; }",
"sandboxCSS": "",
@@ -85,7 +85,7 @@
{
"type": "property_value",
"value": { "property": "align-items", "expected": "center" },
"message": "Set <kbd>align-items: center</kbd>"
"message": "Use the property that controls cross-axis alignment"
}
]
},
@@ -93,7 +93,7 @@
"id": "flexbox-5",
"title": "Flex Wrap",
"description": "By default, flex items squeeze onto one line. <kbd>flex-wrap: wrap</kbd> allows items to flow onto multiple lines when they run out of space.",
"task": "These cards overflow the container. Add <kbd>flex-wrap: wrap</kbd> to allow them to wrap to new rows.",
"task": "The cards overflow the container instead of fitting within it. Allow the items to flow onto new rows when they run out of space.",
"previewHTML": "<div class=\"cards\"><article class=\"card\">Card 1</article><article class=\"card\">Card 2</article><article class=\"card\">Card 3</article><article class=\"card\">Card 4</article><article class=\"card\">Card 5</article><article class=\"card\">Card 6</article></div>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; background: #f5f5f5; } .cards { display: flex; gap: 1rem; } .card { background: white; padding: 2rem; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); min-width: 120px; text-align: center; }",
"sandboxCSS": "",
@@ -106,7 +106,7 @@
{
"type": "property_value",
"value": { "property": "flex-wrap", "expected": "wrap" },
"message": "Set <kbd>flex-wrap: wrap</kbd>"
"message": "Use the property that allows flex items to wrap onto new lines"
}
]
},
@@ -114,7 +114,7 @@
"id": "flexbox-6",
"title": "Flex Grow",
"description": "The <kbd>flex</kbd> property on items controls how they grow and shrink. <kbd>flex: 1</kbd> makes an item grow to fill available space. Multiple items with <kbd>flex: 1</kbd> share space equally.",
"task": "Make the search input expand to fill available space by setting <kbd>flex: 1</kbd> on <kbd>.search</kbd>.",
"task": "The search input is too narrow. Make it stretch to fill all the remaining space in the toolbar.",
"previewHTML": "<div class=\"toolbar\"><input class=\"search\" type=\"text\" placeholder=\"Search...\"><button class=\"btn\">Search</button><button class=\"btn\">Filters</button></div>",
"previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; } .toolbar { display: flex; gap: 8px; padding: 1rem; background: #f5f5f5; border-radius: 8px; } .search { padding: 8px 1rem; border: 1px solid #ddd; border-radius: 4px; font-size: 1rem; } .btn { padding: 8px 1rem; background: steelblue; color: white; border: none; border-radius: 4px; cursor: pointer; }",
"sandboxCSS": "",
@@ -125,9 +125,9 @@
"previewContainer": "preview-area",
"validations": [
{
"type": "property_value",
"value": { "property": "flex", "expected": "1" },
"message": "Set <kbd>flex: 1</kbd>"
"type": "regex",
"value": "(flex\\s*:\\s*1|flex-grow\\s*:\\s*1)",
"message": "Use the property that makes a flex item grow to fill available space"
}
]
}

7
package-lock.json generated
View File

@@ -13,6 +13,7 @@
"@codemirror/commands": "^6.10.1",
"@codemirror/lang-css": "^6.3.1",
"@codemirror/lang-html": "^6.4.11",
"@codemirror/lang-javascript": "^6.2.5",
"@codemirror/lang-markdown": "^6.5.0",
"@codemirror/state": "^6.5.2",
"@codemirror/theme-one-dark": "^6.1.3",
@@ -208,9 +209,9 @@
}
},
"node_modules/@codemirror/lang-javascript": {
"version": "6.2.4",
"resolved": "https://registry.npmjs.org/@codemirror/lang-javascript/-/lang-javascript-6.2.4.tgz",
"integrity": "sha512-0WVmhp1QOqZ4Rt6GlVGwKJN3KW7Xh4H2q8ZZNGZaP6lRdxXJzmjm4FqvmOojVj6khWJHIb9sp7U/72W7xQgqAA==",
"version": "6.2.5",
"resolved": "https://registry.npmjs.org/@codemirror/lang-javascript/-/lang-javascript-6.2.5.tgz",
"integrity": "sha512-zD4e5mS+50htS7F+TYjBPsiIFGanfVqg4HyUz6WNFikgOPf2BgKlx+TQedI1w6n/IqRBVBbBWmGFdLB/7uxO4A==",
"license": "MIT",
"dependencies": {
"@codemirror/autocomplete": "^6.0.0",

View File

@@ -37,6 +37,7 @@
"@codemirror/commands": "^6.10.1",
"@codemirror/lang-css": "^6.3.1",
"@codemirror/lang-html": "^6.4.11",
"@codemirror/lang-javascript": "^6.2.5",
"@codemirror/lang-markdown": "^6.5.0",
"@codemirror/state": "^6.5.2",
"@codemirror/theme-one-dark": "^6.1.3",

View File

@@ -19,8 +19,8 @@
},
"mode": {
"type": "string",
"enum": ["css", "tailwind", "html", "markdown"],
"description": "Whether this module teaches CSS, Tailwind, HTML, or Markdown"
"enum": ["css", "tailwind", "html", "markdown", "javascript"],
"description": "Whether this module teaches CSS, Tailwind, HTML, Markdown, or JavaScript"
},
"difficulty": {
"type": "string",
@@ -60,7 +60,7 @@
},
"mode": {
"type": "string",
"enum": ["css", "tailwind", "html", "markdown"],
"enum": ["css", "tailwind", "html", "markdown", "javascript"],
"description": "Override module mode for individual lessons"
},
"tailwindConfig": {

View File

@@ -0,0 +1,76 @@
# Implementation Plan
## Objective
Rewrite all 6 flexbox lesson task descriptions to describe the desired visual outcome instead of giving the exact CSS declaration. Update validation messages to hint without revealing answers, and accept alternative valid solutions where applicable.
## Approach
This is a content-only change to a single JSON file (`lessons/flexbox.json`). Each lesson needs three edits:
1. **Task text**: Replace copy-pasteable CSS declarations with outcome-oriented descriptions
2. **Validation messages**: Replace answer-revealing messages with pedagogical hints
3. **Validations array**: Add alternative accepted solutions where multiple CSS approaches achieve the same visual result
The lesson `description` fields (which teach concepts with code examples) remain unchanged — they are the learning material, not the exercise prompt.
## File Mapping
| File | Action | Description |
|------|--------|-------------|
| `lessons/flexbox.json` | modify | Rewrite `task` and validation `message` fields for all 6 lessons; add alternative validations for flexbox-6 |
No new files need to be created. No validator code changes needed — the existing `property_value` and `regex` validation types already support everything required.
## Detailed Changes Per Lesson
### flexbox-1 (Container)
- **Task**: Describe that nav links stack vertically and should display side by side
- **Validation msg**: Hint at display property for flex layout
- **Alt solutions**: None — `display: flex` is the only correct answer (inline-flex changes block behavior)
### flexbox-2 (Gap)
- **Task**: Describe that links are crammed together and need 1rem of spacing between them
- **Validation msg**: Hint at the gap property
- **Alt solutions**: None — `gap: 1rem` is the specific expected value
### flexbox-3 (Justify Content)
- **Task**: Describe that Login button should be pushed to the far right, with nav links on the left
- **Validation msg**: Hint at main-axis distribution property
- **Alt solutions**: None — `justify-content: space-between` is the only property that works when targeting `.nav`
### flexbox-4 (Align Items)
- **Task**: Describe the visual misalignment and ask for vertical centering
- **Validation msg**: Hint at cross-axis alignment property
- **Alt solutions**: None — `align-items: center` is the correct answer
### flexbox-5 (Flex Wrap)
- **Task**: Describe cards overflowing and needing to flow onto new rows
- **Validation msg**: Hint at wrapping property
- **Alt solutions**: None — `flex-wrap: wrap` is the only answer
### flexbox-6 (Flex Grow)
- **Task**: Describe that the search input should stretch to fill remaining space
- **Validation msg**: Hint at flex growth property
- **Alt solutions**: Accept both `flex: 1` and `flex-grow: 1` via regex validation
## Architecture Decisions
1. **No validator code changes**: The existing `regex` validation type can handle alternative solutions for flexbox-6. No need to add a new validation type.
2. **Keep values in tasks where needed**: Some tasks mention target values like "1rem" since the validator checks exact values and students need to know the amount. The key change is removing the *property name* from the task.
3. **Solution field unchanged**: The `solution` field is used for the "show solution" feature and should remain as the canonical answer.
4. **codePrefix unchanged**: The existing codePrefix already shows the selector context (e.g., `.nav {`), which is enough guidance for students.
## Risks
| Risk | Likelihood | Mitigation |
|------|-----------|------------|
| Tasks become too vague for beginners | Low | Descriptions still teach the property; tasks describe specific visual outcomes |
| Alternative regex validation too permissive | Low | Regex will be specific to `flex:\s*1` and `flex-grow:\s*1` patterns |
| Validation messages too cryptic | Low | Messages will hint at the property category without giving the exact declaration |
## Testing Strategy
1. **Run existing test suite**: `npm run test` — all tests should pass since no code or module structure changes
2. **Manual verification**: Validate that each rewritten task accurately describes the visual outcome shown in the preview
3. **JSON schema validation**: Ensure `lessons/flexbox.json` still conforms to the module schema

View File

@@ -0,0 +1,35 @@
# fix: remove answers from flexbox task descriptions (copy-paste score 95%)
**Issue**: [libretech/code-crispies#3](https://git.librete.ch/libretech/code-crispies/issues/3)
**State**: open
**Author**: libretech
**Labels**: none
**Complexity**: simple
## Issue Body
Pedagogy audit: All 6 flexbox exercises give the exact CSS declaration in the task text. Students type without understanding. Rewrite tasks to describe the DESIRED OUTCOME instead of the exact code. Example: 'Add display: flex' → 'The navigation links stack vertically. Make them display side by side.' Accept multiple valid solutions in validations.
## Current State
All 6 lessons in `lessons/flexbox.json` have task descriptions that include the exact CSS declaration students need to type:
| Lesson | Current Task (gives away answer) |
|--------|----------------------------------|
| flexbox-1 | "Add `display: flex` to `.nav`" |
| flexbox-2 | "Add `gap: 1rem` to space out..." |
| flexbox-3 | "setting `justify-content: space-between` on the nav" |
| flexbox-4 | "Center them vertically with `align-items: center`" |
| flexbox-5 | "Add `flex-wrap: wrap` to allow them to wrap" |
| flexbox-6 | "setting `flex: 1` on `.search`" |
Validation error messages also give away answers (e.g., "Set `display: flex`").
## Acceptance Criteria
1. All 6 flexbox task descriptions rewritten to describe the desired visual outcome, not the exact CSS code
2. Students cannot copy-paste from the task into the editor to pass
3. Validation error messages updated to provide hints without revealing the exact declaration
4. Where applicable, validations accept multiple valid CSS solutions (e.g., `flex: 1` and `flex-grow: 1`)
5. Existing tests continue to pass
6. Lesson descriptions (which teach the concepts) remain unchanged

View File

@@ -0,0 +1,13 @@
# Tasks
## Phase 1: Core Content Changes
- [X] Task 1.1: Rewrite task text for all 6 flexbox lessons to describe visual outcomes [P]
- [X] Task 1.2: Rewrite validation error messages to hint without revealing answers [P]
## Phase 2: Alternative Validations
- [X] Task 2.1: Add regex validation for flexbox-6 to accept both `flex: 1` and `flex-grow: 1`
## Phase 3: Validation
- [X] Task 3.1: Run existing test suite to confirm no regressions
- [X] Task 3.2: Verify flexbox.json still conforms to module schema
- [X] Task 3.3: Run lesson format check (`npm run format.lessons`)

View File

@@ -578,6 +578,11 @@ function updateEditorForMode(mode) {
label: "Markdown Editor",
cmMode: "markdown"
},
javascript: {
placeholder: "// Write your JavaScript here...",
label: "JavaScript Editor",
cmMode: "javascript"
},
playground: {
placeholder: "<style>\n /* CSS here */\n</style>\n\n<!-- HTML here -->",
label: "HTML & CSS",
@@ -1493,6 +1498,64 @@ This is \`inline code\`.</code></pre>
</div>
</div>
</div>
`,
javascript: `
<div class="section-overview">
<p><strong>JavaScript</strong> is the programming language of the web. It adds interactivity to HTML pages—responding to clicks, updating content dynamically, validating forms, and much more. Every modern browser includes a JavaScript engine, making it the most widely deployed programming language in the world.</p>
<p>These beginner lessons cover the fundamentals: declaring variables, selecting and modifying DOM elements, and handling user events. Each concept builds on the previous one, giving you the tools to make any web page interactive.</p>
</div>
<div class="topic-row">
<div class="topic-text">
<h2>Variables & Data Types</h2>
<p>JavaScript uses <code>const</code> for values that won't change and <code>let</code> for values that will. Template literals with backticks make it easy to embed expressions in strings using <code>\${...}</code> syntax.</p>
<p>Arrays store ordered collections in square brackets. Objects store key-value pairs in curly braces. These are the building blocks of every JavaScript program.</p>
<a href="#js-variables/0" class="topic-link">Learn JS Variables</a>
</div>
<div class="topic-code">
<div class="code-block">
<pre><code>const name = "Alice";
let count = 0;
count = count + 1;
const msg = \`Hello, \${name}!\`;
const colors = ["red", "green"];</code></pre>
</div>
</div>
</div>
<div class="topic-row">
<div class="topic-text">
<h2>DOM Manipulation</h2>
<p>The DOM (Document Object Model) is how JavaScript sees your HTML. Use <code>document.querySelector()</code> to find elements by CSS selector, then modify them with properties like <code>textContent</code>, <code>style</code>, and <code>classList</code>.</p>
<a href="#js-dom/0" class="topic-link">Practice DOM Methods</a>
</div>
<div class="topic-code">
<div class="code-block">
<pre><code>const title = document.querySelector('h1');
title.textContent = "New Title";
title.style.color = "coral";
title.classList.add("active");</code></pre>
</div>
</div>
</div>
<div class="topic-row">
<div class="topic-text">
<h2>Event Handling</h2>
<p>Events let your code respond to user actions. Use <code>addEventListener()</code> to run a function when something happens—a click, a keystroke, or an input change. The callback receives an event object with details about what happened.</p>
<a href="#js-events/0" class="topic-link">Handle Events</a>
</div>
<div class="topic-code">
<div class="code-block">
<pre><code>const btn = document.querySelector('.btn');
btn.addEventListener('click', () => {
alert('Clicked!');
});</code></pre>
</div>
</div>
</div>
`
};
@@ -2310,7 +2373,7 @@ function showLandingPage() {
*/
function renderFooterLessonLinks() {
const modules = lessonEngine.modules || [];
const sectionGroups = { css: [], html: [] };
const sectionGroups = { css: [], html: [], javascript: [] };
modules.forEach((module) => {
if (module.excludeFromProgress) return;
@@ -2347,7 +2410,7 @@ function renderFooterLessonLinks() {
* Update progress indicators on landing page
*/
function updateLandingProgress() {
["css", "html", "markdown"].forEach((sectionId) => { // tailwind temporarily disabled
["css", "html", "markdown", "javascript"].forEach((sectionId) => { // tailwind temporarily disabled
const progressEl = document.getElementById(`${sectionId}-progress`);
if (progressEl) {
const sectionModules = getModulesBySection(lessonEngine.modules, sectionId);

View File

@@ -31,6 +31,9 @@ import filtersEN from "../../lessons/11-filters.json";
import positioningEN from "../../lessons/12-positioning.json";
import pseudoElementsEN from "../../lessons/13-pseudo-elements.json";
import markdownBasicsEN from "../../lessons/40-markdown-basics.json";
import jsVariablesEN from "../../lessons/50-js-variables.json";
import jsDomEN from "../../lessons/51-js-dom.json";
import jsEventsEN from "../../lessons/52-js-events.json";
import playgroundEN from "../../lessons/98-playground.json";
import goodbyeEN from "../../lessons/99-goodbye.json";
@@ -165,6 +168,10 @@ const moduleStoreEN = [
htmlTablesEN,
// Markdown
markdownBasicsEN,
// JavaScript
jsVariablesEN,
jsDomEN,
jsEventsEN,
// Outro
goodbyeEN,
playgroundEN
@@ -206,6 +213,10 @@ const moduleStoreDE = [
htmlTablesDE,
// Markdown
markdownBasicsEN, // Using EN fallback until translated
// JavaScript
jsVariablesEN, // Using EN fallback until translated
jsDomEN, // Using EN fallback until translated
jsEventsEN, // Using EN fallback until translated
// Outro
goodbyeEN,
playgroundEN
@@ -247,6 +258,10 @@ const moduleStorePL = [
htmlTablesPL,
// Markdown
markdownBasicsEN, // Using EN fallback until translated
// JavaScript
jsVariablesEN, // Using EN fallback until translated
jsDomEN, // Using EN fallback until translated
jsEventsEN, // Using EN fallback until translated
// Outro
goodbyeEN,
playgroundEN
@@ -288,6 +303,10 @@ const moduleStoreES = [
htmlTablesES,
// Markdown
markdownBasicsEN, // Using EN fallback until translated
// JavaScript
jsVariablesEN, // Using EN fallback until translated
jsDomEN, // Using EN fallback until translated
jsEventsEN, // Using EN fallback until translated
// Outro
goodbyeEN,
playgroundEN
@@ -329,6 +348,10 @@ const moduleStoreAR = [
htmlTablesAR,
// Markdown
markdownBasicsEN, // Using EN fallback until translated
// JavaScript
jsVariablesEN, // Using EN fallback until translated
jsDomEN, // Using EN fallback until translated
jsEventsEN, // Using EN fallback until translated
// Outro
goodbyeEN,
playgroundEN
@@ -370,6 +393,10 @@ const moduleStoreUK = [
htmlTablesUK,
// Markdown
markdownBasicsEN, // Using EN fallback until translated
// JavaScript
jsVariablesEN, // Using EN fallback until translated
jsDomEN, // Using EN fallback until translated
jsEventsEN, // Using EN fallback until translated
// Outro
goodbyeEN,
playgroundEN

View File

@@ -31,6 +31,13 @@ export const sections = {
description: "Lightweight markup language for formatting text",
color: "#5b8dd9",
order: 4
},
javascript: {
id: "javascript",
title: "JavaScript",
description: "Interactive scripting for web pages",
color: "#f0db4f",
order: 5
}
};
@@ -65,6 +72,7 @@ export function getModuleSection(module) {
if (mode === "html") return "html";
if (mode === "tailwind") return "tailwind";
if (mode === "markdown") return "markdown";
if (mode === "javascript") return "javascript";
return "css";
}

View File

@@ -2,6 +2,7 @@
* Renderer - Handles UI updates for the CSS learning platform
*/
import { t } from "../i18n.js";
import { getModuleSection, getSection, getSectionList } from "../config/sections.js";
/**
* Compute lesson difficulty based on lesson structure
@@ -72,8 +73,24 @@ export function renderModuleList(container, modules, onSelectModule, onSelectLes
}
}
// Group modules by section for headers
let currentSectionId = null;
// Create list items for each module
modules.forEach((module) => {
// Insert section header when section changes
const sectionId = getModuleSection(module);
if (sectionId !== currentSectionId && !module.excludeFromProgress) {
currentSectionId = sectionId;
const section = getSection(sectionId);
if (section) {
const header = document.createElement("h3");
header.className = "sidebar-section-header";
header.textContent = section.title;
header.style.borderLeftColor = section.color;
container.appendChild(header);
}
}
// Create module container
// Use native <details>/<summary> for expand/collapse
const moduleContainer = document.createElement("details");

View File

@@ -27,7 +27,7 @@ export const RouteType = {
/**
* Valid section IDs
*/
const SECTIONS = ["css", "html", "markdown"]; // tailwind temporarily disabled
const SECTIONS = ["css", "html", "markdown", "javascript"]; // tailwind temporarily disabled
/**
* Valid language codes for URL-based switching

View File

@@ -10,6 +10,8 @@ export function validateUserCode(userCode, lesson) {
return validateHtmlCode(userCode, lesson);
case "tailwind":
return validateTailwindClasses(userCode, lesson);
case "javascript":
return validateJavaScriptCode(userCode, lesson);
case "css":
default:
return validateCssCode(userCode, lesson);
@@ -204,6 +206,80 @@ function validateHtmlCode(userHtml, lesson) {
return result;
}
/**
* Validate user JavaScript code against the lesson requirements
* @param {string} userCode - User submitted JavaScript code
* @param {Object} lesson - The current lesson object
* @returns {Object} Validation result with isValid and message properties
*/
function validateJavaScriptCode(userCode, lesson) {
if (!lesson || !lesson.validations) {
return { isValid: true, message: "No validations specified for this lesson." };
}
const validations = lesson.validations;
let result = {
isValid: true,
validCases: 0,
totalCases: validations.length,
message: "Your CODE looks CRISPY!"
};
for (const validation of validations) {
const { type, value, message, options } = validation;
let validationPassed = false;
switch (type) {
case "contains":
validationPassed = containsValidation(userCode, value, options);
if (!validationPassed) {
result = {
...result,
isValid: false,
message: message || `Your code should include "${value}".`
};
}
break;
case "not_contains":
validationPassed = !containsValidation(userCode, value, options);
if (!validationPassed) {
result = {
...result,
isValid: false,
message: message || `Your code should not include "${value}".`
};
}
break;
case "regex":
validationPassed = regexValidation(userCode, value, options);
if (!validationPassed) {
result = {
...result,
isValid: false,
message: message || "Your code does not match the expected pattern."
};
}
break;
default:
console.warn(`Unknown JavaScript validation type: ${type}`);
validationPassed = true;
}
if (validationPassed) {
result.validCases++;
} else {
return result;
}
}
result.validCases = validations.length;
return result;
}
function validateTailwindClasses(userClasses, lesson) {
if (!lesson || !lesson.validations) {
return { isValid: true, message: "No validations specified for this lesson." };

View File

@@ -8,12 +8,13 @@ import { history } from "@codemirror/commands";
import { html } from "@codemirror/lang-html";
import { css } from "@codemirror/lang-css";
import { markdown } from "@codemirror/lang-markdown";
import { javascript } from "@codemirror/lang-javascript";
import { autocompletion } from "@codemirror/autocomplete";
import { abbreviationTracker, expandAbbreviation } from "@emmetio/codemirror6-plugin";
import { HighlightStyle, syntaxHighlighting } from "@codemirror/language";
import { tags } from "@lezer/highlight";
// Custom theme with purple accent colors (matching app completed state)
// Custom theme with pink accent colors (matching app completed state)
const crispyTheme = EditorView.theme(
{
"&": {
@@ -21,10 +22,10 @@ const crispyTheme = EditorView.theme(
color: "#c8c8d0"
},
".cm-content": {
caretColor: "#9b6dd4"
caretColor: "#d46d9b"
},
".cm-cursor, .cm-dropCursor": {
borderLeftColor: "#9b6dd4"
borderLeftColor: "#d46d9b"
},
"&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection": {
backgroundColor: "#3e3e4a"
@@ -35,10 +36,10 @@ const crispyTheme = EditorView.theme(
},
".cm-searchMatch": {
backgroundColor: "#3e3e4a",
outline: "1px solid #9b6dd4"
outline: "1px solid #d46d9b"
},
".cm-searchMatch.cm-searchMatch-selected": {
backgroundColor: "rgba(155, 109, 212, 0.3)"
backgroundColor: "rgba(212, 109, 155, 0.3)"
},
".cm-activeLine": {
backgroundColor: "#2e2e3a"
@@ -63,13 +64,13 @@ const crispyTheme = EditorView.theme(
// Default syntax highlighting (blue accent)
const defaultHighlight = HighlightStyle.define([
{ tag: tags.keyword, color: "#c9a6eb" },
{ tag: tags.keyword, color: "#eba6c9" },
{ tag: tags.operator, color: "#cdd6f4" },
{ tag: tags.variableName, color: "#89b4fa" },
{ tag: tags.propertyName, color: "#89b4fa" },
{ tag: tags.attributeName, color: "#89b4fa" },
{ tag: tags.className, color: "#89b4fa" },
{ tag: tags.tagName, color: "#c9a6eb" },
{ tag: tags.tagName, color: "#eba6c9" },
{ tag: tags.string, color: "#a6e3a1" },
{ tag: tags.number, color: "#fab387" },
{ tag: tags.bool, color: "#fab387" },
@@ -79,20 +80,20 @@ const defaultHighlight = HighlightStyle.define([
{ tag: tags.punctuation, color: "#cdd6f4" },
{ tag: tags.definition(tags.variableName), color: "#89b4fa" },
{ tag: tags.function(tags.variableName), color: "#89b4fa" },
{ tag: tags.atom, color: "#c9a6eb" },
{ tag: tags.atom, color: "#eba6c9" },
{ tag: tags.unit, color: "#a6e3a1" },
{ tag: tags.color, color: "#f9e2af" }
]);
// CSS section highlighting (purple selectors)
// CSS section highlighting (pink selectors)
const cssHighlight = HighlightStyle.define([
{ tag: tags.keyword, color: "#c9a6eb" },
{ tag: tags.keyword, color: "#eba6c9" },
{ tag: tags.operator, color: "#cdd6f4" },
{ tag: tags.variableName, color: "#c9a6eb" },
{ tag: tags.variableName, color: "#eba6c9" },
{ tag: tags.propertyName, color: "#89b4fa" },
{ tag: tags.attributeName, color: "#89b4fa" },
{ tag: tags.className, color: "#c9a6eb" },
{ tag: tags.tagName, color: "#c9a6eb" },
{ tag: tags.className, color: "#eba6c9" },
{ tag: tags.tagName, color: "#eba6c9" },
{ tag: tags.string, color: "#a6e3a1" },
{ tag: tags.number, color: "#fab387" },
{ tag: tags.bool, color: "#fab387" },
@@ -100,9 +101,9 @@ const cssHighlight = HighlightStyle.define([
{ tag: tags.comment, color: "#6c7086", fontStyle: "italic" },
{ tag: tags.bracket, color: "#cdd6f4" },
{ tag: tags.punctuation, color: "#cdd6f4" },
{ tag: tags.definition(tags.variableName), color: "#c9a6eb" },
{ tag: tags.definition(tags.variableName), color: "#eba6c9" },
{ tag: tags.function(tags.variableName), color: "#89b4fa" },
{ tag: tags.atom, color: "#c9a6eb" },
{ tag: tags.atom, color: "#eba6c9" },
{ tag: tags.unit, color: "#a6e3a1" },
{ tag: tags.color, color: "#f9e2af" }
]);
@@ -181,7 +182,8 @@ export class CodeEditor {
const fullDoc = prefix + initialValue + suffix;
// Get language extension based on mode
const langExtension = this.mode === "html" ? html() : this.mode === "markdown" ? markdown() : css();
const langExtension =
this.mode === "html" ? html() : this.mode === "javascript" ? javascript() : this.mode === "markdown" ? markdown() : css();
// Create read-only zones decorations
const readOnlyMark = Decoration.mark({ class: "cm-readonly-zone" });

View File

@@ -216,18 +216,18 @@ export class LessonEngine {
iframe.style.height = "100%";
iframe.style.border = "none";
iframe.title = "Preview";
iframe.setAttribute("sandbox", "allow-scripts");
const container = document.getElementById(previewContainer || "preview-area");
container.innerHTML = "";
container.appendChild(iframe);
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
iframeDoc.open();
let html;
if (mode === "html" || mode === "playground") {
// For HTML/playground mode, user code IS the HTML content (may include <style> blocks)
const userHtml = this.userCode || "";
iframeDoc.write(`
html = `
<!DOCTYPE html>
<html>
<head>
@@ -239,11 +239,11 @@ export class LessonEngine {
${userHtml}
</body>
</html>
`);
`;
} else if (mode === "tailwind") {
// For Tailwind mode, user code goes directly in HTML classes
const htmlWithClasses = this.injectTailwindClasses(previewHTML, this.userCode);
iframeDoc.write(`
html = `
<!DOCTYPE html>
<html>
<head>
@@ -256,11 +256,35 @@ export class LessonEngine {
${htmlWithClasses}
</body>
</html>
`);
`;
} else if (mode === "javascript") {
// For JavaScript mode, user code runs as a script against previewHTML
const { codePrefix, codeSuffix } = this.currentLesson;
const fullScript = `${codePrefix || ""}${this.userCode || ""}${codeSuffix || ""}`;
html = `
<!DOCTYPE html>
<html>
<head>
<style>html, body { min-height: 100%; margin: 0; }</style>
<style>${previewBaseCSS || ""}</style>
<style>${sandboxCSS || ""}</style>
</head>
<body>
${previewHTML || ""}
<script>
try {
${fullScript}
} catch (e) {
document.body.innerHTML += '<pre style="color:red">' + e.message + '</pre>';
}
</script>
</body>
</html>
`;
} else if (mode === "markdown") {
// For Markdown mode, parse user code to HTML
const renderedHtml = marked.parse(this.userCode || "");
iframeDoc.write(`
html = `
<!DOCTYPE html>
<html>
<head>
@@ -288,11 +312,11 @@ export class LessonEngine {
${renderedHtml}
</body>
</html>
`);
`;
} else {
// Original CSS mode
const userCssWithWrapper = this.getCompleteCss();
iframeDoc.write(`
html = `
<!DOCTYPE html>
<html>
<head>
@@ -305,10 +329,10 @@ export class LessonEngine {
${previewHTML}
</body>
</html>
`);
`;
}
iframeDoc.close();
iframe.srcdoc = html;
}
injectTailwindClasses(html, userClasses) {
@@ -341,6 +365,7 @@ export class LessonEngine {
iframe.style.height = "100%";
iframe.style.border = "none";
iframe.title = "Expected Result";
iframe.setAttribute("sandbox", "allow-scripts");
const container = document.getElementById("preview-expected");
if (!container) return;
@@ -348,12 +373,11 @@ export class LessonEngine {
container.innerHTML = "";
container.appendChild(iframe);
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
iframeDoc.open();
let html;
if (mode === "html" || mode === "playground") {
// For HTML/playground mode, solution code IS the HTML content
iframeDoc.write(`
html = `
<!DOCTYPE html>
<html>
<head>
@@ -365,11 +389,11 @@ export class LessonEngine {
${solutionCode}
</body>
</html>
`);
`;
} else if (mode === "tailwind") {
// For Tailwind mode, inject solution classes into HTML
const htmlWithClasses = this.injectTailwindClasses(previewHTML, solutionCode);
iframeDoc.write(`
html = `
<!DOCTYPE html>
<html>
<head>
@@ -382,11 +406,35 @@ export class LessonEngine {
${htmlWithClasses}
</body>
</html>
`);
`;
} else if (mode === "javascript") {
// For JavaScript mode, solution code runs as a script against previewHTML
const { codePrefix, codeSuffix } = this.currentLesson;
const fullScript = `${codePrefix || ""}${solutionCode}${codeSuffix || ""}`;
html = `
<!DOCTYPE html>
<html>
<head>
<style>html, body { min-height: 100%; margin: 0; }</style>
<style>${previewBaseCSS || ""}</style>
<style>${sandboxCSS || ""}</style>
</head>
<body>
${previewHTML || ""}
<script>
try {
${fullScript}
} catch (e) {
document.body.innerHTML += '<pre style="color:red">' + e.message + '</pre>';
}
</script>
</body>
</html>
`;
} else if (mode === "markdown") {
// For Markdown mode, parse solution to HTML
const renderedHtml = marked.parse(solutionCode || "");
iframeDoc.write(`
html = `
<!DOCTYPE html>
<html>
<head>
@@ -414,12 +462,12 @@ export class LessonEngine {
${renderedHtml}
</body>
</html>
`);
`;
} else {
// CSS mode - wrap solution with prefix/suffix
const { codePrefix, codeSuffix } = this.currentLesson;
const solutionCss = `${codePrefix || ""}${solutionCode}${codeSuffix || ""}`;
iframeDoc.write(`
html = `
<!DOCTYPE html>
<html>
<head>
@@ -432,10 +480,10 @@ export class LessonEngine {
${previewHTML}
</body>
</html>
`);
`;
}
iframeDoc.close();
iframe.srcdoc = html;
}
/**

View File

@@ -4,6 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" href="./favicon.ico" type="image/x-icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline' https://librete.ch https://liberapay.com; style-src 'self' 'unsafe-inline'; connect-src 'self' https://*.supabase.co wss://*.supabase.co; img-src 'self' https://liberapay.com data:; font-src 'self'; frame-src 'self' blob:" />
<!-- Primary Meta Tags -->
<title>CODE CRISPIES - Learn HTML & CSS Interactively | Free Coding Practice</title>
@@ -76,6 +77,7 @@
<a href="#html" class="nav-link" data-section="html">HTML</a>
<!-- <a href="#tailwind" class="nav-link" data-section="tailwind">Tailwind</a> -->
<a href="#markdown" class="nav-link" data-section="markdown">Markdown</a>
<a href="#javascript" class="nav-link" data-section="javascript">JavaScript</a>
<a href="#reference/css" class="nav-link nav-link-ref" data-section="reference">Reference</a>
</nav>
<button id="auth-trigger-header" class="btn btn-outline btn-sm" data-i18n="authLogin">Log In</button>
@@ -177,6 +179,12 @@
<p data-i18n="landingMarkdownDesc">Lightweight markup for formatting text</p>
<span class="section-card-progress" id="markdown-progress"></span>
</a>
<a href="#javascript" class="section-card" data-section="javascript">
<div class="section-card-icon" style="background: #f0db4f; color: #333">JS</div>
<h3>JavaScript</h3>
<p data-i18n="landingJsDesc">Interactive scripting for web pages</p>
<span class="section-card-progress" id="javascript-progress"></span>
</a>
</div>
<p class="device-notice" data-i18n-html="deviceNotice">
<strong>Best on desktop or tablet (landscape).</strong> Mobile works, but larger screens make coding easier.
@@ -193,13 +201,6 @@
<h3 data-i18n="comingSoonAchievementsTitle">Achievements</h3>
<p data-i18n="comingSoonAchievementsText">Earn badges as you master new skills. Track your learning milestones.</p>
</article>
<article class="coming-soon-card">
<span class="coming-soon-icon">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M13 2 3 14h9l-1 8 10-12h-9l1-8z"/></svg>
</span>
<h3 data-i18n="comingSoonJsTitle">JavaScript</h3>
<p data-i18n="comingSoonJsText">Interactive JavaScript lessons with live code execution and DOM manipulation.</p>
</article>
<article class="coming-soon-card">
<span class="coming-soon-icon">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/></svg>
@@ -477,6 +478,7 @@
<a href="#css" class="sidebar-nav-link" data-section="css">CSS</a>
<a href="#html" class="sidebar-nav-link" data-section="html">HTML</a>
<!-- <a href="#tailwind" class="sidebar-nav-link" data-section="tailwind">Tailwind</a> -->
<a href="#javascript" class="sidebar-nav-link" data-section="javascript">JavaScript</a>
<button id="auth-trigger-mobile" class="sidebar-nav-link sidebar-auth-link" data-i18n="authLogin">Log In</button>
</nav>

View File

@@ -1,15 +1,15 @@
/* ================= BASE THEME ================= */
:root {
/* Primary colors */
--primary-color: #5e4b8b;
--primary-light: #8a77b5;
--primary-dark: #724a95;
--primary-color: #c9507a;
--primary-light: #e077a0;
--primary-dark: #a83d65;
/* Section colors (default to CSS purple) */
--section-color: #9163b8;
--section-color-light: #a87dc8;
--section-color-dark: #724a95;
--section-color-rgb: 145, 99, 184;
/* Section colors (default to CSS pink) */
--section-color: #d95a8a;
--section-color-light: #e87da6;
--section-color-dark: #b84472;
--section-color-rgb: 217, 90, 138;
/* Secondary colors */
--secondary-color: #444444;
@@ -23,9 +23,9 @@
--white-text: #ffffff;
/* Background colors */
--bg-color: #f8f7fc;
--bg-color: #fcf7f9;
--panel-bg: #ffffff;
--code-bg: #f7f5fa;
--code-bg: #faf5f7;
--editor-bg: #1e1e1e;
--editor-highlight: #303030;
@@ -34,9 +34,9 @@
/* Status colors */
--info-color: #7a93fe;
--success-color: #9b6dd4;
--success-color-dark: #7c4dff;
--success-color-light: #c9b8e8;
--success-color: #d46d9b;
--success-color-dark: #b84472;
--success-color-light: #e8b8d0;
--error-color: #cb6e75;
--danger-color: #dc3545;
@@ -252,11 +252,11 @@ kbd {
}
.logo h1 .code-text {
color: #9163b8;
color: #d95a8a;
}
.logo h1 .crispies-text {
background: #9163b8;
background: #d95a8a;
color: white;
padding: 0.15rem 0.35rem;
border-radius: 4px;
@@ -291,6 +291,14 @@ kbd {
background: #5b8dd9;
}
[data-section="javascript"] .logo h1 .code-text {
color: #d4a017;
}
[data-section="javascript"] .logo h1 .crispies-text {
background: #d4a017;
}
.help-toggle {
width: 28px;
height: 28px;
@@ -468,7 +476,7 @@ kbd {
.completion-badge {
display: inline-block;
padding: 0.15rem 0.5rem;
background: linear-gradient(135deg, #9163b8, #d45aa0, #1aafb8, #7c4dff);
background: linear-gradient(135deg, #d95a8a, #d45aa0, #1aafb8, #ff4d88);
color: white;
font-size: 0.7rem;
font-weight: 600;
@@ -714,7 +722,7 @@ kbd {
position: absolute;
inset: var(--spacing-md);
border-radius: var(--border-radius-md);
background: conic-gradient(from var(--border-angle), #9163b8, #d45aa0, #1aafb8, #7c4dff, #9163b8);
background: conic-gradient(from var(--border-angle), #d95a8a, #d45aa0, #1aafb8, #ff4d88, #d95a8a);
filter: blur(30px);
opacity: 0;
animation: spin-glow 3s ease-out forwards;
@@ -727,7 +735,7 @@ kbd {
position: absolute;
inset: var(--spacing-md);
border-radius: var(--border-radius-md);
background: conic-gradient(from 0deg, #9163b8, #d45aa0, #1aafb8, #7c4dff, #9163b8);
background: conic-gradient(from 0deg, #d95a8a, #d45aa0, #1aafb8, #ff4d88, #d95a8a);
filter: blur(30px);
opacity: 0.35;
pointer-events: none;
@@ -816,7 +824,7 @@ kbd {
border: 6px solid transparent;
background:
linear-gradient(var(--panel-bg), var(--panel-bg)) padding-box,
conic-gradient(from 0deg, #9163b8, #d45aa0, #1aafb8, #7c4dff, #9163b8) border-box;
conic-gradient(from 0deg, #d95a8a, #d45aa0, #1aafb8, #ff4d88, #d95a8a) border-box;
}
.preview-wrapper.matched {
@@ -824,7 +832,7 @@ kbd {
border: 6px solid transparent;
background:
linear-gradient(var(--panel-bg), var(--panel-bg)) padding-box,
conic-gradient(from var(--border-angle), #9163b8, #d45aa0, #1aafb8, #7c4dff, #9163b8) border-box;
conic-gradient(from var(--border-angle), #d95a8a, #d45aa0, #1aafb8, #ff4d88, #d95a8a) border-box;
animation: spin-border 3s ease-out forwards;
overflow: visible;
}
@@ -844,7 +852,7 @@ kbd {
font-weight: 800;
letter-spacing: 0.05em;
color: white;
background: linear-gradient(135deg, #9163b8 0%, #d45aa0 50%, #7c4dff 100%);
background: linear-gradient(135deg, #d95a8a 0%, #d45aa0 50%, #ff4d88 100%);
padding: 1.25rem 2rem 1.75rem;
z-index: 10;
pointer-events: none;
@@ -1142,7 +1150,7 @@ nav.sidebar-section:not(.sidebar-nav-mobile) {
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #9163b8, #d45aa0, #1aafb8, #7c4dff);
background: linear-gradient(90deg, #d95a8a, #d45aa0, #1aafb8, #ff4d88);
background-size: calc(100% * 100 / var(--progress-percent, 100)) 100%;
border-radius: 4px;
transition: width 0.3s ease;
@@ -1206,7 +1214,7 @@ nav.sidebar-section:not(.sidebar-nav-mobile) {
}
/* Each milestone gets a color evenly distributed across the gradient
Gradient: #9163b8 (0%) → #d45aa0 (33%) → #1aafb8 (67%) → #7c4dff (100%) */
Gradient: #d95a8a (0%) → #d45aa0 (33%) → #1aafb8 (67%) → #ff4d88 (100%) */
.milestone.reached:nth-child(1) { background: #a55eac; } /* ~14% */
.milestone.reached:nth-child(2) { background: #c459a2; } /* ~28% */
.milestone.reached:nth-child(3) { background: #d45aa0; } /* ~33% pink */
@@ -1214,12 +1222,12 @@ nav.sidebar-section:not(.sidebar-nav-mobile) {
.milestone.reached:nth-child(5) { background: #7785ac; } /* ~50% */
.milestone.reached:nth-child(6) { background: #33a3b6; } /* ~62% */
.milestone.reached:nth-child(7) { background: #4889d8; } /* ~80% */
.milestone.reached:nth-child(8) { background: #7c4dff; } /* 100% */
.milestone.reached:nth-child(8) { background: #ff4d88; } /* 100% */
.milestone.current {
color: white;
transform: scale(1.15);
box-shadow: 0 2px 8px rgba(145, 99, 184, 0.4);
box-shadow: 0 2px 8px rgba(217, 90, 138, 0.4);
}
.milestone.next {
@@ -1244,6 +1252,22 @@ nav.sidebar-section:not(.sidebar-nav-mobile) {
animation: milestone-pop 0.5s ease-out;
}
/* Sidebar section grouping headers */
.sidebar-section-header {
font-size: 0.7rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.05em;
color: var(--light-text);
padding: 0.75rem 0.75rem 0.25rem;
margin: 0.5rem 0 0;
border-left: 3px solid transparent;
}
.sidebar-section-header:first-child {
margin-top: 0;
}
/* Module List in Sidebar */
.module-list {
/* No max-height - parent nav.sidebar-section handles overflow */
@@ -2590,7 +2614,7 @@ input:checked + .toggle-slider::before {
margin-top: var(--spacing-lg);
text-align: center;
padding: 0.75rem 1.5rem;
background: linear-gradient(135deg, rgba(145, 99, 184, 0.1), rgba(212, 90, 160, 0.1), rgba(26, 175, 184, 0.1));
background: linear-gradient(135deg, rgba(217, 90, 138, 0.1), rgba(212, 90, 160, 0.1), rgba(26, 175, 184, 0.1));
border-radius: var(--border-radius-md);
color: var(--light-text);
font-size: 0.9rem;
@@ -2840,7 +2864,7 @@ input:checked + .toggle-slider::before {
}
.section-overview code {
background: rgba(var(--section-color-rgb, 145, 99, 184), 0.1);
background: rgba(var(--section-color-rgb, 217, 90, 138), 0.1);
color: var(--section-color-dark, var(--primary-dark));
padding: 0.1rem 0.35rem;
border-radius: 4px;
@@ -2950,7 +2974,7 @@ input:checked + .toggle-slider::before {
/* Inline code in topic text */
.topic-text code {
background: rgba(var(--section-color-rgb, 145, 99, 184), 0.1);
background: rgba(var(--section-color-rgb, 217, 90, 138), 0.1);
color: var(--section-color-dark, var(--primary-dark));
padding: 0.15rem 0.4rem;
border-radius: 4px;
@@ -3592,7 +3616,7 @@ input:checked + .toggle-slider::before {
}
/* ================= SECTION COLOR CODING ================= */
/* CSS Section uses default purple from :root */
/* CSS Section uses default pink from :root */
/* HTML Section - Pink (balanced) */
[data-section="html"] {
@@ -3618,9 +3642,17 @@ input:checked + .toggle-slider::before {
--section-color-rgb: 91, 141, 217;
}
/* JavaScript Section - Gold */
[data-section="javascript"] {
--section-color: #d4a017;
--section-color-light: #e0b840;
--section-color-dark: #b08610;
--section-color-rgb: 212, 160, 23;
}
/* Apply section colors to nav links */
.nav-link[data-section="css"] {
color: #9163b8;
color: #d95a8a;
}
.nav-link[data-section="html"] {
@@ -3635,10 +3667,14 @@ input:checked + .toggle-slider::before {
color: #5b8dd9;
}
.nav-link[data-section="javascript"] {
color: #d4a017;
}
.nav-link[data-section="css"]:hover,
.nav-link[data-section="css"].active {
background: rgba(145, 99, 184, 0.1);
color: #724a95;
background: rgba(217, 90, 138, 0.1);
color: #a83d65;
}
.nav-link[data-section="html"]:hover,
@@ -3659,14 +3695,20 @@ input:checked + .toggle-slider::before {
color: #4070b8;
}
.nav-link[data-section="javascript"]:hover,
.nav-link[data-section="javascript"].active {
background: rgba(212, 160, 23, 0.1);
color: #b08610;
}
/* Hint section colors */
body[data-section="css"] .hint {
background: rgba(145, 99, 184, 0.3);
background: rgba(217, 90, 138, 0.3);
border-left-color: #a98cd6;
}
body[data-section="css"] .hint-progress {
background: #9163b8;
background: #d95a8a;
}
body[data-section="html"] .hint {
@@ -3696,6 +3738,15 @@ body[data-section="markdown"] .hint-progress {
background: #5b8dd9;
}
body[data-section="javascript"] .hint {
background: rgba(212, 160, 23, 0.3);
border-left-color: #e0b840;
}
body[data-section="javascript"] .hint-progress {
background: #d4a017;
}
/* RTL hint border */
[dir="rtl"] body[data-section="css"] .hint {
border-right-color: #a98cd6;
@@ -3713,12 +3764,16 @@ body[data-section="markdown"] .hint-progress {
border-right-color: #7ba3e5;
}
[dir="rtl"] body[data-section="javascript"] .hint {
border-right-color: #e0b840;
}
/* Reference nav link colors */
.ref-nav-link[data-ref="css"],
.ref-nav-link[data-ref="selectors"],
.ref-nav-link[data-ref="flexbox"],
.ref-nav-link[data-ref="grid"] {
color: #9163b8;
color: #d95a8a;
}
.ref-nav-link[data-ref="css"]:hover,
@@ -3729,8 +3784,8 @@ body[data-section="markdown"] .hint-progress {
.ref-nav-link[data-ref="flexbox"].active,
.ref-nav-link[data-ref="grid"]:hover,
.ref-nav-link[data-ref="grid"].active {
background: rgba(145, 99, 184, 0.15);
color: #724a95;
background: rgba(217, 90, 138, 0.15);
color: #a83d65;
}
.ref-nav-link[data-ref="html"] {
@@ -3745,21 +3800,21 @@ body[data-section="markdown"] .hint-progress {
/* CodeMirror section color overrides */
body[data-section="css"] .cm-editor .cm-content {
caret-color: #9163b8 !important;
caret-color: #d95a8a !important;
}
body[data-section="css"] .cm-editor .cm-cursor,
body[data-section="css"] .cm-editor .cm-dropCursor {
border-left-color: #9163b8 !important;
border-left-color: #d95a8a !important;
}
body[data-section="css"] .cm-editor .cm-selectionBackground,
body[data-section="css"] .cm-editor .cm-content ::selection {
background-color: rgba(145, 99, 184, 0.25) !important;
background-color: rgba(217, 90, 138, 0.25) !important;
}
body[data-section="css"] .cm-editor .cm-activeLine {
background-color: rgba(145, 99, 184, 0.08) !important;
background-color: rgba(217, 90, 138, 0.08) !important;
}
body[data-section="html"] .cm-editor .cm-content {
@@ -3816,14 +3871,32 @@ body[data-section="markdown"] .cm-editor .cm-activeLine {
background-color: rgba(91, 141, 217, 0.08) !important;
}
body[data-section="javascript"] .cm-editor .cm-content {
caret-color: #d4a017 !important;
}
body[data-section="javascript"] .cm-editor .cm-cursor,
body[data-section="javascript"] .cm-editor .cm-dropCursor {
border-left-color: #d4a017 !important;
}
body[data-section="javascript"] .cm-editor .cm-selectionBackground,
body[data-section="javascript"] .cm-editor .cm-content ::selection {
background-color: rgba(212, 160, 23, 0.25) !important;
}
body[data-section="javascript"] .cm-editor .cm-activeLine {
background-color: rgba(212, 160, 23, 0.08) !important;
}
/* Module pill section colors */
body[data-section="css"] .module-pill {
background: rgba(145, 99, 184, 0.1);
color: #9163b8;
background: rgba(217, 90, 138, 0.1);
color: #d95a8a;
}
body[data-section="css"] .module-pill .level-indicator {
color: #724a95;
color: #a83d65;
}
body[data-section="html"] .module-pill {
@@ -3853,9 +3926,18 @@ body[data-section="markdown"] .module-pill .level-indicator {
color: #4070b8;
}
body[data-section="javascript"] .module-pill {
background: rgba(212, 160, 23, 0.1);
color: #d4a017;
}
body[data-section="javascript"] .module-pill .level-indicator {
color: #b08610;
}
/* Code block border section colors */
body[data-section="css"] .code-block {
border-color: rgba(145, 99, 184, 0.4);
border-color: rgba(217, 90, 138, 0.4);
}
body[data-section="html"] .code-block {
@@ -3870,6 +3952,10 @@ body[data-section="markdown"] .code-block {
border-color: rgba(91, 141, 217, 0.4);
}
body[data-section="javascript"] .code-block {
border-color: rgba(212, 160, 23, 0.4);
}
/* Section code block CodeMirror syntax highlighting overrides */
body[data-section="css"] .code-block .cm-editor .cm-line {
color: #c9c0e0;
@@ -3887,9 +3973,13 @@ body[data-section="markdown"] .code-block .cm-editor .cm-line {
color: #c0d0e8;
}
body[data-section="javascript"] .code-block .cm-editor .cm-line {
color: #e0d8b0;
}
/* Task instruction bubble section colors */
[data-section="css"] .task-instruction {
background: rgba(145, 99, 184, 0.92);
background: rgba(217, 90, 138, 0.92);
}
[data-section="html"] .task-instruction {
@@ -3904,9 +3994,13 @@ body[data-section="markdown"] .code-block .cm-editor .cm-line {
background: rgba(91, 141, 217, 0.92);
}
[data-section="javascript"] .task-instruction {
background: rgba(212, 160, 23, 0.92);
}
/* Section page progress bar colors */
body[data-section="css"] .section-progress-bar .progress-fill {
background: #9163b8;
background: #d95a8a;
}
body[data-section="html"] .section-progress-bar .progress-fill {
@@ -3921,9 +4015,13 @@ body[data-section="markdown"] .section-progress-bar .progress-fill {
background: #5b8dd9;
}
body[data-section="javascript"] .section-progress-bar .progress-fill {
background: #d4a017;
}
/* Section page header colors */
[data-section="css"] .section-hero h1 {
color: #9163b8;
color: #d95a8a;
}
[data-section="html"] .section-hero h1 {
@@ -3938,9 +4036,13 @@ body[data-section="markdown"] .section-progress-bar .progress-fill {
color: #5b8dd9;
}
[data-section="javascript"] .section-hero h1 {
color: #d4a017;
}
/* Lesson title h2 section colors */
body[data-section="css"] #lesson-title {
color: #9163b8;
color: #d95a8a;
}
body[data-section="html"] #lesson-title {
@@ -3955,6 +4057,10 @@ body[data-section="markdown"] #lesson-title {
color: #5b8dd9;
}
body[data-section="javascript"] #lesson-title {
color: #d4a017;
}
/* Section and Reference footer - override landing-footer styles */
.section-footer.landing-footer,
.reference-footer.landing-footer {

View File

@@ -19,6 +19,10 @@ describe("Lessons Config Module", () => {
expect(moduleIds).toContain("css-basic-selectors");
expect(moduleIds).toContain("box-model");
expect(moduleIds).toContain("flexbox");
// JavaScript modules
expect(moduleIds).toContain("js-variables");
expect(moduleIds).toContain("js-dom");
expect(moduleIds).toContain("js-events");
});
test("should have mode set on each lesson", async () => {
@@ -27,7 +31,7 @@ describe("Lessons Config Module", () => {
modules.forEach((module) => {
module.lessons.forEach((lesson) => {
expect(lesson.mode).toBeDefined();
expect(["html", "css", "tailwind", "markdown", "playground"]).toContain(lesson.mode);
expect(["html", "css", "tailwind", "markdown", "javascript", "playground"]).toContain(lesson.mode);
});
});
});

View File

@@ -0,0 +1,538 @@
import { describe, test, expect, vi, beforeEach, afterEach } from "vitest";
import {
renderModuleList,
renderLesson,
renderLevelIndicator,
renderDifficultyBadge,
showFeedback,
clearFeedback,
updateActiveLessonInSidebar,
computeLessonDifficulty
} from "../../src/helpers/renderer.js";
// Mock i18n
vi.mock("../../src/i18n.js", () => ({
t: (key, params = {}) => {
const translations = {
lessonLabel: "Lesson",
untitledLesson: "Untitled Lesson",
lessonFallback: `Lesson ${params.index || ""}`,
difficulty_easy_label: "Easy difficulty",
difficulty_medium_label: "Medium difficulty",
difficulty_hard_label: "Hard difficulty",
difficulty_easy: "Easy",
difficulty_medium: "Medium",
difficulty_hard: "Hard"
};
return translations[key] || key;
}
}));
describe("Renderer Extended Coverage", () => {
beforeEach(() => {
document.body.innerHTML = `
<div id="module-list"></div>
<div class="lesson-title-row">
<h2 id="title"></h2>
</div>
<div id="description"></div>
<div id="task"></div>
<div id="preview"></div>
<div id="prefix"></div>
<textarea id="input"></textarea>
<div id="suffix"></div>
<div id="level-indicator"></div>
<div class="editor-content"></div>
<input type="checkbox" id="disable-feedback-toggle" checked>
`;
localStorage.clear();
});
describe("renderModuleList - progress tracking", () => {
test("renderModuleList_CorruptedProgress_HandlesGracefully", () => {
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
localStorage.setItem("codeCrispies.progress", "not-valid-json{{{");
const container = document.getElementById("module-list");
const modules = [{ id: "mod1", title: "Module 1", lessons: [{ title: "L1" }] }];
renderModuleList(container, modules, vi.fn(), vi.fn());
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("Error parsing progress"), expect.anything());
// Should still render modules despite parse error
expect(container.querySelectorAll(".module-header").length).toBe(1);
errorSpy.mockRestore();
});
test("renderModuleList_CompletedModule_AddedCompletedClass", () => {
localStorage.setItem(
"codeCrispies.progress",
JSON.stringify({
mod1: { completed: [0, 1], current: 1 }
})
);
const container = document.getElementById("module-list");
const modules = [{ id: "mod1", title: "Module 1", lessons: [{ title: "L1" }, { title: "L2" }] }];
renderModuleList(container, modules, vi.fn(), vi.fn());
const header = container.querySelector(".module-header");
expect(header.classList.contains("completed")).toBe(true);
});
test("renderModuleList_PartiallyCompleted_NoCompletedClass", () => {
localStorage.setItem(
"codeCrispies.progress",
JSON.stringify({
mod1: { completed: [0], current: 1 }
})
);
const container = document.getElementById("module-list");
const modules = [{ id: "mod1", title: "Module 1", lessons: [{ title: "L1" }, { title: "L2" }] }];
renderModuleList(container, modules, vi.fn(), vi.fn());
const header = container.querySelector(".module-header");
expect(header.classList.contains("completed")).toBe(false);
});
test("renderModuleList_CompletedLesson_HasCompletedClass", () => {
localStorage.setItem(
"codeCrispies.progress",
JSON.stringify({
mod1: { completed: [0], current: 1 }
})
);
const container = document.getElementById("module-list");
const modules = [{ id: "mod1", title: "Module 1", lessons: [{ title: "L1" }, { title: "L2" }] }];
renderModuleList(container, modules, vi.fn(), vi.fn());
const lessonItems = container.querySelectorAll(".lesson-list-item");
expect(lessonItems[0].classList.contains("completed")).toBe(true);
expect(lessonItems[1].classList.contains("completed")).toBe(false);
});
test("renderModuleList_CurrentLesson_HasCurrentClass", () => {
localStorage.setItem(
"codeCrispies.progress",
JSON.stringify({
mod1: { completed: [0], current: 1 }
})
);
const container = document.getElementById("module-list");
const modules = [{ id: "mod1", title: "Module 1", lessons: [{ title: "L1" }, { title: "L2" }] }];
renderModuleList(container, modules, vi.fn(), vi.fn());
const lessonItems = container.querySelectorAll(".lesson-list-item");
expect(lessonItems[1].classList.contains("current")).toBe(true);
expect(lessonItems[0].classList.contains("current")).toBe(false);
});
});
describe("renderModuleList - welcome/playground always expanded", () => {
test("renderModuleList_WelcomeModule_AlwaysExpanded", () => {
const container = document.getElementById("module-list");
const modules = [{ id: "welcome", title: "Welcome", lessons: [{ title: "Intro" }] }];
renderModuleList(container, modules, vi.fn(), vi.fn());
const details = container.querySelector("details.module-container");
expect(details.open).toBe(true);
});
test("renderModuleList_PlaygroundModule_AlwaysExpanded", () => {
const container = document.getElementById("module-list");
const modules = [{ id: "playground", title: "Playground", lessons: [{ title: "Play" }] }];
renderModuleList(container, modules, vi.fn(), vi.fn());
const details = container.querySelector("details.module-container");
expect(details.open).toBe(true);
});
test("renderModuleList_RegularModule_CollapsedByDefault", () => {
const container = document.getElementById("module-list");
const modules = [{ id: "flexbox", title: "Flexbox", lessons: [{ title: "L1" }] }];
renderModuleList(container, modules, vi.fn(), vi.fn());
const details = container.querySelector("details.module-container");
expect(details.open).toBe(false);
});
});
describe("renderModuleList - lesson fallback title", () => {
test("renderModuleList_NoLessonTitle_UsesFallback", () => {
const container = document.getElementById("module-list");
const modules = [{ id: "mod1", title: "Module 1", lessons: [{}] }];
renderModuleList(container, modules, vi.fn(), vi.fn());
const lessonItem = container.querySelector(".lesson-list-item");
expect(lessonItem.textContent).toContain("Lesson");
});
});
describe("renderModuleList - click behavior", () => {
test("renderModuleList_LessonClick_RemovesActiveFromOthers", () => {
const container = document.getElementById("module-list");
const modules = [
{
id: "mod1",
title: "Module 1",
lessons: [{ title: "L1" }, { title: "L2" }]
}
];
const onSelectLesson = vi.fn();
renderModuleList(container, modules, vi.fn(), onSelectLesson);
const lessonItems = container.querySelectorAll(".lesson-list-item");
// Click first lesson
lessonItems[0].click();
expect(lessonItems[0].classList.contains("active")).toBe(true);
expect(onSelectLesson).toHaveBeenCalledWith("mod1", 0);
// Click second lesson
lessonItems[1].click();
expect(lessonItems[0].classList.contains("active")).toBe(false);
expect(lessonItems[1].classList.contains("active")).toBe(true);
expect(onSelectLesson).toHaveBeenCalledWith("mod1", 1);
});
});
describe("renderModuleList - module dataset", () => {
test("renderModuleList_DataAttributes_SetCorrectly", () => {
const container = document.getElementById("module-list");
const modules = [{ id: "flex-mod", title: "Flex Module", lessons: [{ title: "L1" }] }];
renderModuleList(container, modules, vi.fn(), vi.fn());
const details = container.querySelector("details.module-container");
expect(details.dataset.moduleId).toBe("flex-mod");
const header = container.querySelector(".module-header");
expect(header.dataset.moduleId).toBe("flex-mod");
const lesson = container.querySelector(".lesson-list-item");
expect(lesson.dataset.moduleId).toBe("flex-mod");
expect(lesson.dataset.lessonIndex).toBe("0");
});
});
describe("renderModuleList - empty lessons", () => {
test("renderModuleList_EmptyLessonsArray_RendersModuleOnly", () => {
const container = document.getElementById("module-list");
const modules = [{ id: "mod1", title: "Module 1", lessons: [] }];
renderModuleList(container, modules, vi.fn(), vi.fn());
expect(container.querySelectorAll(".module-header").length).toBe(1);
expect(container.querySelectorAll(".lesson-list-item").length).toBe(0);
});
});
describe("renderDifficultyBadge", () => {
test("renderDifficultyBadge_EasyLesson_CreatesEasyBadge", () => {
const container = document.querySelector(".lesson-title-row");
const lesson = { codePrefix: ".box {\n ", solution: "color: red;" };
renderDifficultyBadge(container, lesson);
const badge = container.querySelector(".difficulty-badge");
expect(badge).not.toBeNull();
expect(badge.classList.contains("difficulty-easy")).toBe(true);
expect(badge.querySelectorAll(".bar").length).toBe(3);
});
test("renderDifficultyBadge_MediumLesson_CreatesMediumBadge", () => {
const container = document.querySelector(".lesson-title-row");
const lesson = { codePrefix: "", solution: "p {\n color: red;\n}" };
renderDifficultyBadge(container, lesson);
const badge = container.querySelector(".difficulty-badge");
expect(badge.classList.contains("difficulty-medium")).toBe(true);
});
test("renderDifficultyBadge_HardLesson_CreatesHardBadge", () => {
const container = document.querySelector(".lesson-title-row");
const lesson = { codePrefix: "", solution: ".nav a {\n color: white;\n}" };
renderDifficultyBadge(container, lesson);
const badge = container.querySelector(".difficulty-badge");
expect(badge.classList.contains("difficulty-hard")).toBe(true);
});
test("renderDifficultyBadge_CalledTwice_RemovesPreviousBadge", () => {
const container = document.querySelector(".lesson-title-row");
const lesson1 = { codePrefix: ".box {\n ", solution: "color: red;" };
const lesson2 = { codePrefix: "", solution: ".nav a {\n color: white;\n}" };
renderDifficultyBadge(container, lesson1);
expect(container.querySelectorAll(".difficulty-wrapper").length).toBe(1);
renderDifficultyBadge(container, lesson2);
expect(container.querySelectorAll(".difficulty-wrapper").length).toBe(1);
const badge = container.querySelector(".difficulty-badge");
expect(badge.classList.contains("difficulty-hard")).toBe(true);
});
test("renderDifficultyBadge_HasAriaLabel", () => {
const container = document.querySelector(".lesson-title-row");
const lesson = { codePrefix: ".box {", solution: "color: red;" };
renderDifficultyBadge(container, lesson);
const badge = container.querySelector(".difficulty-badge");
expect(badge.getAttribute("aria-label")).toBeTruthy();
expect(badge.getAttribute("title")).toBeTruthy();
});
});
describe("showFeedback", () => {
test("showFeedback_Success_CreatesSuccessElement", () => {
showFeedback(true, "Great job!");
const feedback = document.querySelector(".feedback-success");
expect(feedback).not.toBeNull();
expect(feedback.innerHTML).toBe("Great job!");
});
test("showFeedback_Success_InsertedAfterEditorContent", () => {
showFeedback(true, "Good!");
const editorContent = document.querySelector(".editor-content");
const feedback = editorContent.nextSibling;
expect(feedback).not.toBeNull();
expect(feedback.classList.contains("feedback-success")).toBe(true);
});
test("showFeedback_Error_ToggleChecked_ShowsError", () => {
const toggle = document.getElementById("disable-feedback-toggle");
toggle.checked = true;
showFeedback(false, "Try again");
const feedback = document.querySelector(".feedback-error");
expect(feedback).not.toBeNull();
expect(feedback.innerHTML).toBe("Try again");
});
test("showFeedback_Error_ToggleUnchecked_HidesError", () => {
const toggle = document.getElementById("disable-feedback-toggle");
toggle.checked = false;
showFeedback(false, "Try again");
const feedback = document.querySelector(".feedback-error");
expect(feedback).toBeNull();
});
test("showFeedback_Error_AutoClearsAfterTimeout", () => {
vi.useFakeTimers();
const toggle = document.getElementById("disable-feedback-toggle");
toggle.checked = true;
showFeedback(false, "Error!");
expect(document.querySelector(".feedback-error")).not.toBeNull();
vi.advanceTimersByTime(3000);
expect(document.querySelector(".feedback-error")).toBeNull();
vi.useRealTimers();
});
test("showFeedback_Success_DoesNotAutoCleanup", () => {
vi.useFakeTimers();
showFeedback(true, "Good!");
vi.advanceTimersByTime(5000);
expect(document.querySelector(".feedback-success")).not.toBeNull();
vi.useRealTimers();
});
test("showFeedback_CalledTwice_ClearsPrevious", () => {
showFeedback(true, "First");
showFeedback(true, "Second");
const feedbacks = document.querySelectorAll(".feedback-success");
expect(feedbacks.length).toBe(1);
expect(feedbacks[0].innerHTML).toBe("Second");
});
});
describe("clearFeedback", () => {
test("clearFeedback_NoExistingFeedback_DoesNotThrow", () => {
expect(() => clearFeedback()).not.toThrow();
});
test("clearFeedback_ExistingFeedback_RemovesIt", () => {
showFeedback(true, "Test");
expect(document.querySelector(".feedback-success")).not.toBeNull();
clearFeedback();
expect(document.querySelector(".feedback-success")).toBeNull();
});
test("clearFeedback_CalledMultipleTimes_Safe", () => {
showFeedback(true, "Test");
clearFeedback();
clearFeedback();
clearFeedback();
expect(document.querySelector(".feedback-success")).toBeNull();
});
test("clearFeedback_ClearsTimeout", () => {
vi.useFakeTimers();
const toggle = document.getElementById("disable-feedback-toggle");
toggle.checked = true;
showFeedback(false, "Error");
clearFeedback();
// Advance past the timeout - should not throw
vi.advanceTimersByTime(5000);
vi.useRealTimers();
});
});
describe("updateActiveLessonInSidebar", () => {
beforeEach(() => {
document.body.innerHTML = `
<details class="module-container" data-module-id="mod1">
<summary class="module-header">Module 1</summary>
<div class="lessons-container">
<button class="lesson-list-item active" data-module-id="mod1" data-lesson-index="0">L1</button>
<button class="lesson-list-item" data-module-id="mod1" data-lesson-index="1">L2</button>
</div>
</details>
<details class="module-container" data-module-id="mod2">
<summary class="module-header">Module 2</summary>
<div class="lessons-container">
<button class="lesson-list-item" data-module-id="mod2" data-lesson-index="0">L1</button>
</div>
</details>
`;
// Mock scrollIntoView on all lesson items (not available in jsdom)
document.querySelectorAll(".lesson-list-item").forEach((el) => {
el.scrollIntoView = vi.fn();
});
});
test("updateActiveLessonInSidebar_ValidLesson_ActivatesCorrectItem", () => {
updateActiveLessonInSidebar("mod1", 1);
const items = document.querySelectorAll(".lesson-list-item");
expect(items[0].classList.contains("active")).toBe(false);
expect(items[1].classList.contains("active")).toBe(true);
});
test("updateActiveLessonInSidebar_DifferentModule_ExpandsParent", () => {
const details = document.querySelector('details[data-module-id="mod2"]');
expect(details.open).toBe(false);
updateActiveLessonInSidebar("mod2", 0);
expect(details.open).toBe(true);
const mod2Lesson = document.querySelector('.lesson-list-item[data-module-id="mod2"]');
expect(mod2Lesson.classList.contains("active")).toBe(true);
});
test("updateActiveLessonInSidebar_RemovesPreviousActive", () => {
const firstItem = document.querySelector('.lesson-list-item[data-module-id="mod1"][data-lesson-index="0"]');
expect(firstItem.classList.contains("active")).toBe(true);
updateActiveLessonInSidebar("mod2", 0);
expect(firstItem.classList.contains("active")).toBe(false);
});
test("updateActiveLessonInSidebar_NonExistentItem_DoesNotThrow", () => {
expect(() => {
updateActiveLessonInSidebar("nonexistent", 99);
}).not.toThrow();
// All active classes should still be removed
const activeItems = document.querySelectorAll(".lesson-list-item.active");
expect(activeItems.length).toBe(0);
});
test("updateActiveLessonInSidebar_ScrollsToLesson", () => {
const targetItem = document.querySelector('.lesson-list-item[data-module-id="mod1"][data-lesson-index="1"]');
updateActiveLessonInSidebar("mod1", 1);
expect(targetItem.scrollIntoView).toHaveBeenCalledWith({ behavior: "smooth", block: "nearest" });
});
});
describe("computeLessonDifficulty - additional edge cases", () => {
test("computeLessonDifficulty_NoSolution_ReturnsMedium", () => {
expect(computeLessonDifficulty({ codePrefix: "" })).toBe("medium");
});
test("computeLessonDifficulty_SolutionNoBrace_ReturnsMedium", () => {
expect(
computeLessonDifficulty({
codePrefix: "",
solution: "color: red;"
})
).toBe("medium");
});
test("computeLessonDifficulty_CodePrefixWithBrace_IgnoresSolution", () => {
expect(
computeLessonDifficulty({
codePrefix: ".nav a {",
solution: ".nav a {\n color: white;\n}"
})
).toBe("easy");
});
test("computeLessonDifficulty_NullCodePrefix_ReturnsMedium", () => {
expect(computeLessonDifficulty({ codePrefix: null, solution: null })).toBe("medium");
});
});
describe("renderLesson - edge cases", () => {
test("renderLesson_NullInputEl_DoesNotThrow", () => {
const titleEl = document.getElementById("title");
const descriptionEl = document.getElementById("description");
const taskEl = document.getElementById("task");
const previewEl = document.getElementById("preview");
const prefixEl = document.getElementById("prefix");
const suffixEl = document.getElementById("suffix");
const lesson = { title: "Test", description: "Desc", task: "Task" };
expect(() => {
renderLesson(titleEl, descriptionEl, taskEl, previewEl, prefixEl, null, suffixEl, lesson);
}).not.toThrow();
});
});
describe("renderLevelIndicator - formatting", () => {
test("renderLevelIndicator_ContainsLabelSpan", () => {
const element = document.getElementById("level-indicator");
renderLevelIndicator(element, 5, 12);
const label = element.querySelector(".level-label");
expect(label).not.toBeNull();
expect(label.textContent).toBe("Lesson");
expect(element.textContent).toContain("5 / 12");
});
});
});

234
tests/unit/router.test.js Normal file
View File

@@ -0,0 +1,234 @@
import { describe, test, expect, vi, beforeEach, afterEach } from "vitest";
import { parseHash, updateHash, navigateTo, replaceHash, replaceTo, getShareableUrl, getSectionIds, RouteType } from "../../src/helpers/router.js";
describe("Router", () => {
let pushStateSpy;
let replaceStateSpy;
beforeEach(() => {
// Reset hash
window.location.hash = "";
pushStateSpy = vi.spyOn(history, "pushState").mockImplementation(() => {});
replaceStateSpy = vi.spyOn(history, "replaceState").mockImplementation(() => {});
});
afterEach(() => {
pushStateSpy.mockRestore();
replaceStateSpy.mockRestore();
});
describe("RouteType", () => {
test("RouteType_Constants_CorrectValues", () => {
expect(RouteType.HOME).toBe("home");
expect(RouteType.SECTION).toBe("section");
expect(RouteType.REFERENCE).toBe("reference");
expect(RouteType.LESSON).toBe("lesson");
expect(RouteType.LANGUAGE).toBe("language");
});
});
describe("parseHash", () => {
test("parseHash_EmptyHash_ReturnsHome", () => {
window.location.hash = "";
const result = parseHash();
expect(result).toEqual({ type: RouteType.HOME });
});
test("parseHash_HashOnly_ReturnsHome", () => {
window.location.hash = "#";
const result = parseHash();
expect(result).toEqual({ type: RouteType.HOME });
});
test.each([
["de", "de"],
["pl", "pl"],
["ar", "ar"],
["es", "es"],
["en", "en"],
["uk", "uk"]
])("parseHash_LanguageCode_%s_ReturnsLanguageRoute", (code, expectedLang) => {
window.location.hash = `#${code}`;
const result = parseHash();
expect(result).toEqual({ type: RouteType.LANGUAGE, lang: expectedLang });
});
test.each([
["css", "css"],
["html", "html"],
["markdown", "markdown"],
["javascript", "javascript"]
])("parseHash_SectionId_%s_ReturnsSectionRoute", (sectionId, expectedId) => {
window.location.hash = `#${sectionId}`;
const result = parseHash();
expect(result).toEqual({ type: RouteType.SECTION, sectionId: expectedId });
});
test("parseHash_ReferenceWithoutSubpage_ReturnsReferenceRouteNullRefId", () => {
window.location.hash = "#reference";
const result = parseHash();
expect(result).toEqual({ type: RouteType.REFERENCE, refId: null });
});
test("parseHash_ReferenceWithSubpage_ReturnsReferenceRouteWithRefId", () => {
window.location.hash = "#reference/css";
const result = parseHash();
expect(result).toEqual({ type: RouteType.REFERENCE, refId: "css" });
});
test("parseHash_ReferenceWithFlexboxSubpage_ReturnsCorrectRefId", () => {
window.location.hash = "#reference/flexbox";
const result = parseHash();
expect(result).toEqual({ type: RouteType.REFERENCE, refId: "flexbox" });
});
test("parseHash_SingleUnknownSegment_ReturnsLessonWithIndex0", () => {
window.location.hash = "#flexbox";
const result = parseHash();
expect(result).toEqual({ type: RouteType.LESSON, moduleId: "flexbox", lessonIndex: 0 });
});
test("parseHash_ModuleWithLessonIndex_ReturnsLessonRoute", () => {
window.location.hash = "#flexbox/2";
const result = parseHash();
expect(result).toEqual({ type: RouteType.LESSON, moduleId: "flexbox", lessonIndex: 2 });
});
test("parseHash_ModuleWithIndex0_ReturnsLessonRoute", () => {
window.location.hash = "#box-model/0";
const result = parseHash();
expect(result).toEqual({ type: RouteType.LESSON, moduleId: "box-model", lessonIndex: 0 });
});
test("parseHash_NegativeLessonIndex_ReturnsNull", () => {
window.location.hash = "#module/-1";
const result = parseHash();
expect(result).toBeNull();
});
test("parseHash_NonNumericLessonIndex_ReturnsNull", () => {
window.location.hash = "#module/abc";
const result = parseHash();
expect(result).toBeNull();
});
test("parseHash_ThreeOrMoreSegments_ReturnsNull", () => {
window.location.hash = "#a/b/c";
const result = parseHash();
expect(result).toBeNull();
});
test("parseHash_EmptyModuleIdWithIndex_ReturnsNull", () => {
// #/0 → parts = ["", "0"], moduleId is empty string (falsy)
window.location.hash = "#/0";
const result = parseHash();
expect(result).toBeNull();
});
});
describe("updateHash", () => {
test("updateHash_NewHash_CallsPushState", () => {
window.location.hash = "";
updateHash("flexbox", 2);
expect(pushStateSpy).toHaveBeenCalledWith(null, "", "#flexbox/2");
});
test("updateHash_SameHash_DoesNotCallPushState", () => {
window.location.hash = "#flexbox/2";
updateHash("flexbox", 2);
expect(pushStateSpy).not.toHaveBeenCalled();
});
test("updateHash_DifferentModule_CallsPushState", () => {
window.location.hash = "#flexbox/0";
updateHash("box-model", 0);
expect(pushStateSpy).toHaveBeenCalledWith(null, "", "#box-model/0");
});
});
describe("navigateTo", () => {
test("navigateTo_SectionRoute_CallsPushState", () => {
window.location.hash = "";
navigateTo("css");
expect(pushStateSpy).toHaveBeenCalledWith(null, "", "#css");
});
test("navigateTo_EmptyRoute_NavigatesToHash", () => {
window.location.hash = "#something";
navigateTo("");
expect(pushStateSpy).toHaveBeenCalledWith(null, "", "#");
});
test("navigateTo_SameHash_DoesNotCallPushState", () => {
window.location.hash = "#css";
navigateTo("css");
expect(pushStateSpy).not.toHaveBeenCalled();
});
});
describe("replaceHash", () => {
test("replaceHash_ValidArgs_CallsReplaceState", () => {
replaceHash("flexbox", 3);
expect(replaceStateSpy).toHaveBeenCalledWith(null, "", "#flexbox/3");
});
test("replaceHash_Index0_FormatsCorrectly", () => {
replaceHash("box-model", 0);
expect(replaceStateSpy).toHaveBeenCalledWith(null, "", "#box-model/0");
});
});
describe("replaceTo", () => {
test("replaceTo_Route_CallsReplaceState", () => {
replaceTo("css");
expect(replaceStateSpy).toHaveBeenCalledWith(null, "", "#css");
});
test("replaceTo_EmptyRoute_ReplacesToHash", () => {
replaceTo("");
expect(replaceStateSpy).toHaveBeenCalledWith(null, "", "#");
});
test("replaceTo_ReferenceRoute_FormatsCorrectly", () => {
replaceTo("reference/flexbox");
expect(replaceStateSpy).toHaveBeenCalledWith(null, "", "#reference/flexbox");
});
});
describe("getShareableUrl", () => {
test("getShareableUrl_ValidArgs_ReturnsFullUrl", () => {
const url = getShareableUrl("flexbox", 2);
expect(url).toContain("#flexbox/2");
expect(url).toMatch(/^https?:\/\/.+#flexbox\/2$/);
});
test("getShareableUrl_Index0_IncludesIndex", () => {
const url = getShareableUrl("box-model", 0);
expect(url).toContain("#box-model/0");
});
});
describe("getSectionIds", () => {
test("getSectionIds_ReturnsCopy_NotOriginalArray", () => {
const ids1 = getSectionIds();
const ids2 = getSectionIds();
expect(ids1).toEqual(ids2);
expect(ids1).not.toBe(ids2); // Different references
});
test("getSectionIds_ContainsExpectedSections", () => {
const ids = getSectionIds();
expect(ids).toContain("css");
expect(ids).toContain("html");
expect(ids).toContain("markdown");
expect(ids).toContain("javascript");
});
test("getSectionIds_MutatingCopy_DoesNotAffectOriginal", () => {
const ids = getSectionIds();
ids.push("custom");
const freshIds = getSectionIds();
expect(freshIds).not.toContain("custom");
});
});
});

176
tests/unit/sections.test.js Normal file
View File

@@ -0,0 +1,176 @@
import { describe, test, expect } from "vitest";
import { sections, getSection, getSectionList, getModuleSection, getModulesBySection } from "../../src/config/sections.js";
describe("Sections Config", () => {
describe("sections constant", () => {
test("sections_AllDefined_HasFiveSections", () => {
expect(Object.keys(sections)).toHaveLength(5);
expect(sections).toHaveProperty("css");
expect(sections).toHaveProperty("html");
expect(sections).toHaveProperty("tailwind");
expect(sections).toHaveProperty("markdown");
expect(sections).toHaveProperty("javascript");
});
test("sections_EachSection_HasRequiredFields", () => {
for (const [key, section] of Object.entries(sections)) {
expect(section.id).toBe(key);
expect(section.title).toBeTruthy();
expect(section.description).toBeTruthy();
expect(section.color).toMatch(/^#[0-9a-f]{6}$/);
expect(typeof section.order).toBe("number");
}
});
});
describe("getSection", () => {
test.each([
["css", "CSS"],
["html", "HTML"],
["tailwind", "Tailwind CSS"],
["markdown", "Markdown"],
["javascript", "JavaScript"]
])("getSection_%s_ReturnsCorrectSection", (id, expectedTitle) => {
const section = getSection(id);
expect(section).not.toBeNull();
expect(section.id).toBe(id);
expect(section.title).toBe(expectedTitle);
});
test("getSection_NonExistentId_ReturnsNull", () => {
expect(getSection("nonexistent")).toBeNull();
});
test("getSection_Undefined_ReturnsNull", () => {
expect(getSection(undefined)).toBeNull();
});
test("getSection_EmptyString_ReturnsNull", () => {
expect(getSection("")).toBeNull();
});
});
describe("getSectionList", () => {
test("getSectionList_Default_ReturnsSortedByOrder", () => {
const list = getSectionList();
expect(list).toHaveLength(5);
// Verify sorted by order
for (let i = 1; i < list.length; i++) {
expect(list[i].order).toBeGreaterThan(list[i - 1].order);
}
});
test("getSectionList_Default_CSSIsFirst", () => {
const list = getSectionList();
expect(list[0].id).toBe("css");
});
test("getSectionList_Default_JavaScriptIsLast", () => {
const list = getSectionList();
expect(list[list.length - 1].id).toBe("javascript");
});
test("getSectionList_Default_ContainsAllSections", () => {
const list = getSectionList();
const ids = list.map((s) => s.id);
expect(ids).toContain("css");
expect(ids).toContain("html");
expect(ids).toContain("tailwind");
expect(ids).toContain("markdown");
expect(ids).toContain("javascript");
});
});
describe("getModuleSection", () => {
test("getModuleSection_ExplicitSection_UsesExplicitValue", () => {
const module = { mode: "css", section: "html" };
expect(getModuleSection(module)).toBe("html");
});
test.each([
["css", "css"],
["html", "html"],
["tailwind", "tailwind"],
["markdown", "markdown"],
["javascript", "javascript"]
])("getModuleSection_Mode%s_InfersCorrectSection", (mode, expectedSection) => {
const module = { mode };
expect(getModuleSection(module)).toBe(expectedSection);
});
test("getModuleSection_NoMode_DefaultsToCss", () => {
expect(getModuleSection({})).toBe("css");
});
test("getModuleSection_UndefinedMode_DefaultsToCss", () => {
expect(getModuleSection({ mode: undefined })).toBe("css");
});
test("getModuleSection_UnknownMode_DefaultsToCss", () => {
expect(getModuleSection({ mode: "unknown-mode" })).toBe("css");
});
test("getModuleSection_ExplicitSectionOverridesMode_UsesSection", () => {
const module = { mode: "html", section: "tailwind" };
expect(getModuleSection(module)).toBe("tailwind");
});
});
describe("getModulesBySection", () => {
const testModules = [
{ id: "css-basics", mode: "css" },
{ id: "flexbox", mode: "css" },
{ id: "html-elements", mode: "html" },
{ id: "tailwind-intro", mode: "tailwind" },
{ id: "markdown-basics", mode: "markdown" },
{ id: "welcome", mode: "css", excludeFromProgress: true },
{ id: "playground", mode: "css", excludeFromProgress: true }
];
test("getModulesBySection_Css_ReturnsCssModules", () => {
const result = getModulesBySection(testModules, "css");
expect(result).toHaveLength(2);
expect(result.map((m) => m.id)).toEqual(["css-basics", "flexbox"]);
});
test("getModulesBySection_Html_ReturnsHtmlModules", () => {
const result = getModulesBySection(testModules, "html");
expect(result).toHaveLength(1);
expect(result[0].id).toBe("html-elements");
});
test("getModulesBySection_Tailwind_ReturnsTailwindModules", () => {
const result = getModulesBySection(testModules, "tailwind");
expect(result).toHaveLength(1);
expect(result[0].id).toBe("tailwind-intro");
});
test("getModulesBySection_ExcludesFromProgress_FiltersOut", () => {
const result = getModulesBySection(testModules, "css");
const ids = result.map((m) => m.id);
expect(ids).not.toContain("welcome");
expect(ids).not.toContain("playground");
});
test("getModulesBySection_EmptyModules_ReturnsEmptyArray", () => {
const result = getModulesBySection([], "css");
expect(result).toEqual([]);
});
test("getModulesBySection_NonExistentSection_ReturnsEmptyArray", () => {
const result = getModulesBySection(testModules, "nonexistent");
expect(result).toEqual([]);
});
test("getModulesBySection_ExplicitSectionOverride_IncludesModule", () => {
const modules = [
{ id: "special", mode: "css", section: "html" },
{ id: "normal-html", mode: "html" }
];
const result = getModulesBySection(modules, "html");
expect(result).toHaveLength(2);
expect(result.map((m) => m.id)).toContain("special");
});
});
});

View File

@@ -0,0 +1,735 @@
import { describe, test, expect, vi, beforeEach } from "vitest";
import { validateUserCode, validateCssCode } from "../../src/helpers/validator.js";
describe("Validator Extended Coverage", () => {
describe("validateUserCode mode dispatch", () => {
test("validateUserCode_NoMode_DefaultsToCss", () => {
const result = validateUserCode("color: red;", {
validations: [{ type: "contains", value: "color: red" }]
});
expect(result.isValid).toBe(true);
});
test("validateUserCode_CssMode_UsesCssValidator", () => {
const result = validateUserCode("display: flex;", {
mode: "css",
validations: [{ type: "contains", value: "display: flex" }]
});
expect(result.isValid).toBe(true);
});
test("validateUserCode_TailwindMode_UsesTailwindValidator", () => {
const result = validateUserCode("flex items-center", {
mode: "tailwind",
validations: [{ type: "contains_class", value: "flex" }]
});
expect(result.isValid).toBe(true);
});
test("validateUserCode_HtmlMode_UsesHtmlValidator", () => {
const result = validateUserCode("<div>Hello</div>", {
mode: "html",
validations: [{ type: "element_exists", value: "div" }]
});
expect(result.isValid).toBe(true);
});
test("validateUserCode_UnknownMode_DefaultsToCss", () => {
const result = validateUserCode("color: red;", {
mode: "javascript",
validations: [{ type: "contains", value: "color: red" }]
});
expect(result.isValid).toBe(true);
});
test("validateUserCode_NullLesson_Throws", () => {
expect(() => validateUserCode("anything", null)).toThrow();
});
test("validateUserCode_UndefinedLesson_Throws", () => {
expect(() => validateUserCode("anything", undefined)).toThrow();
});
});
describe("Tailwind validation", () => {
test("tailwind_ContainsClass_Pass", () => {
const result = validateUserCode("flex items-center justify-between", {
mode: "tailwind",
validations: [{ type: "contains_class", value: "flex" }]
});
expect(result.isValid).toBe(true);
expect(result.validCases).toBe(1);
});
test("tailwind_ContainsClass_Fail_ReturnsMessage", () => {
const result = validateUserCode("items-center", {
mode: "tailwind",
validations: [{ type: "contains_class", value: "flex", message: "Add flex class" }]
});
expect(result.isValid).toBe(false);
expect(result.message).toBe("Add flex class");
});
test("tailwind_ContainsClass_Fail_DefaultMessage", () => {
const result = validateUserCode("items-center", {
mode: "tailwind",
validations: [{ type: "contains_class", value: "flex" }]
});
expect(result.isValid).toBe(false);
expect(result.message).toContain("flex");
});
test("tailwind_ContainsClass_PartialMatch_Fails", () => {
// "flex-1" contains "flex" as substring but split should not match
const result = validateUserCode("flex-1 items-center", {
mode: "tailwind",
validations: [{ type: "contains_class", value: "flex" }]
});
expect(result.isValid).toBe(false);
});
test("tailwind_ContainsPattern_Pass", () => {
const result = validateUserCode("text-lg font-bold", {
mode: "tailwind",
validations: [{ type: "contains_pattern", value: "text-\\w+" }]
});
expect(result.isValid).toBe(true);
});
test("tailwind_ContainsPattern_Fail_ReturnsMessage", () => {
const result = validateUserCode("font-bold", {
mode: "tailwind",
validations: [{ type: "contains_pattern", value: "text-\\w+", message: "Add text size" }]
});
expect(result.isValid).toBe(false);
expect(result.message).toBe("Add text size");
});
test("tailwind_ContainsPattern_Fail_DefaultMessage", () => {
const result = validateUserCode("font-bold", {
mode: "tailwind",
validations: [{ type: "contains_pattern", value: "text-\\w+" }]
});
expect(result.isValid).toBe(false);
expect(result.message).toContain("pattern");
});
test("tailwind_DefaultType_FallsBackToContains", () => {
const result = validateUserCode("flex items-center", {
mode: "tailwind",
validations: [{ type: "contains", value: "items-center" }]
});
expect(result.isValid).toBe(true);
});
test("tailwind_NoValidations_ReturnsValid", () => {
const result = validateUserCode("flex", { mode: "tailwind" });
expect(result.isValid).toBe(true);
expect(result.message).toContain("No validations specified");
});
test("tailwind_NullLesson_ReturnsValid", () => {
const result = validateUserCode("flex", { mode: "tailwind", validations: null });
// validateTailwindClasses checks !lesson.validations
expect(result.isValid).toBe(true);
});
test("tailwind_MultipleValidations_AllPass", () => {
const result = validateUserCode("flex items-center gap-4", {
mode: "tailwind",
validations: [
{ type: "contains_class", value: "flex" },
{ type: "contains_class", value: "items-center" },
{ type: "contains_class", value: "gap-4" }
]
});
expect(result.isValid).toBe(true);
expect(result.validCases).toBe(3);
});
test("tailwind_MultipleValidations_EarlyReturn", () => {
const result = validateUserCode("flex", {
mode: "tailwind",
validations: [
{ type: "contains_class", value: "flex" },
{ type: "contains_class", value: "items-center", message: "Missing items-center" },
{ type: "contains_class", value: "gap-4" }
]
});
expect(result.isValid).toBe(false);
expect(result.message).toBe("Missing items-center");
expect(result.validCases).toBe(1);
});
test("tailwind_WhitespaceHandling_LeadingTrailing", () => {
const result = validateUserCode(" flex items-center ", {
mode: "tailwind",
validations: [{ type: "contains_class", value: "flex" }]
});
expect(result.isValid).toBe(true);
});
test("tailwind_EmptyUserClasses_Fails", () => {
const result = validateUserCode("", {
mode: "tailwind",
validations: [{ type: "contains_class", value: "flex" }]
});
expect(result.isValid).toBe(false);
});
});
describe("HTML validation - sibling type", () => {
test("sibling_ValidOrder_Passes", () => {
const result = validateUserCode("<h1>Title</h1><p>Content</p>", {
mode: "html",
validations: [{ type: "sibling", value: { first: "h1", then: "p" } }]
});
expect(result.isValid).toBe(true);
});
test("sibling_NonAdjacentButAfter_Passes", () => {
const result = validateUserCode("<h1>Title</h1><span>Middle</span><p>Content</p>", {
mode: "html",
validations: [{ type: "sibling", value: { first: "h1", then: "p" } }]
});
expect(result.isValid).toBe(true);
});
test("sibling_WrongOrder_Fails", () => {
const result = validateUserCode("<p>Content</p><h1>Title</h1>", {
mode: "html",
validations: [{ type: "sibling", value: { first: "h1", then: "p" } }]
});
// h1 is after p, so p is not a sibling after h1 - but wait, h1 exists and p is before h1...
// Actually h1 exists. nextElementSibling of h1 is nothing. So it fails.
expect(result.isValid).toBe(false);
});
test("sibling_FirstNotFound_Fails", () => {
const result = validateUserCode("<p>Content</p>", {
mode: "html",
validations: [{ type: "sibling", value: { first: "h1", then: "p" }, message: "h1 not found" }]
});
expect(result.isValid).toBe(false);
expect(result.message).toBe("h1 not found");
});
test("sibling_ThenNotFound_Fails", () => {
const result = validateUserCode("<h1>Title</h1><span>Only span</span>", {
mode: "html",
validations: [{ type: "sibling", value: { first: "h1", then: "p" } }]
});
expect(result.isValid).toBe(false);
});
test("sibling_DefaultMessage_ContainsBothSelectors", () => {
const result = validateUserCode("<div>Only div</div>", {
mode: "html",
validations: [{ type: "sibling", value: { first: "h1", then: "p" } }]
});
expect(result.isValid).toBe(false);
expect(result.message).toContain("p");
expect(result.message).toContain("h1");
});
test("sibling_NoFollowingSiblings_Fails", () => {
const result = validateUserCode("<div><h1>Title</h1></div>", {
mode: "html",
validations: [{ type: "sibling", value: { first: "h1", then: "p" } }]
});
expect(result.isValid).toBe(false);
});
});
describe("HTML validation - not_contains type", () => {
test("htmlNotContains_AbsentText_Passes", () => {
const result = validateUserCode("<p>Hello</p>", {
mode: "html",
validations: [{ type: "not_contains", value: "class=" }]
});
expect(result.isValid).toBe(true);
});
test("htmlNotContains_PresentText_Fails", () => {
const result = validateUserCode('<p class="red">Hello</p>', {
mode: "html",
validations: [{ type: "not_contains", value: "class=", message: "Remove classes" }]
});
expect(result.isValid).toBe(false);
expect(result.message).toBe("Remove classes");
});
test("htmlNotContains_DefaultMessage", () => {
const result = validateUserCode('<p class="red">Hello</p>', {
mode: "html",
validations: [{ type: "not_contains", value: "class=" }]
});
expect(result.isValid).toBe(false);
expect(result.message).toContain("should not include");
});
});
describe("HTML validation - regex type", () => {
test("htmlRegex_MatchingPattern_Passes", () => {
const result = validateUserCode('<img src="photo.jpg" alt="A photo">', {
mode: "html",
validations: [{ type: "regex", value: 'alt="[^"]+"' }]
});
expect(result.isValid).toBe(true);
});
test("htmlRegex_NonMatchingPattern_Fails", () => {
const result = validateUserCode('<img src="photo.jpg">', {
mode: "html",
validations: [{ type: "regex", value: 'alt="[^"]+"', message: "Add alt text" }]
});
expect(result.isValid).toBe(false);
expect(result.message).toBe("Add alt text");
});
test("htmlRegex_DefaultMessage", () => {
const result = validateUserCode("<p>Hello</p>", {
mode: "html",
validations: [{ type: "regex", value: "<h1>" }]
});
expect(result.isValid).toBe(false);
expect(result.message).toContain("pattern");
});
});
describe("HTML validation - unknown type", () => {
test("htmlUnknownType_SkipsAndPasses", () => {
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const result = validateUserCode("<p>Hello</p>", {
mode: "html",
validations: [{ type: "unknown_type", value: "anything" }]
});
expect(result.isValid).toBe(true);
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("Unknown HTML validation type"));
warnSpy.mockRestore();
});
});
describe("HTML validation - element_count fallback (>0)", () => {
test("elementCount_NoCountNoMin_ChecksGreaterThanZero_Pass", () => {
const result = validateUserCode("<ul><li>Item</li></ul>", {
mode: "html",
validations: [{ type: "element_count", value: { selector: "li" } }]
});
expect(result.isValid).toBe(true);
});
test("elementCount_NoCountNoMin_NoElements_Fails", () => {
const result = validateUserCode("<ul></ul>", {
mode: "html",
validations: [{ type: "element_count", value: { selector: "li" } }]
});
expect(result.isValid).toBe(false);
});
});
describe("HTML validation - attribute_value edge cases", () => {
test("attributeValue_ElementNotFound_Fails", () => {
const result = validateUserCode("<p>Hello</p>", {
mode: "html",
validations: [{ type: "attribute_value", value: { selector: "input", attr: "type", value: "email" } }]
});
expect(result.isValid).toBe(false);
});
test("attributeValue_NullValue_ChecksExists", () => {
const result = validateUserCode('<input data-test="anything">', {
mode: "html",
validations: [{ type: "attribute_value", value: { selector: "input", attr: "data-test", value: null } }]
});
expect(result.isValid).toBe(true);
});
test("attributeValue_NullValue_AttributeMissing_Fails", () => {
const result = validateUserCode("<input>", {
mode: "html",
validations: [{ type: "attribute_value", value: { selector: "input", attr: "data-test", value: null } }]
});
expect(result.isValid).toBe(false);
});
});
describe("HTML validation - element_text edge cases", () => {
test("elementText_ElementNotFound_Fails", () => {
const result = validateUserCode("<p>Hello</p>", {
mode: "html",
validations: [{ type: "element_text", value: { selector: "button", text: "Submit" } }]
});
expect(result.isValid).toBe(false);
});
test("elementText_EmptyTextContent_FailsForNonEmptyExpected", () => {
const result = validateUserCode("<button></button>", {
mode: "html",
validations: [{ type: "element_text", value: { selector: "button", text: "Submit" } }]
});
expect(result.isValid).toBe(false);
});
test("elementText_EmptyExpectedText_MatchesEmptyElement", () => {
const result = validateUserCode("<button></button>", {
mode: "html",
validations: [{ type: "element_text", value: { selector: "button", text: "" } }]
});
expect(result.isValid).toBe(true);
});
});
describe("CSS validation - containsValidation wholeWord option", () => {
test("contains_WholeWord_ExactMatch_Passes", () => {
const result = validateUserCode("color: red;", {
validations: [{ type: "contains", value: "red", options: { wholeWord: true } }]
});
expect(result.isValid).toBe(true);
});
test("contains_WholeWord_PartialMatch_Fails", () => {
const result = validateUserCode("color: darkred;", {
validations: [{ type: "contains", value: "red", options: { wholeWord: true } }]
});
expect(result.isValid).toBe(false);
});
test("contains_WholeWord_CaseInsensitive_Passes", () => {
const result = validateUserCode("COLOR: RED;", {
validations: [{ type: "contains", value: "red", options: { wholeWord: true, caseSensitive: false } }]
});
expect(result.isValid).toBe(true);
});
test("contains_WholeWord_SpecialChars_Escaped", () => {
// \b doesn't match at non-word chars like ".", so use a word value with special chars around it
const result = validateUserCode("value: calc(100% - 20px);", {
validations: [{ type: "contains", value: "calc", options: { wholeWord: true } }]
});
expect(result.isValid).toBe(true);
// "calc" should not match "recalculate"
const failResult = validateUserCode("/* recalculate */", {
validations: [{ type: "contains", value: "calc", options: { wholeWord: true } }]
});
expect(failResult.isValid).toBe(false);
});
});
describe("CSS validation - regexValidation options", () => {
test("regex_CaseInsensitive_Passes", () => {
const result = validateUserCode("COLOR: RED;", {
validations: [{ type: "regex", value: "color:\\s*red", options: { caseSensitive: false } }]
});
expect(result.isValid).toBe(true);
});
test("regex_CaseSensitive_Default_FailsOnCaseMismatch", () => {
const result = validateUserCode("COLOR: RED;", {
validations: [{ type: "regex", value: "color:\\s*red" }]
});
expect(result.isValid).toBe(false);
});
test("regex_MultilineFalse_DoesNotMatchAcrossLines", () => {
const code = "body {\n color: red;\n}";
// With multiline=false, ^ should not match beginning of each line
const result = validateUserCode(code, {
validations: [{ type: "regex", value: "^\\s*color", options: { multiline: false } }]
});
expect(result.isValid).toBe(false);
});
test("regex_MultilineTrue_Default_MatchesEachLine", () => {
const code = "body {\n color: red;\n}";
const result = validateUserCode(code, {
validations: [{ type: "regex", value: "^\\s*color", options: { multiline: true } }]
});
expect(result.isValid).toBe(true);
});
test("regex_InvalidPattern_ReturnsFalse", () => {
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
const result = validateUserCode("color: red;", {
validations: [{ type: "regex", value: "[invalid(regex" }]
});
expect(result.isValid).toBe(false);
expect(errorSpy).toHaveBeenCalled();
errorSpy.mockRestore();
});
test("regex_EmptyPattern_MatchesEverything", () => {
const result = validateUserCode("color: red;", {
validations: [{ type: "regex", value: "" }]
});
expect(result.isValid).toBe(true);
});
});
describe("CSS validation - propertyValueValidation edge cases", () => {
test("propertyValue_PropertyNotFound_Fails", () => {
const result = validateUserCode("color: red;", {
validations: [
{
type: "property_value",
value: { property: "display", expected: "flex" }
}
]
});
expect(result.isValid).toBe(false);
});
test("propertyValue_ExactMatch_Passes", () => {
const result = validateUserCode("display: flex;", {
validations: [
{
type: "property_value",
value: { property: "display", expected: "flex" },
options: { exact: true }
}
]
});
expect(result.isValid).toBe(true);
});
test("propertyValue_ExactMatch_CaseMismatch_Fails", () => {
const result = validateUserCode("display: FLEX;", {
validations: [
{
type: "property_value",
value: { property: "display", expected: "flex" },
options: { exact: true }
}
]
});
expect(result.isValid).toBe(false);
});
test("propertyValue_FlexibleMatch_CaseInsensitive", () => {
const result = validateUserCode("display: FLEX;", {
validations: [
{
type: "property_value",
value: { property: "display", expected: "flex" }
}
]
});
expect(result.isValid).toBe(true);
});
test("propertyValue_ShorthandProperty_Passes", () => {
const result = validateUserCode("margin: 10px 20px;", {
validations: [
{
type: "property_value",
value: { property: "margin", expected: "10px 20px" }
}
]
});
expect(result.isValid).toBe(true);
});
test("propertyValue_DefaultMessage_IncludesPropertyAndExpected", () => {
const result = validateUserCode("color: red;", {
validations: [
{
type: "property_value",
value: { property: "display", expected: "flex" }
}
]
});
expect(result.isValid).toBe(false);
expect(result.message).toContain("display");
expect(result.message).toContain("flex");
});
});
describe("CSS validation - syntaxValidation", () => {
test("syntax_ValidCss_Passes", () => {
const result = validateUserCode("div { color: red; }", {
validations: [{ type: "syntax" }]
});
expect(result.isValid).toBe(true);
});
});
describe("CSS validation - custom edge cases", () => {
test("custom_NoValidatorFunction_ReturnsEarlyWithOriginalResult", () => {
const result = validateUserCode("color: red;", {
validations: [{ type: "custom" }]
});
// When validator is falsy, validationPassed stays false, but result.isValid was never set to false
// The function returns early with the unmodified result (isValid: true)
expect(result.isValid).toBe(true);
});
test("custom_NonFunctionValidator_ReturnsEarlyWithOriginalResult", () => {
const result = validateUserCode("color: red;", {
validations: [{ type: "custom", validator: "not-a-function" }]
});
// Same behavior: validator check fails, validationPassed stays false, returns unmodified result
expect(result.isValid).toBe(true);
});
test("custom_ValidatorReturnsNoMessage_UsesMessage", () => {
const result = validateUserCode("color: red;", {
validations: [
{
type: "custom",
validator: () => ({ isValid: false }),
message: "Fallback message"
}
]
});
expect(result.isValid).toBe(false);
expect(result.message).toBe("Fallback message");
});
test("custom_ValidatorReturnsNoMessage_NoLessonMessage_DefaultMessage", () => {
const result = validateUserCode("color: red;", {
validations: [
{
type: "custom",
validator: () => ({ isValid: false })
}
]
});
expect(result.isValid).toBe(false);
expect(result.message).toContain("does not meet the requirements");
});
});
describe("CSS validation - unknown type", () => {
test("unknownType_WarnsAndContinues", () => {
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const result = validateUserCode("color: red;", {
validations: [
{ type: "invented_type", value: "anything" },
{ type: "contains", value: "color: red" }
]
});
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("Unknown validation type"));
// The unknown type is skipped (continue), then the next validation passes
expect(result.isValid).toBe(true);
warnSpy.mockRestore();
});
});
describe("CSS validation - empty and whitespace input", () => {
test("emptyString_ContainsValidation_Fails", () => {
const result = validateUserCode("", {
validations: [{ type: "contains", value: "color" }]
});
expect(result.isValid).toBe(false);
});
test("whitespaceOnly_ContainsValidation_Fails", () => {
const result = validateUserCode(" \n\t ", {
validations: [{ type: "contains", value: "color" }]
});
expect(result.isValid).toBe(false);
});
test("emptyString_NotContains_Passes", () => {
const result = validateUserCode("", {
validations: [{ type: "not_contains", value: "color" }]
});
expect(result.isValid).toBe(true);
});
});
describe("CSS validation - validCases and totalCases tracking", () => {
test("allPassingValidations_ValidCasesEqualsTotalCases", () => {
const result = validateUserCode("display: flex; color: red;", {
validations: [
{ type: "contains", value: "display: flex" },
{ type: "contains", value: "color: red" }
]
});
expect(result.isValid).toBe(true);
expect(result.validCases).toBe(2);
expect(result.totalCases).toBe(2);
});
test("firstValidationFails_ValidCasesIs0", () => {
const result = validateUserCode("color: red;", {
validations: [
{ type: "contains", value: "display: flex" },
{ type: "contains", value: "color: red" }
]
});
expect(result.isValid).toBe(false);
expect(result.validCases).toBe(0);
expect(result.totalCases).toBe(2);
});
test("secondValidationFails_ValidCasesIs1", () => {
const result = validateUserCode("display: flex;", {
validations: [
{ type: "contains", value: "display: flex" },
{ type: "contains", value: "color: red" }
]
});
expect(result.isValid).toBe(false);
expect(result.validCases).toBe(1);
expect(result.totalCases).toBe(2);
});
});
describe("CSS validation - special regex metacharacters in contains", () => {
test("contains_DotInValue_TreatedAsLiteral", () => {
// ".class" should match literally, not any char + "class"
const result = validateUserCode(".card { color: red; }", {
validations: [{ type: "contains", value: ".card" }]
});
expect(result.isValid).toBe(true);
});
test("contains_BracketsInValue_TreatedAsLiteral", () => {
const result = validateUserCode("content: '[test]';", {
validations: [{ type: "contains", value: "[test]" }]
});
expect(result.isValid).toBe(true);
});
});
describe("HTML validation - deeply nested parent_child", () => {
test("parentChild_DeeplyNested_Passes", () => {
const html = "<div><section><article><p>Deep</p></article></section></div>";
const result = validateUserCode(html, {
mode: "html",
validations: [{ type: "parent_child", value: { parent: "div", child: "p" } }]
});
expect(result.isValid).toBe(true);
});
});
describe("HTML validation - validCases tracking", () => {
test("htmlAllPass_ValidCasesEqualsTotal", () => {
const result = validateUserCode("<h1>Title</h1><p>Content</p>", {
mode: "html",
validations: [
{ type: "element_exists", value: "h1" },
{ type: "element_exists", value: "p" }
]
});
expect(result.isValid).toBe(true);
expect(result.validCases).toBe(2);
expect(result.totalCases).toBe(2);
});
test("htmlPartialPass_EarlyReturn", () => {
const result = validateUserCode("<h1>Title</h1>", {
mode: "html",
validations: [
{ type: "element_exists", value: "h1" },
{ type: "element_exists", value: "p", message: "Need paragraph" }
]
});
expect(result.isValid).toBe(false);
expect(result.validCases).toBe(1);
expect(result.message).toBe("Need paragraph");
});
});
});

View File

@@ -226,6 +226,86 @@ describe("CSS Validator", () => {
});
});
describe("JavaScript Validator", () => {
describe("validateUserCode with mode: javascript", () => {
it("should validate contains correctly for JavaScript", () => {
const userCode = 'const name = "Alice";';
const lesson = {
mode: "javascript",
validations: [{ type: "contains", value: "const", message: "Use const" }]
};
const result = validateUserCode(userCode, lesson);
expect(result.isValid).toBe(true);
expect(result.validCases).toBe(1);
});
it("should validate regex correctly for JavaScript", () => {
const userCode = 'const name = "Alice";';
const lesson = {
mode: "javascript",
validations: [{ type: "regex", value: 'const\\s+name\\s*=', message: "Declare name" }]
};
const result = validateUserCode(userCode, lesson);
expect(result.isValid).toBe(true);
});
it("should validate not_contains correctly for JavaScript", () => {
const userCode = 'const name = "Alice";';
const lesson = {
mode: "javascript",
validations: [{ type: "not_contains", value: "var", message: "Do not use var" }]
};
const result = validateUserCode(userCode, lesson);
expect(result.isValid).toBe(true);
const failCode = 'var name = "Alice";';
const failResult = validateUserCode(failCode, lesson);
expect(failResult.isValid).toBe(false);
expect(failResult.message).toBe("Do not use var");
});
it("should return invalid for missing code", () => {
const userCode = "";
const lesson = {
mode: "javascript",
validations: [{ type: "contains", value: "const", message: "Use const" }]
};
const result = validateUserCode(userCode, lesson);
expect(result.isValid).toBe(false);
});
it("should pass with no validations", () => {
const userCode = 'const x = 1;';
const lesson = { mode: "javascript" };
const result = validateUserCode(userCode, lesson);
expect(result.isValid).toBe(true);
expect(result.message).toContain("No validations specified");
});
it("should handle multiple validations with early return on failure", () => {
const userCode = 'const name = "Alice";';
const lesson = {
mode: "javascript",
validations: [
{ type: "contains", value: "const", message: "Use const" },
{ type: "contains", value: "let", message: "Use let" },
{ type: "contains", value: "name", message: "Declare name" }
]
};
const result = validateUserCode(userCode, lesson);
expect(result.isValid).toBe(false);
expect(result.message).toBe("Use let");
expect(result.validCases).toBe(1);
});
});
});
describe("HTML Validator", () => {
describe("validateUserCode with mode: html", () => {
it("should validate element_exists correctly", () => {

376
wave.yaml Normal file
View File

@@ -0,0 +1,376 @@
adapters:
claude:
binary: claude
default_permissions:
allowed_tools:
- Read
- Write
- Edit
- Bash
deny: []
mode: headless
output_format: json
project_files:
- CLAUDE.md
- .claude/settings.json
apiVersion: v1
kind: WaveManifest
metadata:
description: An interactive platform for learning CSS through practical challenges
name: code-crispies
ontology:
telos: Interactive self-learning platform for web technologies (CSS, HTML, JavaScript, Markdown)
personas:
auditor:
adapter: claude
description: Security review and quality assurance
model: claude-haiku
permissions:
allowed_tools:
- Read
- Write
- Grep
- Glob
- Bash
deny:
- Edit(*)
- Bash(rm -rf /*)
- Bash(git push*)
- Bash(git commit*)
system_prompt_file: .wave/personas/auditor.md
temperature: 0.1
craftsman:
adapter: claude
description: Code implementation and testing
model: claude-opus
permissions:
allowed_tools:
- Read
- Write
- Edit
- Bash
deny:
- Bash(rm -rf /*)
system_prompt_file: .wave/personas/craftsman.md
temperature: 0.7
debugger:
adapter: claude
description: Systematic debugging and root cause analysis
model: claude-opus
permissions:
allowed_tools:
- Read
- Write
- Glob
- Grep
- Bash
deny:
- Edit(*)
- Bash(rm -rf /*)
- Bash(git push*)
- Bash(git commit*)
system_prompt_file: .wave/personas/debugger.md
temperature: 0.1
gitea-analyst:
adapter: claude
description: Gitea issue analysis and scanning
permissions:
allowed_tools:
- Read
- Write
- Bash(tea issues view*)
- Bash(tea issues list*)
- Bash(tea releases list*)
- Bash(tea pulls view*)
- Bash(tea pulls list*)
- Bash(tea --version)
- Bash(git log*)
- Bash(git status*)
- Bash(ls *)
deny:
- Bash(tea issues edit*)
- Bash(tea issues create*)
- Bash(tea issues close*)
- Bash(gh *)
- Bash(glab *)
- Edit(*)
system_prompt_file: .wave/personas/gitea-analyst.md
temperature: 0.1
gitea-commenter:
adapter: claude
description: Posts comments on Gitea issues and pull requests
permissions:
allowed_tools:
- Read
- Write
- Bash(tea issues comment*)
- Bash(tea pulls create*)
- Bash(tea --version)
- Bash(git push*)
- Bash(git status*)
- Bash(git log*)
- Bash(git remote*)
- Bash(git diff*)
deny:
- Bash(tea issues edit*)
- Bash(tea issues close*)
- Bash(tea pulls merge*)
- Bash(tea pulls close*)
- Bash(gh *)
- Bash(glab *)
- Edit(*)
system_prompt_file: .wave/personas/gitea-commenter.md
temperature: 0.2
gitea-enhancer:
adapter: claude
description: Gitea issue enhancement and improvement
permissions:
allowed_tools:
- Read
- Write
- Bash(tea issues edit*)
- Bash(tea issues view*)
- Bash(tea --version)
deny:
- Bash(tea issues create*)
- Bash(tea issues close*)
- Bash(gh *)
- Bash(glab *)
- Edit(*)
system_prompt_file: .wave/personas/gitea-enhancer.md
temperature: 0.2
gitea-scoper:
adapter: claude
description: Gitea epic analysis, decomposition, and sub-issue creation
permissions:
allowed_tools:
- Read
- Write
- Bash(tea issues create*)
- Bash(tea issues view*)
- Bash(tea issues list*)
- Bash(tea --version)
deny:
- Bash(tea issues edit*)
- Bash(tea issues close*)
- Bash(gh *)
- Bash(glab *)
- Edit(*)
system_prompt_file: .wave/personas/gitea-scoper.md
temperature: 0.1
implementer:
adapter: claude
description: Execution specialist for code changes and structured output
model: claude-opus
permissions:
allowed_tools:
- Read
- Write
- Edit
- Bash
- Glob
- Grep
deny:
- Bash(rm -rf /*)
- Bash(sudo *)
system_prompt_file: .wave/personas/implementer.md
temperature: 0.3
navigator:
adapter: claude
description: Read-only codebase exploration and analysis
model: claude-haiku
permissions:
allowed_tools:
- Read
- Write
- Glob
- Grep
- Bash(git log*)
- Bash(git status*)
deny:
- Edit(*)
- Bash(git commit*)
- Bash(git push*)
system_prompt_file: .wave/personas/navigator.md
temperature: 0.1
philosopher:
adapter: claude
description: Architecture design and specification
model: claude-opus
permissions:
allowed_tools:
- Read
- Write
- Edit
- Bash
- Glob
- Grep
deny: []
system_prompt_file: .wave/personas/philosopher.md
temperature: 0.3
planner:
adapter: claude
description: Task breakdown and planning
model: claude-haiku
permissions:
allowed_tools:
- Read
- Write
- Edit
- Bash
- Glob
- Grep
deny: []
system_prompt_file: .wave/personas/planner.md
temperature: 0.2
provocateur:
adapter: claude
description: Creative challenger for divergent thinking and complexity hunting
model: claude-opus
permissions:
allowed_tools:
- Read
- Write
- Glob
- Grep
- Bash(wc *)
- Bash(git log*)
- Bash(git diff*)
- Bash(find*)
- Bash(ls*)
deny:
- Edit(*)
- Bash(git commit*)
- Bash(git push*)
- Bash(rm*)
system_prompt_file: .wave/personas/provocateur.md
temperature: 0.8
researcher:
adapter: claude
description: Deep codebase research and analysis
model: claude-opus
permissions:
allowed_tools:
- Read
- Write
- Edit
- Bash
- Glob
- Grep
- WebSearch
- WebFetch
deny: []
system_prompt_file: .wave/personas/researcher.md
temperature: 0.1
reviewer:
adapter: claude
description: Code review and quality checks
permissions:
allowed_tools:
- Read
- Write
- Glob
- Grep
- Bash
deny:
- Write(*.go)
- Write(*.ts)
- Write(*.py)
- Write(*.rs)
- Edit(*)
- Bash(rm *)
- Bash(git push*)
- Bash(git commit*)
system_prompt_file: .wave/personas/reviewer.md
temperature: 0.1
summarizer:
adapter: claude
description: Context compaction for relay handoffs
model: claude-haiku
permissions:
allowed_tools:
- Read
- Write
- Edit
- Bash
- Glob
- Grep
deny: []
system_prompt_file: .wave/personas/summarizer.md
temperature: 0
supervisor:
adapter: claude
description: Work supervision and quality evaluation
model: claude-opus
permissions:
allowed_tools:
- Read
- Write
- Glob
- Grep
- Bash
deny:
- Edit(*)
- Bash(git push*)
- Bash(git commit*)
- Bash(rm*)
system_prompt_file: .wave/personas/supervisor.md
temperature: 0.2
synthesizer:
adapter: claude
description: Structured synthesis of analysis findings into actionable JSON proposals
permissions:
allowed_tools:
- Read
- Write
- Edit
- Bash
- Glob
- Grep
deny: []
system_prompt_file: .wave/personas/synthesizer.md
temperature: 0.2
validator:
adapter: claude
description: Skeptical analysis and verification of findings against source code
permissions:
allowed_tools:
- Read
- Write
- Glob
- Grep
- Bash(wc *)
- Bash(git log*)
- Bash(git diff*)
deny:
- Edit(*)
- Bash(git commit*)
- Bash(git push*)
- Bash(rm*)
system_prompt_file: .wave/personas/validator.md
temperature: 0.1
project:
build_command: npm run build
flavour: node
format_command: npm run format
language: javascript
lint_command: ""
skill: javascript
source_glob: '*.{js,jsx,ts,tsx}'
test_command: npm test
runtime:
audit:
log_all_file_operations: false
log_all_tool_calls: true
log_dir: .wave/traces/
default_timeout_minutes: 30
max_concurrent_workers: 5
meta_pipeline:
max_depth: 2
max_total_steps: 20
max_total_tokens: 500000
timeout_minutes: 60
relay:
strategy: summarize_to_checkpoint
token_threshold_percent: 80
workspace_root: .wave/workspaces