| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | import type { VcsRef } from "@t3tools/contracts"; |
| 3 | import { buildBaseRefChoices, filterBaseRefChoices } from "./baseRefChoices"; |
| 4 | |
| 5 | function ref(name: string, remoteName?: string): VcsRef { |
| 6 | return { |
| 7 | name, |
| 8 | current: false, |
| 9 | isDefault: false, |
| 10 | isRemote: remoteName !== undefined, |
| 11 | ...(remoteName ? { remoteName } : {}), |
| 12 | worktreePath: null, |
| 13 | }; |
| 14 | } |
| 15 | |
| 16 | describe("buildBaseRefChoices", () => { |
| 17 | it("pairs matching local and remote branches and prefers origin", () => { |
| 18 | const choices = buildBaseRefChoices( |
| 19 | [ref("main")], |
| 20 | [ref("upstream/main", "upstream"), ref("origin/main", "origin")], |
| 21 | ); |
| 22 | |
| 23 | expect(choices).toEqual([ |
| 24 | expect.objectContaining({ |
| 25 | label: "main", |
| 26 | local: expect.objectContaining({ name: "main" }), |
| 27 | remote: expect.objectContaining({ name: "origin/main" }), |
| 28 | }), |
| 29 | expect.objectContaining({ |
| 30 | label: "upstream/main", |
| 31 | local: null, |
| 32 | remote: expect.objectContaining({ name: "upstream/main" }), |
| 33 | }), |
| 34 | ]); |
| 35 | }); |
| 36 | }); |
| 37 | |
| 38 | describe("filterBaseRefChoices", () => { |
| 39 | it("filters stale server results against the current query", () => { |
| 40 | const choices = buildBaseRefChoices( |
| 41 | [ref("main"), ref("feature/search")], |
| 42 | [ref("origin/main", "origin"), ref("origin/feature/search", "origin")], |
| 43 | ); |
| 44 | |
| 45 | expect(filterBaseRefChoices(choices, "SEARCH").map((choice) => choice.label)).toEqual([ |
| 46 | "feature/search", |
| 47 | ]); |
| 48 | expect(filterBaseRefChoices(choices, "origin/main").map((choice) => choice.label)).toEqual([ |
| 49 | "main", |
| 50 | ]); |
| 51 | }); |
| 52 | }); |