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

knownEnvironment.test.ts

92 lines2.9 KB

1import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3​
4import { createKnownEnvironment, getKnownEnvironmentHttpBaseUrl } from "./knownEnvironment.ts";
5import {
6 parseScopedProjectKey,
7 parseScopedThreadKey,
8 scopedProjectKey,
9 scopedRefKey,
10 scopedThreadKey,
11 scopeProjectRef,
12 scopeThreadRef,
13} from "./scoped.ts";
14​
15describe("known environment bootstrap helpers", () => {
16 it("creates known environments from explicit server base urls", () => {
17 expect(
18 createKnownEnvironment({
19 label: "Remote environment",
20 target: {
21 httpBaseUrl: "https://remote.example.com",
22 wsBaseUrl: "wss://remote.example.com",
23 },
24 }),
25 ).toEqual({
26 id: "ws:Remote environment",
27 label: "Remote environment",
28 source: "manual",
29 target: {
30 httpBaseUrl: "https://remote.example.com",
31 wsBaseUrl: "wss://remote.example.com",
32 },
33 });
34 });
35​
36 it("returns the explicit fetchable http origin", () => {
37 expect(
38 getKnownEnvironmentHttpBaseUrl(
39 createKnownEnvironment({
40 label: "Local environment",
41 target: {
42 httpBaseUrl: "http://localhost:3773",
43 wsBaseUrl: "ws://localhost:3773",
44 },
45 }),
46 ),
47 ).toBe("http://localhost:3773");
48​
49 expect(
50 getKnownEnvironmentHttpBaseUrl(
51 createKnownEnvironment({
52 label: "Remote environment",
53 target: {
54 httpBaseUrl: "https://remote.example.com/api",
55 wsBaseUrl: "wss://remote.example.com/api",
56 },
57 }),
58 ),
59 ).toBe("https://remote.example.com/api");
60 });
61});
62​
63describe("scoped refs", () => {
64 const environmentId = EnvironmentId.make("environment-test");
65 const projectRef = scopeProjectRef(environmentId, ProjectId.make("project-1"));
66 const threadRef = scopeThreadRef(environmentId, ThreadId.make("thread-1"));
67​
68 it("builds stable scoped project and thread keys", () => {
69 expect(scopedRefKey(projectRef)).toBe("environment-test:project-1");
70 expect(scopedRefKey(threadRef)).toBe("environment-test:thread-1");
71 expect(scopedProjectKey(projectRef)).toBe("environment-test:project-1");
72 expect(scopedThreadKey(threadRef)).toBe("environment-test:thread-1");
73 });
74​
75 it("returns typed scoped refs", () => {
76 expect(projectRef).toEqual({
77 environmentId,
78 projectId: ProjectId.make("project-1"),
79 });
80 expect(threadRef).toEqual({
81 environmentId,
82 threadId: ThreadId.make("thread-1"),
83 });
84 });
85​
86 it("parses scoped project and thread keys back into refs", () => {
87 expect(parseScopedProjectKey("environment-test:project-1")).toEqual(projectRef);
88 expect(parseScopedThreadKey("environment-test:thread-1")).toEqual(threadRef);
89 expect(parseScopedProjectKey("bad-key")).toBeNull();
90 expect(parseScopedThreadKey("bad-key")).toBeNull();
91 });
92});