Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

promptLinks.test.ts

127 lines4.7 KB

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​
44describe("pickPromptLinkPullRequestThread", () => {
45 it("takes the first open thread in order once every earlier lookup has answered", () => {
46 const laptop = EnvironmentId.make("laptop");
47 const server = EnvironmentId.make("server");
48 const linked = (
49 id: string,
50 archivedAt: string | null = null,
51 ): PullRequestLinkedThreadsResult => ({
52 threads: [{ id: ThreadId.make(id), projectId: ProjectId.make("p"), title: id, archivedAt }],
53 });
54 const none: PullRequestLinkedThreadsResult = { threads: [] };
55 const pick = (
56 ...results: ReadonlyArray<AsyncResult.AsyncResult<PullRequestLinkedThreadsResult, unknown>>
57 ) =>
58 pickPromptLinkPullRequestThread(
59 results.map((result, index) => ({ environmentId: index === 0 ? laptop : server, result })),
60 );
61 // A value shown while it refetches is not an answer yet.
62 expect(pick(AsyncResult.success(none, { waiting: true }))).toBeUndefined();
63 expect(pick(AsyncResult.success(none), AsyncResult.initial(true))).toBeUndefined();
64 expect(pick(AsyncResult.initial(true), AsyncResult.success(linked("b")))).toBeUndefined();
65 const laptopThread = { environmentId: laptop, id: ThreadId.make("a") };
66 expect(pick(AsyncResult.success(linked("a")), AsyncResult.initial(true))).toEqual(laptopThread);
67 expect(pick(AsyncResult.success(linked("a")), AsyncResult.success(linked("b")))).toEqual(
68 laptopThread,
69 );
70 expect(pick(AsyncResult.success(none), AsyncResult.success(linked("old")))).toEqual({
71 environmentId: server,
72 id: ThreadId.make("old"),
73 });
74 expect(
75 pick(
76 AsyncResult.success(linked("archived", "2026-01-01T00:00:00.000Z")),
77 AsyncResult.fail(1),
78 ),
79 ).toBeNull();
80 });
81});
82​
83describe("resolvePromptLinkModelSelection", () => {
84 const provider = (
85 instanceId: string,
86 driver: string,
87 status: "ready" | "warning" | "error" | "disabled" = "ready",
88 ) => ({
89 instanceId: ProviderInstanceId.make(instanceId),
90 driver: ProviderDriverKind.make(driver),
91 enabled: true,
92 status,
93 models: [
94 { slug: "small", name: "Small" },
95 { slug: "big", name: "Big", aliases: ["opus"], isDefault: true },
96 ],
97 });
98​
99 it("maps the requested provider and model onto a usable instance", () => {
100 const providers = [provider("broken", "claudeAgent", "error"), provider("work", "claudeAgent")];
101 expect(
102 resolvePromptLinkModelSelection(providers, { provider: "claude", model: "small" }),
103 ).toEqual({
104 selection: { instanceId: "work", model: "small" },
105 exact: true,
106 });
107 expect(
108 resolvePromptLinkModelSelection(providers, { provider: "claudeagent", model: "opus" }),
109 ).toEqual({
110 selection: { instanceId: "work", model: "big" },
111 exact: true,
112 });
113 });
114​
115 it("uses the instance default for an unknown model, and nothing for a missing provider", () => {
116 const providers = [provider("codex", "codex")];
117 expect(
118 resolvePromptLinkModelSelection(providers, { provider: "codex", model: "nope" }),
119 ).toEqual({
120 selection: { instanceId: "codex", model: "big" },
121 exact: false,
122 });
123 expect(
124 resolvePromptLinkModelSelection(providers, { provider: "cursor", model: null }),
125 ).toBeNull();
126 });
127});