open-code-review/internal/session/comments_test.go
kite 533b526b4c
Some checks are pending
CI / cross-compile (arm64, darwin) (push) Waiting to run
CI / cross-compile (arm64, linux) (push) Waiting to run
CI / cross-compile (arm64, windows) (push) Waiting to run
CI / test (push) Waiting to run
CI / cross-compile (amd64, darwin) (push) Waiting to run
CI / cross-compile (amd64, windows) (push) Waiting to run
CodeQL Advanced / Analyze (go) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
Deploy Pages / build (push) Waiting to run
Deploy Pages / deploy (push) Blocked by required conditions
chore: add SPDX license headers and automated verification (#740)
* chore: add SPDX license headers to all source files

Add Apache-2.0 SPDX license identifiers and copyright notices to all
tracked .go, .sh, .js, .mjs, .ts, and .tsx source files.

Introduce scripts/verify-license.sh and scripts/add-license.sh for
automated verification and bulk addition of license headers. Integrate
the check into CI (ci.yml) and the Makefile (license-check target as
a prerequisite of the existing check target).

This satisfies the OpenSSF Best Practices Badge requirements for
copyright_per_file and license_per_file.

* fix: restore execute permissions on scripts

* docs: add license header instructions to CONTRIBUTING guides

* docs: add license header instructions to pages contributing guides

* fix(pages): strip unclosed HTML comment markers to satisfy CodeQL

* fix: apply code review suggestions for license scripts

- Fix portability: detect macOS vs Linux stat for permission copy
- Fix has_header: check both SPDX and copyright (match verify logic)
- Fix is_ignored: match on path boundaries to avoid false positives
- Fix year extraction: use consistent pipeline across both scripts
- Fix Bash 3.2 compat: quote array length expansion for set -u

* fix(pages): use loop-until-clean for HTML comment stripping (CodeQL)

* fix(pages): use split/join instead of replace to avoid CodeQL false positive

CodeQL's js/incomplete-multi-character-sanitization rule flags any
.replace() that removes multi-character sequences like '<!--...-->',
regardless of context. The data here comes from readFileSync on the
project's own index.html (no untrusted input), making this a false
positive. Using split(regex).join('') achieves the same result without
triggering the taint-tracking rule.
2026-08-05 21:26:27 +08:00

82 lines
2.4 KiB
Go

// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 alibaba/open-code-review Contributors
package session
import (
"testing"
"github.com/alibaba/open-code-review/internal/model"
)
func TestLoadComments_ReturnsCommentsInOrder(t *testing.T) {
tmpHome := t.TempDir()
t.Setenv("HOME", tmpHome)
repoDir := t.TempDir()
sh := New(repoDir, "main", "test-model", SessionOptions{
ReviewMode: ReviewModeCommit,
DiffCommit: "abc123",
})
sh.RecordReviewItemDone("a.go", "a.go", "a.go", "fp-a", []model.LlmComment{
{Path: "a.go", Content: "first", Severity: "high", Category: "bug"},
{Content: "second, no path", Severity: "low"},
})
sh.RecordReviewItemReused("b.go", "b.go", "b.go", "fp-b", "prior-session", []model.LlmComment{
{Path: "b.go", Content: "cached", Severity: "medium"},
})
sh.RecordReviewItemFailed("c.go", "c.go", "c.go", "fp-c", "boom")
sh.Finalize()
got, err := LoadComments(repoDir, sh.SessionID)
if err != nil {
t.Fatalf("LoadComments: %v", err)
}
if len(got) != 3 {
t.Fatalf("expected 3 comments, got %d: %+v", len(got), got)
}
if got[0].Content != "first" || got[1].Content != "second, no path" || got[2].Content != "cached" {
t.Errorf("unexpected order: %+v", got)
}
if got[1].Path != "a.go" {
t.Errorf("comment without path should inherit record file path, got %q", got[1].Path)
}
}
func TestLoadComments_LaterCheckpointSupersedes(t *testing.T) {
tmpHome := t.TempDir()
t.Setenv("HOME", tmpHome)
repoDir := t.TempDir()
sh := New(repoDir, "main", "test-model", SessionOptions{
ReviewMode: ReviewModeCommit,
DiffCommit: "abc123",
})
sh.RecordReviewItemDone("a.go", "a.go", "a.go", "fp-a", []model.LlmComment{
{Path: "a.go", Content: "stale"},
})
sh.RecordReviewItemDone("a.go", "a.go", "a.go", "fp-a", []model.LlmComment{
{Path: "a.go", Content: "fresh"},
})
sh.RecordReviewItemDone("b.go", "b.go", "b.go", "fp-b", []model.LlmComment{
{Path: "b.go", Content: "kept"},
})
sh.RecordReviewItemFailed("b.go", "b.go", "b.go", "fp-b", "boom")
sh.Finalize()
got, err := LoadComments(repoDir, sh.SessionID)
if err != nil {
t.Fatalf("LoadComments: %v", err)
}
if len(got) != 1 || got[0].Content != "fresh" {
t.Fatalf("expected only the superseding comment, got %+v", got)
}
}
func TestLoadComments_MissingSession(t *testing.T) {
tmpHome := t.TempDir()
t.Setenv("HOME", tmpHome)
if _, err := LoadComments(t.TempDir(), "nonexistent"); err == nil {
t.Fatal("expected error for missing session")
}
}