system-config-save.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import { locales } from "@/i18n/config";
  3. // Mock dependencies
  4. const getSessionMock = vi.fn();
  5. const revalidatePathMock = vi.fn();
  6. const invalidateSystemSettingsCacheMock = vi.fn();
  7. const updateSystemSettingsMock = vi.fn();
  8. const getSystemSettingsMock = vi.fn();
  9. vi.mock("@/lib/auth", () => ({
  10. getSession: () => getSessionMock(),
  11. }));
  12. vi.mock("next/cache", () => ({
  13. revalidatePath: (...args: unknown[]) => revalidatePathMock(...args),
  14. }));
  15. vi.mock("@/lib/config", () => ({
  16. invalidateSystemSettingsCache: () => invalidateSystemSettingsCacheMock(),
  17. }));
  18. vi.mock("@/lib/logger", () => ({
  19. logger: {
  20. trace: vi.fn(),
  21. debug: vi.fn(),
  22. info: vi.fn(),
  23. warn: vi.fn(),
  24. error: vi.fn(),
  25. },
  26. }));
  27. vi.mock("@/lib/utils/timezone", () => ({
  28. resolveSystemTimezone: vi.fn(async () => "UTC"),
  29. isValidIANATimezone: vi.fn(() => true),
  30. }));
  31. vi.mock("@/repository/system-config", () => ({
  32. getSystemSettings: () => getSystemSettingsMock(),
  33. updateSystemSettings: (...args: unknown[]) => updateSystemSettingsMock(...args),
  34. }));
  35. // Import the action after mocks are set up
  36. import { saveSystemSettings } from "@/actions/system-config";
  37. describe("saveSystemSettings", () => {
  38. beforeEach(() => {
  39. vi.clearAllMocks();
  40. // Default: admin session
  41. getSessionMock.mockResolvedValue({ user: { id: 1, role: "admin" } });
  42. // Default: successful update
  43. updateSystemSettingsMock.mockResolvedValue({
  44. id: 1,
  45. siteTitle: "Test Site",
  46. allowGlobalUsageView: false,
  47. currencyDisplay: "CNY",
  48. billingModelSource: "original",
  49. timezone: null,
  50. enableAutoCleanup: false,
  51. cleanupRetentionDays: 30,
  52. cleanupSchedule: "0 3 * * *",
  53. cleanupBatchSize: 1000,
  54. enableClientVersionCheck: false,
  55. verboseProviderError: false,
  56. enableHttp2: false,
  57. interceptAnthropicWarmupRequests: false,
  58. enableThinkingSignatureRectifier: false,
  59. enableCodexSessionIdCompletion: false,
  60. enableResponseFixer: false,
  61. responseFixerConfig: {
  62. fixEncoding: false,
  63. fixStreamingJson: false,
  64. fixEmptyResponse: false,
  65. fixContentBlockDelta: false,
  66. maxRetries: 3,
  67. timeout: 5000,
  68. },
  69. quotaDbRefreshIntervalSeconds: 60,
  70. quotaLeasePercent5h: 0.05,
  71. quotaLeasePercentDaily: 0.05,
  72. quotaLeasePercentWeekly: 0.05,
  73. quotaLeasePercentMonthly: 0.05,
  74. quotaLeaseCapUsd: null,
  75. createdAt: new Date(),
  76. updatedAt: new Date(),
  77. });
  78. });
  79. it("should return error when user is not admin", async () => {
  80. getSessionMock.mockResolvedValue({ user: { id: 1, role: "user" } });
  81. const result = await saveSystemSettings({ siteTitle: "New Title" });
  82. expect(result.ok).toBe(false);
  83. expect(result.error).toContain("无权限");
  84. expect(updateSystemSettingsMock).not.toHaveBeenCalled();
  85. });
  86. it("should return error when user is not logged in", async () => {
  87. getSessionMock.mockResolvedValue(null);
  88. const result = await saveSystemSettings({ siteTitle: "New Title" });
  89. expect(result.ok).toBe(false);
  90. expect(result.error).toContain("无权限");
  91. expect(updateSystemSettingsMock).not.toHaveBeenCalled();
  92. });
  93. it("should call updateSystemSettings with validated data", async () => {
  94. const result = await saveSystemSettings({
  95. siteTitle: "New Site Title",
  96. verboseProviderError: true,
  97. });
  98. expect(result.ok).toBe(true);
  99. expect(updateSystemSettingsMock).toHaveBeenCalledWith(
  100. expect.objectContaining({
  101. siteTitle: "New Site Title",
  102. verboseProviderError: true,
  103. })
  104. );
  105. });
  106. it("should invalidate system settings cache after successful save", async () => {
  107. await saveSystemSettings({ siteTitle: "New Title" });
  108. expect(invalidateSystemSettingsCacheMock).toHaveBeenCalled();
  109. });
  110. describe("revalidatePath locale coverage", () => {
  111. it("should revalidate paths for ALL supported locales", async () => {
  112. await saveSystemSettings({ siteTitle: "New Title" });
  113. // Collect all revalidatePath calls
  114. const calls = revalidatePathMock.mock.calls.map((call) => call[0]);
  115. // Check that each locale's settings/config path is revalidated
  116. for (const locale of locales) {
  117. const expectedSettingsPath = `/${locale}/settings/config`;
  118. expect(calls).toContain(expectedSettingsPath);
  119. }
  120. });
  121. it("should revalidate dashboard paths for ALL supported locales", async () => {
  122. await saveSystemSettings({ siteTitle: "New Title" });
  123. const calls = revalidatePathMock.mock.calls.map((call) => call[0]);
  124. // Check that each locale's dashboard path is revalidated
  125. for (const locale of locales) {
  126. const expectedDashboardPath = `/${locale}/dashboard`;
  127. expect(calls).toContain(expectedDashboardPath);
  128. }
  129. });
  130. it("should revalidate root layout", async () => {
  131. await saveSystemSettings({ siteTitle: "New Title" });
  132. // Check that root layout is revalidated
  133. expect(revalidatePathMock).toHaveBeenCalledWith("/", "layout");
  134. });
  135. it("should call revalidatePath at least 2 * locales.length + 1 times", async () => {
  136. await saveSystemSettings({ siteTitle: "New Title" });
  137. // 2 paths per locale (settings/config + dashboard) + 1 for root layout
  138. const expectedMinCalls = locales.length * 2 + 1;
  139. expect(revalidatePathMock).toHaveBeenCalledTimes(expectedMinCalls);
  140. });
  141. });
  142. it("should return updated settings on success", async () => {
  143. const mockUpdated = {
  144. id: 1,
  145. siteTitle: "Updated Title",
  146. allowGlobalUsageView: true,
  147. currencyDisplay: "USD",
  148. billingModelSource: "original",
  149. timezone: "America/New_York",
  150. enableAutoCleanup: false,
  151. cleanupRetentionDays: 30,
  152. cleanupSchedule: "0 3 * * *",
  153. cleanupBatchSize: 1000,
  154. enableClientVersionCheck: false,
  155. verboseProviderError: true,
  156. enableHttp2: true,
  157. interceptAnthropicWarmupRequests: false,
  158. enableThinkingSignatureRectifier: false,
  159. enableCodexSessionIdCompletion: false,
  160. enableResponseFixer: false,
  161. responseFixerConfig: {
  162. fixEncoding: false,
  163. fixStreamingJson: false,
  164. fixEmptyResponse: false,
  165. fixContentBlockDelta: false,
  166. maxRetries: 3,
  167. timeout: 5000,
  168. },
  169. quotaDbRefreshIntervalSeconds: 60,
  170. quotaLeasePercent5h: 0.05,
  171. quotaLeasePercentDaily: 0.05,
  172. quotaLeasePercentWeekly: 0.05,
  173. quotaLeasePercentMonthly: 0.05,
  174. quotaLeaseCapUsd: null,
  175. createdAt: new Date(),
  176. updatedAt: new Date(),
  177. };
  178. updateSystemSettingsMock.mockResolvedValue(mockUpdated);
  179. const result = await saveSystemSettings({
  180. siteTitle: "Updated Title",
  181. allowGlobalUsageView: true,
  182. currencyDisplay: "USD",
  183. timezone: "America/New_York",
  184. verboseProviderError: true,
  185. enableHttp2: true,
  186. });
  187. expect(result.ok).toBe(true);
  188. expect(result.data).toEqual(mockUpdated);
  189. });
  190. it("should handle repository errors gracefully", async () => {
  191. updateSystemSettingsMock.mockRejectedValue(new Error("Database error"));
  192. const result = await saveSystemSettings({ siteTitle: "New Title" });
  193. expect(result.ok).toBe(false);
  194. expect(result.error).toContain("Database error");
  195. });
  196. });