| 1 | import { slugToUri } from "./uriUtils.js"; |
| 2 | import { |
| 3 | AuthenticatedConfig, |
| 4 | EnvironmentAuthConfig, |
| 5 | getAccessToken, |
| 6 | getAssistantSlug, |
| 7 | getOrganizationId, |
| 8 | isAuthenticatedConfig, |
| 9 | isEnvironmentAuthConfig, |
| 10 | } from "./workos.js"; |
| 11 | |
| 12 | describe("workos utility functions", () => { |
| 13 | describe("isAuthenticatedConfig", () => { |
| 14 | it("should return true for authenticated config", () => { |
| 15 | const config: AuthenticatedConfig = { |
| 16 | userId: "user123", |
| 17 | userEmail: "user@example.com", |
| 18 | accessToken: "token123", |
| 19 | refreshToken: "refresh123", |
| 20 | expiresAt: Date.now() + 3600000, |
| 21 | organizationId: "org123", |
| 22 | configUri: slugToUri("assistant123"), |
| 23 | }; |
| 24 | expect(isAuthenticatedConfig(config)).toBe(true); |
| 25 | }); |
| 26 | |
| 27 | it("should return false for environment auth config", () => { |
| 28 | const config: EnvironmentAuthConfig = { |
| 29 | accessToken: "token123", |
| 30 | organizationId: null, |
| 31 | configUri: slugToUri("assistant123"), |
| 32 | }; |
| 33 | expect(isAuthenticatedConfig(config)).toBe(false); |
| 34 | }); |
| 35 | |
| 36 | it("should return false for null config", () => { |
| 37 | expect(isAuthenticatedConfig(null)).toBe(false); |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | describe("isEnvironmentAuthConfig", () => { |
| 42 | it("should return true for environment auth config", () => { |
| 43 | const config: EnvironmentAuthConfig = { |
| 44 | accessToken: "token123", |
| 45 | organizationId: null, |
| 46 | configUri: slugToUri("assistant123"), |
| 47 | }; |
| 48 | expect(isEnvironmentAuthConfig(config)).toBe(true); |
| 49 | }); |
| 50 | |
| 51 | it("should return false for authenticated config", () => { |
| 52 | const config: AuthenticatedConfig = { |
| 53 | userId: "user123", |
| 54 | userEmail: "user@example.com", |
| 55 | accessToken: "token123", |
| 56 | refreshToken: "refresh123", |
| 57 | expiresAt: Date.now() + 3600000, |
| 58 | organizationId: "org123", |
| 59 | configUri: slugToUri("assistant123"), |
| 60 | }; |
| 61 | expect(isEnvironmentAuthConfig(config)).toBe(false); |
| 62 | }); |
| 63 | |
| 64 | it("should return false for null config", () => { |
| 65 | expect(isEnvironmentAuthConfig(null)).toBe(false); |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | describe("getAccessToken", () => { |
| 70 | it("should return access token from authenticated config", () => { |
| 71 | const config: AuthenticatedConfig = { |
| 72 | userId: "user123", |
| 73 | userEmail: "user@example.com", |
| 74 | accessToken: "token123", |
| 75 | refreshToken: "refresh123", |
| 76 | expiresAt: Date.now() + 3600000, |
| 77 | organizationId: "org123", |
| 78 | configUri: slugToUri("assistant123"), |
| 79 | }; |
| 80 | expect(getAccessToken(config)).toBe("token123"); |
| 81 | }); |
| 82 | |
| 83 | it("should return access token from environment auth config", () => { |
| 84 | const config: EnvironmentAuthConfig = { |
| 85 | accessToken: "env-token123", |
| 86 | organizationId: null, |
| 87 | configUri: slugToUri("assistant123"), |
| 88 | }; |
| 89 | expect(getAccessToken(config)).toBe("env-token123"); |
| 90 | }); |
| 91 | |
| 92 | it("should return null for null config", () => { |
| 93 | expect(getAccessToken(null)).toBe(null); |
| 94 | }); |
| 95 | }); |
| 96 | |
| 97 | describe("getOrganizationId", () => { |
| 98 | it("should return organization ID from authenticated config", () => { |
| 99 | const config: AuthenticatedConfig = { |
| 100 | userId: "user123", |
| 101 | userEmail: "user@example.com", |
| 102 | accessToken: "token123", |
| 103 | refreshToken: "refresh123", |
| 104 | expiresAt: Date.now() + 3600000, |
| 105 | organizationId: "org123", |
| 106 | configUri: slugToUri("assistant123"), |
| 107 | }; |
| 108 | expect(getOrganizationId(config)).toBe("org123"); |
| 109 | }); |
| 110 | |
| 111 | it("should return null organization ID from authenticated config", () => { |
| 112 | const config: AuthenticatedConfig = { |
| 113 | userId: "user123", |
| 114 | userEmail: "user@example.com", |
| 115 | accessToken: "token123", |
| 116 | refreshToken: "refresh123", |
| 117 | expiresAt: Date.now() + 3600000, |
| 118 | organizationId: null, |
| 119 | configUri: slugToUri("assistant123"), |
| 120 | }; |
| 121 | expect(getOrganizationId(config)).toBe(null); |
| 122 | }); |
| 123 | |
| 124 | it("should return null from environment auth config", () => { |
| 125 | const config: EnvironmentAuthConfig = { |
| 126 | accessToken: "env-token123", |
| 127 | organizationId: null, |
| 128 | configUri: slugToUri("assistant123"), |
| 129 | }; |
| 130 | expect(getOrganizationId(config)).toBe(null); |
| 131 | }); |
| 132 | |
| 133 | it("should return null for null config", () => { |
| 134 | expect(getOrganizationId(null)).toBe(null); |
| 135 | }); |
| 136 | }); |
| 137 | |
| 138 | describe("getAssistantSlug", () => { |
| 139 | it("should return assistant slug from authenticated config", () => { |
| 140 | const config: AuthenticatedConfig = { |
| 141 | userId: "user123", |
| 142 | userEmail: "user@example.com", |
| 143 | accessToken: "token123", |
| 144 | refreshToken: "refresh123", |
| 145 | expiresAt: Date.now() + 3600000, |
| 146 | organizationId: "org123", |
| 147 | configUri: slugToUri("assistant123"), |
| 148 | }; |
| 149 | expect(getAssistantSlug(config)).toBe("assistant123"); |
| 150 | }); |
| 151 | |
| 152 | it("should return null when assistant slug is undefined in authenticated config", () => { |
| 153 | const config: AuthenticatedConfig = { |
| 154 | userId: "user123", |
| 155 | userEmail: "user@example.com", |
| 156 | accessToken: "token123", |
| 157 | refreshToken: "refresh123", |
| 158 | expiresAt: Date.now() + 3600000, |
| 159 | organizationId: "org123", |
| 160 | configUri: undefined, |
| 161 | }; |
| 162 | expect(getAssistantSlug(config)).toBe(null); |
| 163 | }); |
| 164 | |
| 165 | it("should return assistant slug from environment auth config", () => { |
| 166 | const config: EnvironmentAuthConfig = { |
| 167 | accessToken: "env-token123", |
| 168 | organizationId: null, |
| 169 | configUri: slugToUri("env-assistant123"), |
| 170 | }; |
| 171 | expect(getAssistantSlug(config)).toBe("env-assistant123"); |
| 172 | }); |
| 173 | |
| 174 | it("should return null when assistant slug is undefined in environment auth config", () => { |
| 175 | const config: EnvironmentAuthConfig = { |
| 176 | accessToken: "env-token123", |
| 177 | organizationId: null, |
| 178 | configUri: undefined, |
| 179 | }; |
| 180 | expect(getAssistantSlug(config)).toBe(null); |
| 181 | }); |
| 182 | |
| 183 | it("should return null for null config", () => { |
| 184 | expect(getAssistantSlug(null)).toBe(null); |
| 185 | }); |
| 186 | }); |
| 187 | }); |