1
+import { describe, it, expect } from 'vitest';
2
+import {
3
+ ConnectionDataSchema,
4
+ ConnectionStatuses,
5
+ Oauth2RevokedConnectionDataSchema,
6
+} from '../../src/types/connectedAccountAuthStates.types';
7
+import { transformConnectedAccountResponse } from '../../src/utils/transformers/connectedAccounts';
8
+import { AuthSchemeTypes } from '../../src/types/authConfigs.types';
9
+
10
+describe('connectedAccountAuthStates — REVOKED arms', () => {
11
+ describe('Oauth2RevokedConnectionDataSchema', () => {
12
+ it('parses a payload with only the status discriminator', () => {
13
+ const parsed = Oauth2RevokedConnectionDataSchema.parse({
14
+ status: ConnectionStatuses.REVOKED,
15
+ });
16
+
17
+ expect(parsed.status).toBe(ConnectionStatuses.REVOKED);
18
+ expect(parsed.revoked_at).toBeUndefined();
19
+ });
20
+
21
+ it('parses a payload with optional revoked_at', () => {
22
+ const parsed = Oauth2RevokedConnectionDataSchema.parse({
23
+ status: ConnectionStatuses.REVOKED,
24
+ revoked_at: '2026-05-01T12:34:56Z',
25
+ });
26
+
27
+ expect(parsed.revoked_at).toBe('2026-05-01T12:34:56Z');
28
+ });
29
+
30
+ it('rejects when status is not REVOKED', () => {
31
+ expect(() =>
32
+ Oauth2RevokedConnectionDataSchema.parse({
33
+ status: ConnectionStatuses.ACTIVE,
34
+ })
35
+ ).toThrow();
36
+ });
37
+ });
38
+
39
+ describe('ConnectionDataSchema discriminated union', () => {
40
+ it('accepts REVOKED for OAUTH2 and preserves auth-scheme metadata', () => {
41
+ const parsed = ConnectionDataSchema.parse({
42
+ authScheme: AuthSchemeTypes.OAUTH2,
43
+ val: {
44
+ status: ConnectionStatuses.REVOKED,
45
+ revoked_at: '2026-05-01T00:00:00Z',
46
+ },
47
+ });
48
+
49
+ expect(parsed.authScheme).toBe(AuthSchemeTypes.OAUTH2);
50
+ expect(parsed.val.status).toBe(ConnectionStatuses.REVOKED);
51
+ });
52
+
53
+ it('accepts REVOKED for S2S_OAUTH2 (preempts the queued Apollo rollout)', () => {
54
+ // Today the server only emits REVOKED for OAUTH2, but S2S_OAUTH2 is on
55
+ // Apollo's near-term roadmap. Without the matching arm, the entire
56
+ // `state` would silently fail discriminated-union parsing in the
57
+ // top-level transformer.
58
+ const parsed = ConnectionDataSchema.parse({
59
+ authScheme: AuthSchemeTypes.S2S_OAUTH2,
60
+ val: {
61
+ status: ConnectionStatuses.REVOKED,
62
+ revoked_at: '2026-05-01T00:00:00Z',
63
+ },
64
+ });
65
+
66
+ expect(parsed.authScheme).toBe(AuthSchemeTypes.S2S_OAUTH2);
67
+ expect(parsed.val.status).toBe(ConnectionStatuses.REVOKED);
68
+ });
69
+ });
70
+
71
+ describe('transformConnectedAccountResponse — REVOKED preserves state', () => {
72
+ const baseResponse = {
73
+ id: 'conn_123',
74
+ auth_config: {
75
+ id: 'auth_123',
76
+ auth_scheme: 'OAUTH2',
77
+ is_composio_managed: true,
78
+ is_disabled: false,
79
+ },
80
+ user_id: 'user_123',
81
+ data: {},
82
+ params: {},
83
+ is_disabled: false,
84
+ created_at: '2023-01-01T00:00:00Z',
85
+ updated_at: '2023-01-01T00:00:00Z',
86
+ status_reason: 'token revoked at provider',
87
+ toolkit: { slug: 'gmail' },
88
+ test_request_endpoint: '',
89
+ } as const;
90
+
91
+ it('does not drop state for an OAUTH2 REVOKED payload', () => {
92
+ const transformed = transformConnectedAccountResponse({
93
+ ...baseResponse,
94
+ status: ConnectionStatuses.REVOKED,
95
+ state: {
96
+ authScheme: AuthSchemeTypes.OAUTH2,
97
+ val: {
98
+ status: ConnectionStatuses.REVOKED,
99
+ revoked_at: '2026-05-01T00:00:00Z',
100
+ },
101
+ },
102
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
103
+ } as any);
104
+
105
+ expect(transformed.status).toBe(ConnectionStatuses.REVOKED);
106
+ expect(transformed.state).toBeDefined();
107
+ expect(transformed.state?.authScheme).toBe(AuthSchemeTypes.OAUTH2);
108
+ expect(transformed.state?.val.status).toBe(ConnectionStatuses.REVOKED);
109
+ });
110
+
111
+ it('does not drop state for an S2S_OAUTH2 REVOKED payload', () => {
112
+ const transformed = transformConnectedAccountResponse({
113
+ ...baseResponse,
114
+ auth_config: { ...baseResponse.auth_config, auth_scheme: 'S2S_OAUTH2' },
115
+ status: ConnectionStatuses.REVOKED,
116
+ state: {
117
+ authScheme: AuthSchemeTypes.S2S_OAUTH2,
118
+ val: {
119
+ status: ConnectionStatuses.REVOKED,
120
+ revoked_at: '2026-05-01T00:00:00Z',
121
+ },
122
+ },
123
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
124
+ } as any);
125
+
126
+ expect(transformed.state).toBeDefined();
127
+ expect(transformed.state?.authScheme).toBe(AuthSchemeTypes.S2S_OAUTH2);
128
+ });
129
+ });
130
+
131
+ describe('ConnectionStatuses enum closure', () => {
132
+ it('enumerates exactly the values this PR contracts for', () => {
133
+ // Closure test — if Apollo adds a new status, the SDK should mirror it
134
+ // here on purpose, not by accident. Compare the enum object's value set
135
+ // to the set this PR ships.
136
+ expect(new Set(Object.values(ConnectionStatuses))).toEqual(
137
+ new Set(['INITIALIZING', 'INITIATED', 'ACTIVE', 'FAILED', 'EXPIRED', 'INACTIVE', 'REVOKED'])
138
+ );
139
+ });
140
+ });
141
+});