Skip to content

feat(core): add math operators and layout composition tests for Dim Algebra#2330

Open
suhaniiz wants to merge 1 commit into
Karanjot786:mainfrom
suhaniiz:feat/dim-algebra-operators
Open

feat(core): add math operators and layout composition tests for Dim Algebra#2330
suhaniiz wants to merge 1 commit into
Karanjot786:mainfrom
suhaniiz:feat/dim-algebra-operators

Conversation

@suhaniiz

@suhaniiz suhaniiz commented Jul 10, 2026

Copy link
Copy Markdown

Description

This PR introduces core mathematical chaining utilities (.add(), .sub()) and composition strategies (Dim.max()) to the Dimension Algebra framework. It ensures complex terminal UI constraints can be cleanly calculated in a declarative way while preserving complete architectural tracking over underlying layout context dependencies.

Related Issue

Closes #2327

Which package(s)?

@termuijs/core

Type of Change

  • 🐛 Bug fix (type:bug)
  • ✨ Feature (type:feature)
  • 📝 Docs (type:docs)
  • 🧪 Tests (type:testing)
  • ♻️ Refactor (type:refactor)

Checklist

  • ⭐ You starred the repo. The needs-star check blocks your merge otherwise.
  • Tests pass locally: bun vitest run
  • Build passes: bun run build
  • Typecheck passes: bun run typecheck
  • Your PR title follows type: short description.

GSSoC 2026 Participation

  • You are a GSSoC 2026 contributor.

Screenshots / Recordings (UI changes)

Notes for the Reviewer

Added complete verification coverage within dim.test.ts ensuring nested dimension operations correctly combine internal lambda evaluations and seamlessly flatten their aggregate context dependency arrays ('parentSize', 'contentSize').

Summary by CodeRabbit

  • Tests
    • Added coverage for chained dimension arithmetic and composition.
    • Added validation for correct dimension evaluation and dependency tracking.

@suhaniiz suhaniiz requested a review from Karanjot786 as a code owner July 10, 2026 14:38
@github-actions github-actions Bot added type:feature +10 pts. New feature. needs-star PR author has not starred the repo. labels Jul 10, 2026
@github-actions

Copy link
Copy Markdown

Hi @suhaniiz 👋

Star this repo before your PR merges.

Why? GSSoC 2026 contributors who star get priority review and points credit. After you star, push any commit (or re-run this check). The needs-star label lifts automatically.

Thanks for your contribution to TermUI.

@github-actions github-actions Bot added area:core @termuijs/core type:testing +10 pts. Tests. and removed type:feature +10 pts. New feature. needs-star PR author has not starred the repo. labels Jul 10, 2026
@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a Vitest case covering chained Dim arithmetic, Dim.max composition, evaluation results, and dependency aggregation.

Changes

Dim algebra validation

Layer / File(s) Summary
Chained and composed dimension tests
packages/core/src/layout/dim.test.ts
Verifies sub, add, and Dim.max evaluation results and confirms dependency propagation for composed expressions.

Estimated code review effort: 1 (Trivial) | ~5 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR appears to add tests only, but issue #2327 requires implementing Dim composition utilities like .add(), .sub(), and Dim.max(). Add the actual Dim composition methods and dependency-merging logic required by #2327, then keep the tests as verification of the new API.
✅ Passed checks (4 passed)
Check name Status Explanation
Out of Scope Changes check ✅ Passed The change stays within the Dim algebra area and adds only targeted tests, with no unrelated refactors or extra subsystems touched.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title is concise and accurately summarizes the Dim algebra chaining and composition additions.
Description check ✅ Passed The description covers the change, linked issue, package, type, checklist, and reviewer notes, with only optional screenshot content left empty.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the type:feature +10 pts. New feature. label Jul 10, 2026
@suhaniiz

Copy link
Copy Markdown
Author

hey @Karanjot786 , this pr is under GSSoC 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/core/src/layout/dim.test.ts (1)

53-56: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider asserting the full merged dependency array for stricter coverage.

The test uses toContain for each dependency, which only verifies presence of individual tokens. Since the PR objective emphasizes "combining underlying dimension dependency arrays," asserting the exact merged array (e.g., toEqual(['parentSize']) for fillMinusMargin) would catch duplicate entries, missing tokens, or unexpected extra dependencies from the composition logic.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/core/src/layout/dim.test.ts` around lines 53 - 56, Update the
dependency assertions in the automated dependency merging tests for
fillMinusMargin, autoPlusOffset, and responsiveWidth to use toEqual with their
complete expected dependency arrays instead of toContain, verifying no
duplicates, omissions, or unexpected dependencies.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/src/layout/dim.test.ts`:
- Around line 40-57: Implement the missing Dim.add, Dim.sub, and Dim.max
combinators in the Dim class alongside auto(), fill(), and func(). Ensure
add/sub support chaining and evaluate operands using the layout context, while
max combines multiple Dim expressions and returns the greatest evaluated value;
preserve and merge dependency flags from all operands.

---

Nitpick comments:
In `@packages/core/src/layout/dim.test.ts`:
- Around line 53-56: Update the dependency assertions in the automated
dependency merging tests for fillMinusMargin, autoPlusOffset, and
responsiveWidth to use toEqual with their complete expected dependency arrays
instead of toContain, verifying no duplicates, omissions, or unexpected
dependencies.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 82b782dd-7afa-4ca2-98a5-5eb7975e0db6

📥 Commits

Reviewing files that changed from the base of the PR and between 65d1f7e and e402b0b.

📒 Files selected for processing (1)
  • packages/core/src/layout/dim.test.ts

Comment on lines +40 to +57

it('Dim algebra engine supports chaining math operators and composition properties', () => {
// 1. Chaining Math Operators
const fillMinusMargin = Dim.fill(2).sub(5);
const autoPlusOffset = Dim.auto().add(10);

expect(fillMinusMargin.evaluate(mockCtx)).toBe(93); // (100 - 2) - 5
expect(autoPlusOffset.evaluate(mockCtx)).toBe(20); // 10 + 10

// 2. Layout Composition Utilities
const responsiveWidth = Dim.max(Dim.auto(), Dim.func(ctx => ctx.parentWidth * 0.3));
expect(responsiveWidth.evaluate(mockCtx)).toBe(30); // max(10, 30)

// 3. Automated Dependency Flag Merging
expect(fillMinusMargin.dependencies()).toContain('parentSize');
expect(autoPlusOffset.dependencies()).toContain('contentSize');
expect(responsiveWidth.dependencies()).toContain('contentSize');
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify .sub(), .add(), Dim.max() exist in dim.ts
rg -n '\b(sub|add|max)\b' packages/core/src/layout/dim.ts --type=ts -C2

# Verify mockCtx fields in the test file
sed -n '1,39p' packages/core/src/layout/dim.test.ts

Repository: Karanjot786/TermUI

Length of output: 1538


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== outline: packages/core/src/layout/dim.ts =="
ast-grep outline packages/core/src/layout/dim.ts --view expanded || true

echo
echo "== dim.ts relevant contents =="
wc -l packages/core/src/layout/dim.ts
sed -n '1,220p' packages/core/src/layout/dim.ts

echo
echo "== dim.test.ts relevant contents =="
wc -l packages/core/src/layout/dim.test.ts
sed -n '1,120p' packages/core/src/layout/dim.test.ts

Repository: Karanjot786/TermUI

Length of output: 4450


Implement Dim.add/Dim.sub/Dim.max before this test lands
packages/core/src/layout/dim.ts only exposes auto(), fill(), and func(), so this test will fail until those combinators exist.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/core/src/layout/dim.test.ts` around lines 40 - 57, Implement the
missing Dim.add, Dim.sub, and Dim.max combinators in the Dim class alongside
auto(), fill(), and func(). Ensure add/sub support chaining and evaluate
operands using the layout context, while max combines multiple Dim expressions
and returns the greatest evaluated value; preserve and merge dependency flags
from all operands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core @termuijs/core type:feature +10 pts. New feature. type:testing +10 pts. Tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature] (core): Add addition, subtraction, and min/max composition utilities to Dim Algebra

1 participant