Open History to see the last commit
1import {
2 EnvironmentId,
3 ProjectId,
4 ProviderDriverKind,
5 ProviderInstanceId,
6 ThreadId,
7 type PullRequestLinkedThreadsResult,
8 type RepositoryIdentity,
9} from "@t3tools/contracts";
10import { AsyncResult } from "effect/reactivity";
11import { describe, expect, it } from "vite-plus/test";
12
13import {
14 findPromptLinkProjects,
15 pickPromptLinkPullRequestThread,
16 resolvePromptLinkModelSelection,
17} from "./promptLinks.ts";
18
19const identity = (canonicalKey: string, origin?: string): RepositoryIdentity => ({
20 canonicalKey,
21 locator: { source: "git-remote", remoteName: "origin", remoteUrl: `https://${canonicalKey}` },
22 ...(origin ? { origin: { canonicalKey: origin } } : {}),
23});
24
25describe("findPromptLinkProjects", () => {
26 it("keeps exact matches in order and never a fork of the repository", () => {
27 const projects = [
28 {
29 id: "fork",
30 repositoryIdentity: identity("github.com/pingdotgg/t3code", "github.com/me/t3code"),
31 },
32 { id: "none", repositoryIdentity: null },
33 { id: "recent", repositoryIdentity: identity("github.com/PingDotGG/T3Code") },
34 { id: "older", repositoryIdentity: identity("github.com/pingdotgg/t3code") },
35 ];
36 const ids = (owner: string, name: string) =>
37 findPromptLinkProjects(projects, { owner, name }).map((project) => project.id);
38 expect(ids("pingdotgg", "t3code")).toEqual(["recent", "older"]);
39 expect(ids("me", "t3code")).toEqual(["fork"]);
40 expect(ids("gitlab", "x")).toEqual([]);
41 });
42
43 it("matches a checkout of the repository's GitCafe mirror", () => {
44 const projects = [
45 { id: "gitcafe", repositoryIdentity: identity("git.cafe/pingdotgg/t3code") },
46 { id: "staging", repositoryIdentity: identity("staging.git.cafe/pingdotgg/t3code") },
47 { id: "gitlab", repositoryIdentity: identity("gitlab.com/pingdotgg/t3code") },
48 ];
49 expect(
50 findPromptLinkProjects(projects, { owner: "pingdotgg", name: "t3code" }).map(
51 (project) => project.id,
52 ),
53 ).toEqual(["gitcafe", "staging"]);
54 });
55});
56
57describe("pickPromptLinkPullRequestThread", () => {
58 it("takes the first open thread in order once every earlier lookup has answered", () => {
59 const laptop = EnvironmentId.make("laptop");
60 const server = EnvironmentId.make("server");
61 const linked = (
62 id: string,
63 archivedAt: string | null = null,
64 ): PullRequestLinkedThreadsResult => ({
65 threads: [{ id: ThreadId.make(id), projectId: ProjectId.make("p"), title: id, archivedAt }],
66 });
67 const none: PullRequestLinkedThreadsResult = { threads: [] };
68 const pick = (
69 ...results: ReadonlyArray<AsyncResult.AsyncResult<PullRequestLinkedThreadsResult, unknown>>
70 ) =>
71 pickPromptLinkPullRequestThread(
72 results.map((result, index) => ({ environmentId: index === 0 ? laptop : server, result })),
73 );
74 // A value shown while it refetches is not an answer yet.
75 expect(pick(AsyncResult.success(none, { waiting: true }))).toBeUndefined();
76 expect(pick(AsyncResult.success(none), AsyncResult.initial(true))).toBeUndefined();
77 expect(pick(AsyncResult.initial(true), AsyncResult.success(linked("b")))).toBeUndefined();
78 const laptopThread = { environmentId: laptop, id: ThreadId.make("a") };
79 expect(pick(AsyncResult.success(linked("a")), AsyncResult.initial(true))).toEqual(laptopThread);
80 expect(pick(AsyncResult.success(linked("a")), AsyncResult.success(linked("b")))).toEqual(
81 laptopThread,
82 );
83 expect(pick(AsyncResult.success(none), AsyncResult.success(linked("old")))).toEqual({
84 environmentId: server,
85 id: ThreadId.make("old"),
86 });
87 expect(
88 pick(
89 AsyncResult.success(linked("archived", "2026-01-01T00:00:00.000Z")),
90 AsyncResult.fail(1),
91 ),
92 ).toBeNull();
93 });
94});
95
96describe("resolvePromptLinkModelSelection", () => {
97 const provider = (
98 instanceId: string,
99 driver: string,
100 status: "ready" | "warning" | "error" | "disabled" = "ready",
101 ) => ({
102 instanceId: ProviderInstanceId.make(instanceId),
103 driver: ProviderDriverKind.make(driver),
104 enabled: true,
105 status,
106 models: [
107 { slug: "small", name: "Small" },
108 { slug: "big", name: "Big", aliases: ["opus"], isDefault: true },
109 ],
110 });
111
112 it("maps the requested provider and model onto a usable instance", () => {
113 const providers = [provider("broken", "claudeAgent", "error"), provider("work", "claudeAgent")];
114 expect(
115 resolvePromptLinkModelSelection(providers, { provider: "claude", model: "small" }),
116 ).toEqual({
117 selection: { instanceId: "work", model: "small" },
118 exact: true,
119 });
120 expect(
121 resolvePromptLinkModelSelection(providers, { provider: "claudeagent", model: "opus" }),
122 ).toEqual({
123 selection: { instanceId: "work", model: "big" },
124 exact: true,
125 });
126 });
127
128 it("uses the instance default for an unknown model, and nothing for a missing provider", () => {
129 const providers = [provider("codex", "codex")];
130 expect(
131 resolvePromptLinkModelSelection(providers, { provider: "codex", model: "nope" }),
132 ).toEqual({
133 selection: { instanceId: "codex", model: "big" },
134 exact: false,
135 });
136 expect(
137 resolvePromptLinkModelSelection(providers, { provider: "cursor", model: null }),
138 ).toBeNull();
139 });
140});