|
| 1 | +/** |
| 2 | + * Fork: `headlessUsageRefreshInterval` through the real settings service: old files, patches, |
| 3 | + * the sparse settings file and restarts. |
| 4 | + */ |
| 5 | +import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 6 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 7 | +import { DEFAULT_SERVER_SETTINGS, ServerSettings, ServerSettingsPatch } from "@t3tools/contracts"; |
| 8 | +import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings"; |
| 9 | +import { assert, it } from "@effect/vitest"; |
| 10 | +import * as Duration from "effect/Duration"; |
| 11 | +import * as Effect from "effect/Effect"; |
| 12 | +import * as FileSystem from "effect/FileSystem"; |
| 13 | +import * as Layer from "effect/Layer"; |
| 14 | +import * as Schema from "effect/Schema"; |
| 15 | + |
| 16 | +import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; |
| 17 | +import * as ServerConfig from "../config.ts"; |
| 18 | +import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
| 19 | +import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; |
| 20 | +import * as ServerSettingsModule from "../serverSettings.ts"; |
| 21 | + |
| 22 | +const decodePatch = Schema.decodeUnknownSync(ServerSettingsPatch); |
| 23 | +const decodeSettingsJson = Schema.decodeUnknownEffect(Schema.fromJsonString(ServerSettings)); |
| 24 | + |
| 25 | +/** One settings service; each provide is one server run over the same settings file. */ |
| 26 | +const serverRun = () => |
| 27 | + ServerSettingsModule.layer.pipe( |
| 28 | + Layer.provide(ServerSecretStore.layer), |
| 29 | + Layer.provide(ManagedPolicy.layerTest()), |
| 30 | + Layer.provideMerge(Layer.fresh(SqlitePersistence.layerMemory)), |
| 31 | + ); |
| 32 | + |
| 33 | +const minutesOf = (value: Duration.Duration) => Duration.toMillis(value) / 60_000; |
| 34 | + |
| 35 | +it("a patch keeps the interval a whole Duration and leaves it alone when unrelated", () => { |
| 36 | + const set = applyServerSettingsPatch( |
| 37 | + DEFAULT_SERVER_SETTINGS, |
| 38 | + decodePatch({ headlessUsageRefreshInterval: 15 * 60_000 }), |
| 39 | + ); |
| 40 | + assert.isTrue(Duration.isDuration(set.headlessUsageRefreshInterval)); |
| 41 | + assert.equal(minutesOf(set.headlessUsageRefreshInterval), 15); |
| 42 | + const unrelated = applyServerSettingsPatch(set, decodePatch({ enableAgentBrowserAccess: false })); |
| 43 | + assert.equal(minutesOf(unrelated.headlessUsageRefreshInterval), 15); |
| 44 | + const off = applyServerSettingsPatch(set, decodePatch({ headlessUsageRefreshInterval: 0 })); |
| 45 | + assert.equal(Duration.toMillis(off.headlessUsageRefreshInterval), 0); |
| 46 | +}); |
| 47 | + |
| 48 | +it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("settings file", (it) => { |
| 49 | + it.effect("an old file gets the default; a saved interval survives a restart, unchanged", () => |
| 50 | + Effect.gen(function* () { |
| 51 | + const { settingsPath } = yield* ServerConfig.ServerConfig; |
| 52 | + const fs = yield* FileSystem.FileSystem; |
| 53 | + // Old state: a settings file written before the setting existed. |
| 54 | + yield* fs.writeFileString(settingsPath, `{ "enableAgentBrowserAccess": false }`); |
| 55 | + const first = yield* Effect.gen(function* () { |
| 56 | + const service = yield* ServerSettingsModule.ServerSettingsService; |
| 57 | + assert.equal(minutesOf((yield* service.getSettings).headlessUsageRefreshInterval), 10); |
| 58 | + const saved = yield* service.updateSettings( |
| 59 | + decodePatch({ headlessUsageRefreshInterval: 30 * 60_000 }), |
| 60 | + ); |
| 61 | + assert.isTrue(Duration.isDuration(saved.headlessUsageRefreshInterval)); |
| 62 | + assert.equal(minutesOf(saved.headlessUsageRefreshInterval), 30); |
| 63 | + // A second, unrelated save keeps it. |
| 64 | + yield* service.updateSettings(decodePatch({ enableAgentBrowserAccess: true })); |
| 65 | + return JSON.parse(yield* fs.readFileString(settingsPath)); |
| 66 | + }).pipe(Effect.provide(serverRun())); |
| 67 | + assert.equal(first.headlessUsageRefreshInterval, 30 * 60_000); |
| 68 | + assert.equal( |
| 69 | + minutesOf((yield* decodeSettingsJson(JSON.stringify(first))).headlessUsageRefreshInterval), |
| 70 | + 30, |
| 71 | + ); |
| 72 | + |
| 73 | + // A restart reads it back; turning it off and back to the default keeps the file sparse. |
| 74 | + const second = yield* Effect.gen(function* () { |
| 75 | + const service = yield* ServerSettingsModule.ServerSettingsService; |
| 76 | + assert.equal(minutesOf((yield* service.getSettings).headlessUsageRefreshInterval), 30); |
| 77 | + yield* service.updateSettings(decodePatch({ headlessUsageRefreshInterval: 0 })); |
| 78 | + const off = JSON.parse(yield* fs.readFileString(settingsPath)); |
| 79 | + yield* service.updateSettings(decodePatch({ headlessUsageRefreshInterval: 10 * 60_000 })); |
| 80 | + return { off, back: JSON.parse(yield* fs.readFileString(settingsPath)) }; |
| 81 | + }).pipe(Effect.provide(serverRun())); |
| 82 | + assert.equal(second.off.headlessUsageRefreshInterval, 0); |
| 83 | + assert.notProperty(second.back, "headlessUsageRefreshInterval"); |
| 84 | + }).pipe( |
| 85 | + Effect.provide( |
| 86 | + Layer.fresh( |
| 87 | + ServerConfig.layerTest(process.cwd(), { prefix: "t3code-headless-usage-settings-test-" }), |
| 88 | + ), |
| 89 | + ), |
| 90 | + ), |
| 91 | + ); |
| 92 | +}); |