repository.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { describe, expect, test } from "bun:test"
  2. import path from "path"
  3. import { pathToFileURL } from "url"
  4. import { Repository } from "@opencode-ai/core/repository"
  5. describe("Repository", () => {
  6. test("parses github shorthand and builds an explicit-root cache path", () => {
  7. const reference = Repository.parseRemote("owner/repo")
  8. expect(reference).toMatchObject({
  9. host: "github.com",
  10. path: "owner/repo",
  11. segments: ["owner", "repo"],
  12. owner: "owner",
  13. repo: "repo",
  14. remote: "https://github.com/owner/repo.git",
  15. label: "owner/repo",
  16. })
  17. expect(Repository.cachePath("/cache", reference)).toBe(path.join("/cache", "github.com", "owner", "repo"))
  18. expect(Repository.cacheIdentity(reference)).toBe("github.com/owner/repo")
  19. })
  20. test("parses host path and scp remote references", () => {
  21. expect(Repository.parseRemote("gitlab.com/group/repo")).toMatchObject({
  22. host: "gitlab.com",
  23. path: "group/repo",
  24. remote: "https://gitlab.com/group/repo.git",
  25. label: "gitlab.com/group/repo",
  26. })
  27. expect(Repository.parseRemote("git@github.com:owner/repo.git")).toMatchObject({
  28. host: "github.com",
  29. path: "owner/repo",
  30. remote: "git@github.com:owner/repo.git",
  31. label: "owner/repo",
  32. })
  33. })
  34. test("keeps local file repositories distinct from remote repositories", () => {
  35. const localPath = path.resolve("repo.git")
  36. const reference = Repository.parse(pathToFileURL(localPath).href)
  37. expect(reference).toMatchObject({ host: "file", protocol: "file:", label: localPath })
  38. expect(reference && Repository.isFile(reference)).toBe(true)
  39. expect(reference && Repository.isRemote(reference)).toBe(false)
  40. expect(() => Repository.parseRemote(pathToFileURL(localPath).href)).toThrow(
  41. Repository.UnsupportedLocalRepositoryError,
  42. )
  43. })
  44. test("rejects unsafe remote references and branches with typed errors", () => {
  45. expect(() => Repository.parseRemote("not-a-repo")).toThrow(Repository.InvalidReferenceError)
  46. expect(() => Repository.parseRemote("git@github.com:../../../etc/passwd")).toThrow(Repository.InvalidReferenceError)
  47. expect(() => Repository.validateBranch("feature/docs.v1")).not.toThrow()
  48. expect(() => Repository.validateBranch("-bad")).toThrow(Repository.InvalidBranchError)
  49. expect(() => Repository.validateBranch("bad..branch")).toThrow(Repository.InvalidBranchError)
  50. expect(() => Repository.validateBranch("bad branch")).toThrow(Repository.InvalidBranchError)
  51. })
  52. test("compares cache identity independent of input spelling", () => {
  53. const shorthand = Repository.parseRemote("owner/repo")
  54. expect(Repository.same(shorthand, Repository.parseRemote("https://github.com/owner/repo.git"))).toBe(true)
  55. expect(Repository.same(shorthand, Repository.parseRemote("github.com/owner/repo"))).toBe(true)
  56. })
  57. })