Skip to content
Merged
85 changes: 85 additions & 0 deletions apps/server/src/usage/UsageLimitSources.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import { expect, it } from "@effect/vitest";
import { ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import { HttpClient } from "effect/http";

import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
import * as ServerSettings from "../serverSettings.ts";
import * as UsageLimitSources from "./UsageLimitSources.ts";

const encodeConfigEvent = Schema.encodeSync(ServerConfigStreamEvent);

it.effect("keeps malformed source URLs encodable for config subscribers", () =>
Effect.gen(function* () {
const sources = yield* UsageLimitSources.make;
yield* sources.refresh;
const snapshots = yield* sources.current;
expect(snapshots.map((source) => source.label)).toEqual([
"local-hub",
"opaque-hub",
"Named hub",
"hub.test:8317",
"Usage limit source",
]);
expect(() =>
encodeConfigEvent({
version: 1,
type: "usageLimitSourcesUpdated",
payload: { sources: snapshots },
}),
).not.toThrow();
expect(snapshots.every((source) => source.error === "No management key configured.")).toBe(
true,
);
}).pipe(
Effect.provide([
ServerSettings.layerTest({
usageLimitSources: {
[UsageLimitSourceId.make("local-hub")]: {
kind: "cliproxy",
url: "localhost:8317",
managementKey: "",
enabled: true,
},
[UsageLimitSourceId.make("opaque-hub")]: {
kind: "cliproxy",
url: "file:///tmp/hub",
managementKey: "",
enabled: true,
},
[UsageLimitSourceId.make("named-hub")]: {
kind: "cliproxy",
url: "localhost:8317",
label: "Named hub",
managementKey: "",
enabled: true,
},
[UsageLimitSourceId.make("valid-hub")]: {
kind: "cliproxy",
url: "https://hub.test:8317",
managementKey: "",
enabled: true,
},
[UsageLimitSourceId.make(" ")]: {
kind: "cliproxy",
url: "localhost:8317",
managementKey: "",
enabled: true,
},
},
}),
Layer.mock(BackgroundPolicy.BackgroundPolicy)({
shouldRunScopeWork: () => Effect.succeed(false),
}),
Layer.succeed(
HttpClient.HttpClient,
HttpClient.make(() => Effect.die("Unexpected network request")),
),
NodeCrypto.layer,
]),
Effect.scoped,
),
);
5 changes: 3 additions & 2 deletions apps/server/src/usage/UsageLimitSources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ export class UsageLimitSources extends Context.Service<

function sourceLabel(id: string, config: UsageLimitSourceConfig): string {
if (config.label) return config.label;
const fallback = id.trim() || "Usage limit source";
try {
return new URL(config.url).host;
return new URL(config.url).host || fallback;
} catch {
return id;
return fallback;
}
}

Expand Down
Loading