github-remote.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { test, expect } from "bun:test"
  2. import { parseGitHubRemote } from "../../src/cli/cmd/github"
  3. test("parses https URL with .git suffix", () => {
  4. expect(parseGitHubRemote("https://github.com/sst/opencode.git")).toEqual({ owner: "sst", repo: "kirincode" })
  5. })
  6. test("parses https URL without .git suffix", () => {
  7. expect(parseGitHubRemote("https://github.com/sst/opencode")).toEqual({ owner: "sst", repo: "kirincode" })
  8. })
  9. test("parses git@ URL with .git suffix", () => {
  10. expect(parseGitHubRemote("git@github.com:sst/opencode.git")).toEqual({ owner: "sst", repo: "kirincode" })
  11. })
  12. test("parses git@ URL without .git suffix", () => {
  13. expect(parseGitHubRemote("git@github.com:sst/opencode")).toEqual({ owner: "sst", repo: "kirincode" })
  14. })
  15. test("parses ssh:// URL with .git suffix", () => {
  16. expect(parseGitHubRemote("ssh://git@github.com/sst/opencode.git")).toEqual({ owner: "sst", repo: "kirincode" })
  17. })
  18. test("parses ssh:// URL without .git suffix", () => {
  19. expect(parseGitHubRemote("ssh://git@github.com/sst/opencode")).toEqual({ owner: "sst", repo: "kirincode" })
  20. })
  21. test("parses git protocol URLs from package metadata", () => {
  22. expect(parseGitHubRemote("git://github.com/facebook/react.git")).toEqual({ owner: "facebook", repo: "react" })
  23. expect(parseGitHubRemote("git+https://github.com/facebook/react.git")).toEqual({ owner: "facebook", repo: "react" })
  24. expect(parseGitHubRemote("git+ssh://git@github.com/facebook/react.git")).toEqual({ owner: "facebook", repo: "react" })
  25. })
  26. test("parses npm-style github shorthand", () => {
  27. expect(parseGitHubRemote("github:facebook/react")).toBeNull()
  28. })
  29. test("parses http URL", () => {
  30. expect(parseGitHubRemote("http://github.com/owner/repo")).toEqual({ owner: "owner", repo: "repo" })
  31. })
  32. test("parses URL with hyphenated owner and repo names", () => {
  33. expect(parseGitHubRemote("https://github.com/my-org/my-repo.git")).toEqual({ owner: "my-org", repo: "my-repo" })
  34. })
  35. test("parses URL with underscores in names", () => {
  36. expect(parseGitHubRemote("git@github.com:my_org/my_repo.git")).toEqual({ owner: "my_org", repo: "my_repo" })
  37. })
  38. test("parses URL with numbers in names", () => {
  39. expect(parseGitHubRemote("https://github.com/org123/repo456")).toEqual({ owner: "org123", repo: "repo456" })
  40. })
  41. test("parses repos with dots in the name", () => {
  42. expect(parseGitHubRemote("https://github.com/socketio/socket.io.git")).toEqual({
  43. owner: "socketio",
  44. repo: "socket.io",
  45. })
  46. expect(parseGitHubRemote("https://github.com/vuejs/vue.js")).toEqual({
  47. owner: "vuejs",
  48. repo: "vue.js",
  49. })
  50. expect(parseGitHubRemote("git@github.com:mrdoob/three.js.git")).toEqual({
  51. owner: "mrdoob",
  52. repo: "three.js",
  53. })
  54. expect(parseGitHubRemote("https://github.com/jashkenas/backbone.git")).toEqual({
  55. owner: "jashkenas",
  56. repo: "backbone",
  57. })
  58. })
  59. test("returns null for non-github URLs", () => {
  60. expect(parseGitHubRemote("https://gitlab.com/owner/repo.git")).toBeNull()
  61. expect(parseGitHubRemote("git@gitlab.com:owner/repo.git")).toBeNull()
  62. expect(parseGitHubRemote("https://bitbucket.org/owner/repo")).toBeNull()
  63. })
  64. test("returns null for invalid URLs", () => {
  65. expect(parseGitHubRemote("not-a-url")).toBeNull()
  66. expect(parseGitHubRemote("")).toBeNull()
  67. expect(parseGitHubRemote("github.com")).toBeNull()
  68. expect(parseGitHubRemote("https://github.com/")).toBeNull()
  69. expect(parseGitHubRemote("https://github.com/owner")).toBeNull()
  70. })
  71. test("returns null for URLs with extra path segments", () => {
  72. expect(parseGitHubRemote("https://github.com/owner/repo/tree/main")).toBeNull()
  73. expect(parseGitHubRemote("https://github.com/owner/repo/blob/main/file.ts")).toBeNull()
  74. })