Skip to content

A new test framwork with a ULSP test suite - #540

Draft
Jafaral wants to merge 5 commits into
uniconproject:masterfrom
Jafaral:ulsp-testing
Draft

Jafaral wants to merge 5 commits into
uniconproject:masterfrom
Jafaral:ulsp-testing

Conversation

@Jafaral

@Jafaral Jafaral commented Nov 24, 2025 •

Copy link
Copy Markdown
Member

Add a complete test suite for the Unicon Language Server Protocol (ULSP)
implementation with four main test categories:

  1. JSON-RPC Tests (jsonrpc_test.icn)

    • Tests JSON-RPC protocol with calculator server
    • Validates concurrent request handling
    • Tests basic arithmetic operations
  2. LSP Message Tests (lsp_message_test.icn)

    • Tests JSON-RPC message parsing and validation
    • Tests all LSP request/response message types
    • Tests notification handling
    • Tests error handling and malformed message rejection
  3. LSP Feature Tests (lsp_feature_test.icn)

    • Tests LSP features with realistic Unicon code
    • Tests completion, hover, definition, symbols, formatting, folding
    • Tests signature help functionality
    • Uses LSPDB for symbol information
  4. LSP Protocol Tests (lsp_protocol_test.icn)

    • Integration tests with actual server-client communication
    • Tests server initialization and capability negotiation
    • Tests document synchronization (didOpen, didChange, didSave)
    • Tests end-to-end LSP feature workflows

Unified Test Runner (lsp_run_tests.icn):

  • Executes all four test suites sequentially
  • Aggregates test counts from all suites
  • Provides comprehensive test reporting
  • Supports individual test suite execution

Test Framework Features:

  • Expected result capture mode with JSON output
  • Deep comparison for expected vs actual results
  • JSON-formatted error reporting
  • Consistent test structure across all test files
  • Proper Unicon success/failure semantics

Makefile Integration:

  • Individual test targets for each test suite
  • Unified test runner target
  • Clean targets for test artifacts

All test files follow consistent structure:

  • Test class with run_all_tests() method
  • Global counters for test tracking
  • Standalone execution support via $ifdef STANDALONE

@Jafaral
Jafaral force-pushed the ulsp-testing branch 3 times, most recently from ab4b7ac to a5847c9 Compare November 24, 2025 17:16
@Jafaral
Jafaral requested review from Don-Ward and cjeffery November 24, 2025 17:45
@Don-Ward

Copy link
Copy Markdown
Collaborator

Perhaps it makes sense to defer a review until the CI runs without errors -- either because the underlying cause of the CI failures have been fixed, or the ULSP tests are explicitly disabled for the configurations that currently fail.

@Jafaral
Jafaral marked this pull request as draft December 1, 2025 06:30
@Jafaral Jafaral changed the title lib,ulsp: jsonrpc and ulsp fixes with a ULSP test suite A new test framwork with a ULSP test suite Dec 1, 2025
@Jafaral
Jafaral force-pushed the ulsp-testing branch 2 times, most recently from 58306ef to 60be717 Compare December 7, 2025 18:34
Add a complete test suite for the Unicon Language Server Protocol (ULSP)
implementation with four main test categories:

1. JSON-RPC Tests (jsonrpc_test.icn)
   - Tests JSON-RPC protocol with calculator server
   - Validates concurrent request handling
   - Tests basic arithmetic operations

2. LSP Message Tests (lsp_message_test.icn)
   - Tests JSON-RPC message parsing and validation
   - Tests all LSP request/response message types
   - Tests notification handling
   - Tests error handling and malformed message rejection

3. LSP Feature Tests (lsp_feature_test.icn)
   - Tests LSP features with realistic Unicon code
   - Tests completion, hover, definition, symbols, formatting, folding
   - Tests signature help functionality
   - Uses LSPDB for symbol information

4. LSP Protocol Tests (lsp_protocol_test.icn)
   - Integration tests with actual server-client communication
   - Tests server initialization and capability negotiation
   - Tests document synchronization (didOpen, didChange, didSave)
   - Tests end-to-end LSP feature workflows

Unified Test Runner (lsp_run_tests.icn):
- Executes all four test suites sequentially
- Aggregates test counts from all suites
- Provides comprehensive test reporting
- Supports individual test suite execution

Test Framework Features:
- Expected result capture mode with JSON output
- Deep comparison for expected vs actual results
- JSON-formatted error reporting
- Consistent test structure across all test files
- Proper Unicon success/failure semantics

Makefile Integration:
- Individual test targets for each test suite
- Unified test runner target
- Clean targets for test artifacts

All test files follow consistent structure:
- Test class with run_all_tests() method
- Global counters for test tracking
- Standalone execution support via $ifdef STANDALONE
Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
@greptile-apps

greptile-apps Bot commented Mar 6, 2026 •

Copy link
Copy Markdown

Greptile Summary

This PR introduces a comprehensive ULSP (Unicon Language Server Protocol) test framework consisting of a reusable UniconTest/UniconTestSuite base library (uni/lib/testsuite.icn), four specialised test suites (test_jsonrpc, test_lsp_message, test_lsp_feature, test_lsp_protocol), a unified runner (lsp_test_suite), Makefile integration, and documentation. It also migrates the old inline uni/ulsp/jsonrpc-test.icn into the new framework and applies whitespace / robustness fixes to ipl/procs/paths.icn.

Key issues found:

  • Critical — deep_compare is broken for tables (test_lsp_protocol.icn): !table in Unicon generates values, not keys. Every table-equality check in deep_compare compares values against keys, producing wrong results. key(T) should be used instead.
  • Critical — add_test_subsection uses a non-standard get() call (testsuite.icn): get(L, n) is not a valid Unicon call; get(L) removes the first element. The intent (read/update the last element) requires self.section_stack[*self.section_stack].
  • Build breakage — missing distclean target (tests/ulsp/Makefile): tests/Makefile calls $(MAKE) -C ulsp distclean but the target doesn't exist in the ULSP Makefile.
  • Semantic change in ipl/procs/paths.icn (ulibprefix()): The function previously returned &null (success) when no prefix was found; it now fails. Callers such as ulibpaths() that expected a null return will silently cascade-fail.
  • _exec_cmd has an undeclared local, debug output, and is dead code (testsuite.icn): The variable f is not declared local (becomes global); write(image(f)) is debug output; the method is never called.
  • --loglevel 6 passed as one string (test_lsp_protocol.icn): When system() receives a list, each element is a separate argv. The combined "--loglevel 6" string will not be split into two arguments by the OS argument parser.
  • disconnect() commented out (test_lsp_protocol.icn): After shutdown/exit, the client socket is never closed, potentially leaving dangling resources.
  • assert_equal parameter labels are swapped (testsuite.icn): Call-sites pass the computed value first and the constant second, but parameters are named (expected, actual) — verbose-mode failure messages print labels in the wrong order.

Confidence Score: 2/5

  • Not safe to merge as-is: two correctness bugs in the core framework (broken table comparison in deep_compare, non-standard get() in add_test_subsection) and a build-breaking missing distclean target need to be resolved first.
  • The PR introduces a substantial new test framework and test suite but has two logic-level bugs that render the framework partially unreliable (deep_compare always fails for tables, add_test_subsection corrupts the section stack). The missing distclean target causes a hard build failure. The semantic change in paths.icn could silently break library path resolution on systems where the prefix isn't detected. These issues must be resolved before merging.
  • uni/lib/testsuite.icn (framework logic bugs), tests/ulsp/test_lsp_protocol.icn (broken deep_compare, split argument bug), tests/ulsp/Makefile (missing distclean), ipl/procs/paths.icn (semantic change in return behaviour).

Important Files Changed

Filename Overview
uni/lib/testsuite.icn New 805-line test framework (UniconTest + UniconTestSuite + assertion helpers); contains an undeclared local variable f in _exec_cmd, debug output left in production code, a broken add_test_subsection using a non-standard get() call, and swapped assert_equal parameter naming that produces misleading error messages.
tests/ulsp/test_lsp_protocol.icn Integration test file (731 lines) containing a critical bug in deep_compare: !table iterates values rather than keys, making all table-equality checks produce wrong results. Also has a mis-split --loglevel 6 CLI argument and a commented-out disconnect() that leaves sockets open after shutdown.
ipl/procs/paths.icn Whitespace cleanup (tabs→spaces) plus two substantive fixes: null-guarding map() and changing the return of ulibprefix() from returning &null to failing when no prefix is found. The semantic change from returning null to failing may silently break callers (e.g., ulibpaths()) that expected a null return.
tests/ulsp/Makefile New Makefile for ULSP test targets; missing distclean target which is invoked by the parent tests/Makefile, causing make distclean to fail.
tests/ulsp/test_lsp_feature.icn LSP feature tests covering completion, hover, definition, symbols, formatting, folding, and signature help. Uses a shared global feature_test_db to avoid rebuilding the database for each test case. Logic is mostly sound; relies on correct framework assertion helpers.
tests/ulsp/test_lsp_message.icn Unit tests for JSON-RPC message parsing and all LSP request/notification/error message types; straightforward and well-organised, no major logic issues found.
tests/ulsp/test_jsonrpc.icn JSON-RPC calculator server/client integration test migrated from uni/ulsp/jsonrpc-test.icn; adds thread availability guard and proper pass/fail counters; logic looks correct.
tests/ulsp/lsp_test_suite.icn Top-level test runner that wires JSONRPCTest, LSPMessageTest, LSPFeatureTest, and LSPProtocolTest into a UniconTestSuite; straightforward orchestration with proper exit codes.

Sequence Diagram

sequenceDiagram
    participant Main as lsp_test_suite
    participant Suite as UniconTestSuite
    participant JT as JSONRPCTest
    participant MT as LSPMessageTest
    participant FT as LSPFeatureTest
    participant PT as LSPProtocolTest
    participant Srv as ULSP Server
    participant DB as LSPDB

    Main->>Suite: add_test(JSONRPCTest)
    Main->>Suite: add_test(LSPMessageTest)
    Main->>Suite: add_test(LSPFeatureTest)
    Main->>Suite: add_test(LSPProtocolTest)
    Main->>Suite: run_all_tests()

    Suite->>JT: _setup_tests() → register_tests()
    Suite->>MT: _setup_tests() → register_tests()
    Suite->>FT: _setup_tests() → register_tests()
    Suite->>PT: _setup_tests() → register_tests()

    Suite->>JT: _execute() → setup / run / teardown
    JT-->>Suite: pass/fail counts

    Suite->>MT: _execute() → setup / run / teardown
    MT-->>Suite: pass/fail counts

    Suite->>FT: _execute()
    FT->>DB: LSPDB().build()
    DB-->>FT: database ready
    FT->>FT: run test_completion / test_hover / ...
    FT-->>Suite: pass/fail counts

    Suite->>PT: _execute()
    PT->>Srv: system(["ulsp","--socket",port],"nowait")
    note over PT,Srv: delay(5000ms)
    PT->>Srv: connect()
    PT->>Srv: initialize request
    Srv-->>PT: capabilities response
    PT->>Srv: didOpen / didChange / didSave notifications
    PT->>Srv: completion / hover / definition / symbols requests
    Srv-->>PT: responses
    PT->>Srv: shutdown + exit
    PT-->>Suite: pass/fail counts

    Suite-->>Main: aggregated results
    Main->>Main: print_summary() + exit(0/1)
Loading

Comments Outside Diff (7)

  1. tests/ulsp/test_lsp_protocol.icn, line 2494-2502 (link)

    !table generates values, not keys — deep_compare is broken for tables

    In Unicon (as in Icon), !T applied to a table generates the values stored in the table, not the keys. As a result, the variable named key actually holds a value, making member(actual, key) and expected[key] / actual[key] semantically incorrect.

    For example, given expected = table("a" → 1) and actual = table("a" → 1):

    • every key := !expected yields 1 (the value)
    • member(actual, 1) checks if 1 is a key of actual — it isn't — so the comparison incorrectly fails even for equal tables

    The correct way to iterate over keys in Unicon is key(T). The fix should be:

    every key := key(expected) do {
       if not member(actual, key) then fail
       if not deep_compare(actual[key], expected[key]) then fail
    }
    every key := key(actual) do {
       if not member(expected, key) then fail
    }
    

    This same bug applies to both the forward (every key := !expected) and the reverse (every key := !actual) checks on lines 2495–2502.

  2. tests/ulsp/Makefile, line 1022-1023 (link)

    distclean target is missing — make distclean will fail

    tests/Makefile references $(MAKE) -C ulsp distclean, but tests/ulsp/Makefile only defines clean Clean — there is no distclean target. Running make distclean from the top-level tests/ directory will therefore fail with a "No rule to make target distclean" error.

    Add a distclean target to this Makefile:

  3. uni/lib/testsuite.icn, line 3474-3482 (link)

    f is undeclared, debug output left in place, and method appears unused

    Three problems with _exec_cmd:

    1. f is never declared local, so it becomes a global variable — any concurrent test execution or re-entry will corrupt it.
    2. write(image(f)) is debug output that should not remain in production code.
    3. The method _exec_cmd is not called anywhere in this framework or in the test files in this PR. It appears to be dead code.
    method _exec_cmd(cmd)
       local f                          # declare local
       if f := open(cmd || " 2>/dev/null", "pw") then {
          # write(image(f))             # remove debug line
          close(f)
       } else
          write(&errornumber, ":", &errortext)
    end
    

    If this is truly unused, consider removing it entirely to keep the public API clean.

  4. tests/ulsp/test_lsp_protocol.icn, line 2706-2707 (link)

    "--loglevel 6" passed as a single string argument — server may not parse it

    When system() receives a list, each list element is passed as a separate argument to the child process. "--loglevel 6" is a single string element and will be passed as one argument (literally --loglevel 6 with a space inside), rather than two separate arguments --loglevel and 6. Most argument parsers will fail to recognise this.

  5. tests/ulsp/test_lsp_protocol.icn, line 2697-2700 (link)

    Client never disconnects after shutdown — commented-out disconnect() call

    #disconnect() is commented out, so after sending shutdown + exit the socket is never explicitly closed on the client side. This may leave a dangling socket open for the remainder of the test run and could cause resource leaks or interfere with subsequent tests.

    Either uncomment the disconnect() call or document clearly why it is intentionally omitted.

  6. uni/lib/testsuite.icn, line 3181-3189 (link)

    get(self.section_stack, *self.section_stack) is not a valid Unicon call

    In Unicon, get(L) removes and returns the first element of list L; it takes exactly one argument. Passing a second argument is non-standard and the behaviour is undefined.

    The apparent intent is to read the last element (the subsection number just put), increment it, and store it back. That should instead be:

    subsection_num := self.section_stack[*self.section_stack] + 1
    self.section_stack[*self.section_stack] := subsection_num
    

    As written, the call may remove the first (parent) element from the stack and corrupt the hierarchy path for all subsequent nested sections.

  7. uni/lib/testsuite.icn, line 3670-3691 (link)

    assert_equal parameter order is the reverse of convention — error messages will be backwards

    The signature is assert_equal(expected, actual, message), but call-sites throughout the test files place the computed value first and the static constant second, e.g.:

    assert_equal(msg.get_kind(), "request", "Request message kind")
    

    This means in verbose mode the error output will read:

    Expected: request
    Actual:   <computed value>
    

    which is the reverse of what a developer expects. While functionally symmetric for equality tests, the swapped labelling makes failure diagnostics misleading. Consider renaming the parameters to (actual, expected, message) or reversing the call-site convention consistently.

Last reviewed commit: c39c6f9

Comment thread ipl/procs/paths.icn
Comment on lines +42 to +45
# unicon directory

# remove the trailing slash it could be / or \
c := path[-1:0]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Semantic change: ulibprefix() now fails instead of returning &null

The original code unconditionally returned prefix (which could be &null, succeeding with a null value). The new code falls off the end of the procedure when prefix is null — in Unicon this means the procedure fails.

Any caller that relied on getting &null back (e.g., ulibpaths() which does prefix := ulibprefix() and then builds libpath := prefix || "ipl/lib" || ...) will now also silently fail when no prefix is found, potentially causing library path resolution to break silently on systems where the prefix cannot be detected.

If the intent is to fail when no prefix is found, callers such as ulibpaths() should guard against this explicitly with if prefix := ulibprefix() then .... Please confirm this behavior change is intentional and update callers accordingly.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants