0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
126 lines
4.3 KiB
Python
126 lines
4.3 KiB
Python
"""Regression tests for two CodeAwareCompressor AST-reassembly bugs found
|
|
while investigating a reported Go brace-duplication issue.
|
|
|
|
1. `export` keyword duplication (TS/JS): `_compress_function_ast` and
|
|
`_compress_class_ast` use LINE-based slicing (not byte-offset) to
|
|
preserve indentation for nested definitions. When a function/class shares
|
|
its first line with a preceding sibling — e.g. the `export` keyword in
|
|
`export function foo() {`, a sibling of the function inside
|
|
`export_statement`, not part of the function node itself — a naive
|
|
full-line slice pulled that sibling's text in too. The `export_statement`
|
|
handler then re-prepended the same `export` text, producing
|
|
`export export function foo() {` (invalid syntax, silently discarded by
|
|
the `_verify_syntax` fallback).
|
|
2. Doc-comment displacement (all languages): doc comments are top-level
|
|
siblings of the declaration they document, not children of it. Left
|
|
unattached during AST extraction, they fell through to a "leftover
|
|
top-level code" bucket that `_assemble_compressed` emits as one block
|
|
after every function signature — detaching every doc comment from what it
|
|
documents and dumping them all at the end of the file.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from headroom.transforms.code_compressor import (
|
|
CodeAwareCompressor,
|
|
CodeCompressorConfig,
|
|
CodeLanguage,
|
|
_check_tree_sitter_available,
|
|
)
|
|
|
|
pytestmark = pytest.mark.skipif(
|
|
not _check_tree_sitter_available(),
|
|
reason="tree-sitter not installed (pip install headroom-ai[code])",
|
|
)
|
|
|
|
TS_EXPORTED = """export interface User {
|
|
id: string;
|
|
name: string;
|
|
}
|
|
|
|
/**
|
|
* Fetches a user by id.
|
|
*/
|
|
export function getUser(id: string): User {
|
|
return { id, name: "test" };
|
|
}
|
|
|
|
/**
|
|
* Greets a user by name.
|
|
*/
|
|
export function greet(user: User): string {
|
|
return `Hello, ${user.name}!`;
|
|
}
|
|
|
|
export class UserStore {
|
|
private users: User[] = [];
|
|
|
|
add(user: User): void {
|
|
this.users.push(user);
|
|
}
|
|
}
|
|
"""
|
|
|
|
GO_DOC_COMMENTS = """package main
|
|
|
|
import "fmt"
|
|
|
|
// Add adds two integers together and returns the sum.
|
|
func Add(a, b int) int {
|
|
\treturn a + b
|
|
}
|
|
|
|
// Greet returns a friendly greeting for the given name.
|
|
func Greet(name string) string {
|
|
\treturn fmt.Sprintf("Hello, %s!", name)
|
|
}
|
|
"""
|
|
|
|
|
|
def _compress_ast(code: str, language: CodeLanguage):
|
|
compressor = CodeAwareCompressor(CodeCompressorConfig())
|
|
compressed, _, _ = compressor._compress_with_ast(code, language, "", None)
|
|
return compressor, compressed
|
|
|
|
|
|
def test_ts_export_keyword_not_duplicated() -> None:
|
|
"""`export function`/`export class` must not become `export export ...`."""
|
|
compressor, compressed = _compress_ast(TS_EXPORTED, CodeLanguage.TYPESCRIPT)
|
|
|
|
assert "export export" not in compressed, compressed
|
|
assert compressor._verify_syntax(compressed, CodeLanguage.TYPESCRIPT) is True
|
|
|
|
|
|
def test_ts_doc_comments_stay_attached_to_declaration() -> None:
|
|
"""A `/** ... */` doc comment must stay immediately before the function it
|
|
documents, not get dumped in a cluster at the end of the output."""
|
|
_, compressed = _compress_ast(TS_EXPORTED, CodeLanguage.TYPESCRIPT)
|
|
|
|
assert "/**\n * Fetches a user by id.\n */\nexport function getUser" in compressed
|
|
assert "/**\n * Greets a user by name.\n */\nexport function greet" in compressed
|
|
|
|
|
|
def test_go_doc_comments_stay_attached_to_function() -> None:
|
|
"""Same doc-comment-attachment bug, Go's `//` line-comment form."""
|
|
compressor, compressed = _compress_ast(GO_DOC_COMMENTS, CodeLanguage.GO)
|
|
|
|
assert "// Add adds two integers together and returns the sum.\nfunc Add" in compressed
|
|
assert "// Greet returns a friendly greeting for the given name.\nfunc Greet" in compressed
|
|
assert compressor._verify_syntax(compressed, CodeLanguage.GO) is True
|
|
|
|
|
|
def test_actual_typescript_compression() -> None:
|
|
"""Parity with the existing JS/Python/Go 'actual compression' tests —
|
|
real TS input must actually compress, not silently no-op."""
|
|
config = CodeCompressorConfig(min_tokens_for_compression=10, enable_ccr=False)
|
|
compressor = CodeAwareCompressor(config)
|
|
code = TS_EXPORTED * 3 # large enough to trigger body elision
|
|
|
|
result = compressor.compress(code, language="typescript")
|
|
|
|
assert result.compression_ratio < 1.0
|
|
assert result.syntax_valid is True
|
|
assert result.language == CodeLanguage.TYPESCRIPT
|