@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 169 lines 5.87 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as cci from "@distilled.cloud/gcp/contactcenterinsights_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const datasetId = "alchemy-cci-label-ds";
15 const conversationId = "alchemy-cci-ds-label-conv";
16 const namesOf = (project: string) => {
17 const locationParent = `projects/${project}/locations/us-central1`;
18 const datasetName = `${locationParent}/datasets/${datasetId}`;
19 const conversationName = `${datasetName}/conversations/${conversationId}`;
20 return { locationParent, datasetName, conversationName };
21 };
22
23 const waitUntilGone = (name: string) =>
24 cci.getProjectsLocationsDatasetsConversationsFeedbackLabels({ name }).pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.repeat({
28 schedule: Schedule.spaced("1 second"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34 const waitUntilDatasetGone = (name: string) =>
35 cci.getProjectsLocationsDatasets({ name }).pipe(
36 Effect.as("found" as const),
37 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
38 Effect.repeat({
39 schedule: Schedule.spaced("2 seconds"),
40 until: (status) => status === "gone",
41 times: 10,
42 }),
43 );
44
45 const ensureDataset = ({ locationParent, datasetName }: ReturnType<typeof namesOf>) =>
46 cci.getProjectsLocationsDatasets({ name: datasetName }).pipe(
47 Effect.catchTag("NotFound", () =>
48 cci.createProjectsLocationsDatasets({
49 parent: locationParent,
50 datasetId,
51 body: {
52 displayName: "alchemy-cci-label-ds",
53 type: "EVAL",
54 description: "alchemy test dataset",
55 },
56 }),
57 ),
58 );
59
60 const ensureConversation = ({ datasetName, conversationName }: ReturnType<typeof namesOf>) =>
61 cci.getProjectsLocationsDatasetsConversations({ name: conversationName }).pipe(
62 Effect.catchTag("NotFound", () =>
63 cci.createProjectsLocationsConversations({
64 parent: datasetName,
65 conversationId,
66 body: {
67 medium: "CHAT",
68 languageCode: "en-US",
69 labels: { "alchemy-test": "cci" },
70 },
71 }),
72 ),
73 );
74
75 const deleteParents = ({ datasetName, conversationName }: ReturnType<typeof namesOf>) =>
76 Effect.gen(function* () {
77 yield* cci
78 .deleteProjectsLocationsDatasetsConversations({
79 name: conversationName,
80 force: true,
81 })
82 .pipe(Effect.catchTag("NotFound", () => Effect.void));
83 const deleted = yield* cci
84 .deleteProjectsLocationsDatasets({ name: datasetName })
85 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
86 if (deleted !== undefined) {
87 yield* waitUntilDatasetGone(datasetName);
88 }
89 });
90
91 test.provider(
92 "getProjectsLocationsDatasetsConversationsFeedbackLabels on a missing label fails with a typed tag",
93 (stack) =>
94 Effect.gen(function* () {
95 const { project } = yield* GcpEnvironment.current;
96 const names = namesOf(project);
97 const { conversationName } = names;
98 yield* stack.destroy();
99
100 const error = yield* Effect.flip(
101 cci.getProjectsLocationsDatasetsConversationsFeedbackLabels({
102 name: `${conversationName}/feedbackLabels/missing`,
103 }),
104 );
105 expect(error._tag).toEqual("NotFound");
106
107 yield* stack.destroy();
108 }).pipe(logLevel),
109 {
110 tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"],
111 timeout: 90_000,
112 },
113 );
114
115 // The fixture creates its dataset conversation with
116 // createProjectsLocationsConversations under a dataset parent, which the
117 // testing project answers with NotFound "404"; dataset conversations have
118 // to be ingested. Set GCP_TEST_CCI_DATASETS=1 where that path works.
119 test.provider.skipIf(!!process.env.FAST || !process.env.GCP_TEST_CCI_DATASETS)(
120 "create, update, and delete a dataset conversation feedback label",
121 (stack) =>
122 Effect.gen(function* () {
123 const { project } = yield* GcpEnvironment.current;
124 const names = namesOf(project);
125 const { conversationName } = names;
126 yield* stack.destroy();
127 yield* ensureDataset(names);
128 const conversation = yield* ensureConversation(names);
129
130 const created = yield* stack.deploy(
131 Effect.gen(function* () {
132 return yield* GCP.ContactCenterInsights.DatasetsConversationsFeedbackLabel("Topic", {
133 parent: conversation.name ?? conversationName,
134 label: "billing",
135 });
136 }),
137 );
138
139 expect(created.name).toContain("/feedbackLabels/");
140 expect(created.label).toEqual("billing");
141
142 const fetched = yield* cci.getProjectsLocationsDatasetsConversationsFeedbackLabels({
143 name: created.name,
144 });
145 expect(fetched.name).toEqual(created.name);
146 expect(fetched.label).toContain("alchemy-id=");
147
148 const updated = yield* stack.deploy(
149 Effect.gen(function* () {
150 return yield* GCP.ContactCenterInsights.DatasetsConversationsFeedbackLabel("Topic", {
151 parent: conversation.name ?? conversationName,
152 feedbackLabelId: created.feedbackLabelId,
153 label: "invoices",
154 });
155 }),
156 );
157 expect(updated.name).toEqual(created.name);
158 expect(updated.label).toEqual("invoices");
159
160 yield* stack.destroy();
161 const gone = yield* waitUntilGone(created.name);
162 expect(gone).toEqual("gone");
163 yield* deleteParents(names);
164 }).pipe(logLevel),
165 {
166 tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"],
167 timeout: 120_000,
168 },
169 );