-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathupdate-org-config-handler.test.ts
More file actions
163 lines (138 loc) · 5.1 KB
/
Copy pathupdate-org-config-handler.test.ts
File metadata and controls
163 lines (138 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Regression: PATCH /organization/config must merge, not replace. A prior
* `OrgConfigSchema.partial()` body schema filled every field's default, so each
* save overwrote previously-set flags. These tests drive the real HTTP handler.
*/
import { expect, test } from "bun:test";
import {
type DbUsageAlert,
type OrgConfig,
OrgConfigSchema,
organizations,
} from "@autumn/shared";
import { initScenario } from "@tests/utils/testInitUtils/initScenario";
import chalk from "chalk";
import { eq } from "drizzle-orm";
import { AutumnInt } from "@/external/autumn/autumnCli.js";
test(`${chalk.yellowBright("org config handler: second save preserves first save")}`, async () => {
const { ctx } = await initScenario({ setup: [], actions: [] });
const { db, org } = ctx;
const originalConfig = org.config;
const readDbConfig = async (): Promise<OrgConfig> => {
const [row] = await db
.select({ config: organizations.config })
.from(organizations)
.where(eq(organizations.id, org.id))
.limit(1);
return OrgConfigSchema.parse(row?.config ?? {});
};
try {
// Known empty starting state.
await db
.update(organizations)
.set({ config: {} as OrgConfig })
.where(eq(organizations.id, org.id));
const autumn = new AutumnInt({ secretKey: ctx.orgSecretKey });
// First save: one toggle.
const firstResponse = (await autumn.patch("/organization/config", {
automatic_tax: true,
})) as { success: boolean; config: OrgConfig };
expect(firstResponse.config.automatic_tax).toBe(true);
// Second save: a different toggle.
const secondResponse = (await autumn.patch("/organization/config", {
cancel_on_past_due: true,
})) as { success: boolean; config: OrgConfig };
// The regression assertions: the first field must survive the second save.
expect(secondResponse.config.automatic_tax).toBe(true);
expect(secondResponse.config.cancel_on_past_due).toBe(true);
const persisted = await readDbConfig();
expect(persisted.automatic_tax).toBe(true);
expect(persisted.cancel_on_past_due).toBe(true);
} finally {
await db
.update(organizations)
.set({ config: originalConfig })
.where(eq(organizations.id, org.id));
}
});
/**
* Guards the mixed-type path: `usage_alerts` (an array) is saved through the
* same endpoint and must coexist with previously-set boolean flags.
*/
test(`${chalk.yellowBright("org config handler: array field (usage_alerts) merges with boolean flags")}`, async () => {
const { ctx } = await initScenario({ setup: [], actions: [] });
const { db, org } = ctx;
const originalConfig = org.config;
const readDbConfig = async (): Promise<OrgConfig> => {
const [row] = await db
.select({ config: organizations.config })
.from(organizations)
.where(eq(organizations.id, org.id))
.limit(1);
return OrgConfigSchema.parse(row?.config ?? {});
};
const alert: DbUsageAlert = {
enabled: true,
threshold: 80,
threshold_type: "usage_percentage",
basis: "balance",
};
try {
await db
.update(organizations)
.set({ config: {} as OrgConfig })
.where(eq(organizations.id, org.id));
const autumn = new AutumnInt({ secretKey: ctx.orgSecretKey });
// Boolean toggle first.
await autumn.patch("/organization/config", { automatic_tax: true });
// Then an array field through the same endpoint.
const response = (await autumn.patch("/organization/config", {
usage_alerts: [alert],
})) as { success: boolean; config: OrgConfig };
expect(response.config.automatic_tax).toBe(true);
expect(response.config.usage_alerts).toHaveLength(1);
expect(response.config.usage_alerts?.[0]?.threshold).toBe(80);
const persisted = await readDbConfig();
expect(persisted.automatic_tax).toBe(true);
expect(persisted.usage_alerts).toHaveLength(1);
} finally {
await db
.update(organizations)
.set({ config: originalConfig })
.where(eq(organizations.id, org.id));
}
});
test(`${chalk.yellowBright("org config handler: disable_overage_billing saves and preserves other fields")}`, async () => {
const { ctx } = await initScenario({ setup: [], actions: [] });
const { db, org } = ctx;
const originalConfig = org.config;
const readDbConfig = async (): Promise<OrgConfig> => {
const [row] = await db
.select({ config: organizations.config })
.from(organizations)
.where(eq(organizations.id, org.id))
.limit(1);
return OrgConfigSchema.parse(row?.config ?? {});
};
try {
await db
.update(organizations)
.set({ config: {} as OrgConfig })
.where(eq(organizations.id, org.id));
const autumn = new AutumnInt({ secretKey: ctx.orgSecretKey });
await autumn.patch("/organization/config", { automatic_tax: true });
const response = (await autumn.patch("/organization/config", {
disable_overage_billing: true,
})) as { success: boolean; config: OrgConfig };
expect(response.config.disable_overage_billing).toBe(true);
expect(response.config.automatic_tax).toBe(true);
const persisted = await readDbConfig();
expect(persisted.disable_overage_billing).toBe(true);
expect(persisted.automatic_tax).toBe(true);
} finally {
await db
.update(organizations)
.set({ config: originalConfig })
.where(eq(organizations.id, org.id));
}
});