Files
kenn-io--agentsview/internal/parser/single_file_source_set_test.go
wehub-resource-sync f99010fae1
CI / lint (push) Failing after 1s
CI / frontend (push) Failing after 1s
CI / scripts (push) Failing after 1s
CI / Go Test (ubuntu-latest) (push) Failing after 0s
CI / frontend-node-25 (push) Failing after 1s
CI / docs (push) Failing after 0s
CI / coverage (push) Failing after 0s
CI / e2e (push) Failing after 0s
Docker / build-and-push (push) Failing after 1s
CI / integration (push) Failing after 4m43s
CI / Go Test (windows-latest) (push) Has been cancelled
CI / Desktop Unit Tests (Windows) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux (arm64)) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Windows) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (aarch64)) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (x86_64)) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:36 +08:00

86 lines
3.0 KiB
Go

package parser
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// newShapeOnlyTestSingleFileSourceSet builds a single-file source set whose
// classifyPath accepts a stored path by SHAPE alone (no on-disk check, as
// Reasonix and Cowork do) and whose findFile re-resolves a raw ID to the live
// file. Only the FindSource-relevant hooks carry real behavior; the rest are
// inert stubs required by the constructor.
func newShapeOnlyTestSingleFileSourceSet(root, livePath string) singleFileSourceSet {
return NewSingleFileSourceSet(
AgentReasonix,
[]string{root},
WithFileDiscovery(func(string) []singleFileMatch { return nil }),
WithFileWatchRoots(func([]string) []WatchRoot { return nil }),
WithFileChangedPathClassifier(
func(_, path string, _ bool) (singleFileMatch, bool) {
if path == "" {
return singleFileMatch{}, false
}
return singleFileMatch{Path: path}, true
},
),
WithFileLookup(func(_, rawID string) (singleFileMatch, bool) {
if rawID != "" && IsRegularFile(livePath) {
return singleFileMatch{Path: livePath}, true
}
return singleFileMatch{}, false
}),
WithFileFingerprint(
func(singleFileSource) (SourceFingerprint, error) {
return SourceFingerprint{}, nil
},
),
WithFileParse(
func(singleFileSource, ParseRequest) ([]ParseResult, []string, error) {
return nil, nil, nil
},
),
)
}
// TestSingleFileFindSourceRejectsStaleStoredPath verifies the fresh-source guard
// in singleFileSourceSet.FindSource: a stored path that classifies by shape but
// no longer exists must not be returned under RequireFreshSource; the lookup
// falls through to raw-ID re-resolution to the live file. Without
// RequireFreshSource the stored path is honored, preserving prior behavior.
func TestSingleFileFindSourceRejectsStaleStoredPath(t *testing.T) {
root := t.TempDir()
livePath := filepath.Join(root, "archive", "sess.jsonl")
require.NoError(t, os.MkdirAll(filepath.Dir(livePath), 0o755))
require.NoError(t, os.WriteFile(livePath, []byte("{}\n"), 0o644))
stalePath := filepath.Join(root, "sessions", "sess.jsonl") // never created
s := newShapeOnlyTestSingleFileSourceSet(root, livePath)
src, ok, err := s.FindSource(context.Background(), FindSourceRequest{
StoredFilePath: stalePath,
FingerprintKey: stalePath,
RawSessionID: "sess",
RequireFreshSource: true,
})
require.NoError(t, err)
require.True(t, ok, "raw-ID re-resolution should still find the live file")
assert.Equal(t, livePath, src.DisplayPath,
"a stale stored path must re-resolve to the live file under RequireFreshSource")
src2, ok2, err := s.FindSource(context.Background(), FindSourceRequest{
StoredFilePath: stalePath,
FingerprintKey: stalePath,
RawSessionID: "sess",
})
require.NoError(t, err)
require.True(t, ok2)
assert.Equal(t, stalePath, src2.DisplayPath,
"without RequireFreshSource the stored-path hint is honored unchanged")
}