Files
forge/services/repository/files/tree_test.go
zeekayandHanzo Dev 3ee9531d56 Give the test fixtures hosts and names of their own
Fixtures asserted against try.gitea.io, gitea.io and repositories under
upstream's accounts. They now use reserved example hosts and an example owner,
the recorded downloader responses were renamed to match, and the RPM and Alpine
package fixtures were rebuilt with recomputed digests. The schema dumps the
migration test restores are named for the version they capture.

Two fixtures were wrong, not merely branded. readme-test carried a
special-subdir-.gitea branch while the README lookup and its test both read
.hanzo, so that case asserted against a branch that did not exist; the branch
now holds .hanzo/README.md. The repository fixtures' delegate hooks still
re-exec the pre-rename binary name, and the licenses end-to-end test looked for
a module this tree no longer depends on.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-10 18:38:20 -07:00

122 lines
3.7 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// SPDX-License-Identifier: MIT
package files
import (
"html/template"
"testing"
"github.com/hanzoai/git/models/unittest"
"github.com/hanzoai/git/modules/fileicon"
"github.com/hanzoai/git/modules/git"
api "github.com/hanzoai/git/modules/structs"
"github.com/hanzoai/git/services/contexttest"
"github.com/stretchr/testify/assert"
)
func TestGetTreeBySHA(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1")
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
page := 1
perPage := 10
ctx.SetPathParam("sha", ctx.Repo.Repository.DefaultBranch)
tree, err := GetTreeBySHA(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.PathParam("sha"), page, perPage, true)
assert.NoError(t, err)
expectedTree := &api.GitTreeResponse{
SHA: "2a2f1d4670728a2e10049e345bd7a276468beab6",
URL: "https://git.example.com/v1/repos/user2/repo1/git/trees/2a2f1d4670728a2e10049e345bd7a276468beab6",
Entries: []api.GitEntry{
{
Path: "README.md",
Mode: "100644",
Type: "blob",
Size: 30,
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
URL: "https://git.example.com/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f",
},
},
Truncated: false,
Page: 1,
TotalCount: 1,
}
assert.Equal(t, expectedTree, tree)
}
func TestGetTreeViewNodes(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1")
ctx.Repo.RefFullName = git.RefNameFromBranch("sub-home-md-img-check")
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
curRepoLink := "/any/repo-link"
renderedIconPool := fileicon.NewRenderedIconPool()
mockIconForFile := func(id string) template.HTML {
return template.HTML(`<svg class="svg git-entry-icon octicon-file" width="16" height="16" aria-hidden="true"><use href="#` + id + `"></use></svg>`)
}
mockIconForFolder := func() template.HTML {
// With basic theme (default for folders), we get octicon icons without IDs
return template.HTML(`<span>octicon-file-directory-fill(16/)</span>`)
}
mockOpenIconForFolder := func() template.HTML {
// With basic theme (default for folders), we get octicon icons without IDs
return template.HTML(`<span>octicon-file-directory-open-fill(16/)</span>`)
}
treeNodes, err := GetTreeViewNodes(ctx, curRepoLink, renderedIconPool, ctx.Repo.GitRepo, ctx.Repo.Commit, "", "")
assert.NoError(t, err)
assert.Equal(t, []*TreeViewNode{
{
EntryName: "docs",
EntryMode: "tree",
FullPath: "docs",
EntryIcon: mockIconForFolder(),
EntryIconOpen: mockOpenIconForFolder(),
},
}, treeNodes)
treeNodes, err = GetTreeViewNodes(ctx, curRepoLink, renderedIconPool, ctx.Repo.GitRepo, ctx.Repo.Commit, "", "docs/README.md")
assert.NoError(t, err)
assert.Equal(t, []*TreeViewNode{
{
EntryName: "docs",
EntryMode: "tree",
FullPath: "docs",
EntryIcon: mockIconForFolder(),
EntryIconOpen: mockOpenIconForFolder(),
Children: []*TreeViewNode{
{
EntryName: "README.md",
EntryMode: "blob",
FullPath: "docs/README.md",
EntryIcon: mockIconForFile(`svg-mfi-readme`),
},
},
},
}, treeNodes)
treeNodes, err = GetTreeViewNodes(ctx, curRepoLink, renderedIconPool, ctx.Repo.GitRepo, ctx.Repo.Commit, "docs", "README.md")
assert.NoError(t, err)
assert.Equal(t, []*TreeViewNode{
{
EntryName: "README.md",
EntryMode: "blob",
FullPath: "docs/README.md",
EntryIcon: mockIconForFile(`svg-mfi-readme`),
},
}, treeNodes)
}