Files
t3code/packages/client-runtime/src/platform/storageDocument.test.ts
T

283 lines
9.1 KiB
TypeScript

import { EnvironmentId } from "@t3tools/contracts";
import { describe, expect, it } from "@effect/vitest";
import * as Schema from "effect/Schema";
import * as TokenStore from "../authorization/tokenStore.ts";
import {
BearerConnectionCredential,
BearerConnectionProfile,
BearerConnectionRegistration,
RelayConnectionRegistration,
SshConnectionProfile,
SshConnectionRegistration,
} from "../connection/catalog.ts";
import {
BearerConnectionTarget,
RelayConnectionTarget,
SshConnectionTarget,
} from "../connection/model.ts";
import {
ConnectionCatalogDocument,
EMPTY_CONNECTION_CATALOG_DOCUMENT,
putRemoteDpopTokenInCatalog,
registerConnectionInCatalog,
removeConnectionFromCatalog,
setConnectionEnabledInCatalog,
} from "./storageDocument.ts";
const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
const decodeCatalogDocument = Schema.decodeUnknownSync(ConnectionCatalogDocument);
const RELAY_TARGET = new RelayConnectionTarget({
environmentId: ENVIRONMENT_ID,
label: "Remote",
});
const BEARER_TARGET = new BearerConnectionTarget({
environmentId: ENVIRONMENT_ID,
label: "Remote",
connectionId: "bearer-1",
});
const BEARER_PROFILE = new BearerConnectionProfile({
connectionId: BEARER_TARGET.connectionId,
environmentId: ENVIRONMENT_ID,
label: BEARER_TARGET.label,
httpBaseUrl: "https://remote.example.test",
wsBaseUrl: "wss://remote.example.test",
});
const BEARER_CREDENTIAL = new BearerConnectionCredential({
token: "bearer-token",
});
const REMOTE_TOKEN = new TokenStore.RemoteDpopAccessToken({
environmentId: ENVIRONMENT_ID,
label: "Remote",
endpoint: {
httpBaseUrl: "https://remote.example.test",
wsBaseUrl: "wss://remote.example.test",
providerKind: "cloudflare_tunnel",
},
accessToken: "dpop-token",
expiresAtEpochMs: 1_000_000,
dpopThumbprint: "thumbprint",
});
describe("ConnectionCatalogDocument", () => {
it.each([
{ name: "legacy", accountId: undefined },
{ name: "account-bound", accountId: "account-1" },
])("round-trips a catalog containing a $name DPoP token", ({ accountId }) => {
const token = new TokenStore.RemoteDpopAccessToken({
...REMOTE_TOKEN,
...(accountId === undefined ? {} : { accountId }),
});
const document = {
...EMPTY_CONNECTION_CATALOG_DOCUMENT,
targets: [RELAY_TARGET],
remoteDpopTokens: [token],
};
const schema = Schema.fromJsonString(ConnectionCatalogDocument);
const restored = Schema.decodeUnknownSync(schema)(Schema.encodeSync(schema)(document));
expect(restored).toEqual(document);
expect(restored.remoteDpopTokens[0]?.accountId).toBe(accountId);
});
it("registers a bearer connection as one catalog mutation", () => {
const document = registerConnectionInCatalog(
EMPTY_CONNECTION_CATALOG_DOCUMENT,
new BearerConnectionRegistration({
target: BEARER_TARGET,
profile: BEARER_PROFILE,
credential: BEARER_CREDENTIAL,
}),
);
expect(document.targets).toEqual([BEARER_TARGET]);
expect(document.profiles).toEqual([BEARER_PROFILE]);
expect(document.credentials).toEqual([
{
connectionId: BEARER_TARGET.connectionId,
credential: BEARER_CREDENTIAL,
},
]);
});
it("replaces obsolete connection metadata without discarding a reusable DPoP token", () => {
const bearer = registerConnectionInCatalog(
{
...EMPTY_CONNECTION_CATALOG_DOCUMENT,
remoteDpopTokens: [REMOTE_TOKEN],
},
new BearerConnectionRegistration({
target: BEARER_TARGET,
profile: BEARER_PROFILE,
credential: BEARER_CREDENTIAL,
}),
);
const relayTarget = new RelayConnectionTarget({
environmentId: ENVIRONMENT_ID,
label: "Remote",
});
const relay = registerConnectionInCatalog(
bearer,
new RelayConnectionRegistration({ target: relayTarget }),
);
expect(relay.targets).toEqual([relayTarget]);
expect(relay.profiles).toEqual([]);
expect(relay.credentials).toEqual([]);
expect(relay.remoteDpopTokens).toEqual([REMOTE_TOKEN]);
});
it("removes every catalog record owned by an explicit disconnect", () => {
const registered = registerConnectionInCatalog(
{
...EMPTY_CONNECTION_CATALOG_DOCUMENT,
remoteDpopTokens: [REMOTE_TOKEN],
},
new BearerConnectionRegistration({
target: BEARER_TARGET,
profile: BEARER_PROFILE,
credential: BEARER_CREDENTIAL,
}),
);
expect(removeConnectionFromCatalog(registered, BEARER_TARGET)).toEqual(
EMPTY_CONNECTION_CATALOG_DOCUMENT,
);
});
it("stores a DPoP token for a registered relay environment", () => {
const registered = registerConnectionInCatalog(
EMPTY_CONNECTION_CATALOG_DOCUMENT,
new RelayConnectionRegistration({ target: RELAY_TARGET }),
);
expect(putRemoteDpopTokenInCatalog(registered, REMOTE_TOKEN)).toEqual({
...registered,
remoteDpopTokens: [REMOTE_TOKEN],
});
});
it("ignores a late DPoP token after the environment is removed", () => {
const registered = registerConnectionInCatalog(
EMPTY_CONNECTION_CATALOG_DOCUMENT,
new RelayConnectionRegistration({ target: RELAY_TARGET }),
);
const removed = removeConnectionFromCatalog(registered, RELAY_TARGET);
expect(putRemoteDpopTokenInCatalog(removed, REMOTE_TOKEN)).toEqual(
EMPTY_CONNECTION_CATALOG_DOCUMENT,
);
});
it("removes a DPoP token stored before the environment is removed", () => {
const registered = registerConnectionInCatalog(
EMPTY_CONNECTION_CATALOG_DOCUMENT,
new RelayConnectionRegistration({ target: RELAY_TARGET }),
);
const refreshed = putRemoteDpopTokenInCatalog(registered, REMOTE_TOKEN);
expect(removeConnectionFromCatalog(refreshed, RELAY_TARGET)).toEqual(
EMPTY_CONNECTION_CATALOG_DOCUMENT,
);
});
it("does not store DPoP tokens for a different environment or connection kind", () => {
const bearer = registerConnectionInCatalog(
EMPTY_CONNECTION_CATALOG_DOCUMENT,
new BearerConnectionRegistration({
target: BEARER_TARGET,
profile: BEARER_PROFILE,
credential: BEARER_CREDENTIAL,
}),
);
const otherRelay = registerConnectionInCatalog(
EMPTY_CONNECTION_CATALOG_DOCUMENT,
new RelayConnectionRegistration({
target: new RelayConnectionTarget({
environmentId: EnvironmentId.make("environment-2"),
label: "Other relay",
}),
}),
);
expect(putRemoteDpopTokenInCatalog(bearer, REMOTE_TOKEN)).toBe(bearer);
expect(putRemoteDpopTokenInCatalog(otherRelay, REMOTE_TOKEN)).toBe(otherRelay);
});
it("decodes a document written before the disabled list existed", () => {
const decoded = decodeCatalogDocument({
schemaVersion: 1,
targets: [],
profiles: [],
credentials: [],
remoteDpopTokens: [],
});
expect(decoded.disabledEnvironmentIds).toEqual([]);
});
it("switches a saved environment off and back on without touching its records", () => {
const registered = registerConnectionInCatalog(
EMPTY_CONNECTION_CATALOG_DOCUMENT,
new BearerConnectionRegistration({
target: BEARER_TARGET,
profile: BEARER_PROFILE,
credential: BEARER_CREDENTIAL,
}),
);
const disabled = setConnectionEnabledInCatalog(registered, ENVIRONMENT_ID, false);
expect(disabled.disabledEnvironmentIds).toEqual([ENVIRONMENT_ID]);
expect(disabled.targets).toEqual(registered.targets);
expect(disabled.credentials).toEqual(registered.credentials);
// Idempotent: switching off twice stores the id once.
expect(
setConnectionEnabledInCatalog(disabled, ENVIRONMENT_ID, false).disabledEnvironmentIds,
).toEqual([ENVIRONMENT_ID]);
expect(
setConnectionEnabledInCatalog(disabled, ENVIRONMENT_ID, true).disabledEnvironmentIds,
).toEqual([]);
// Re-registering (editing label or URL) keeps the flag.
expect(
registerConnectionInCatalog(
disabled,
new BearerConnectionRegistration({
target: BEARER_TARGET,
profile: BEARER_PROFILE,
credential: BEARER_CREDENTIAL,
}),
).disabledEnvironmentIds,
).toEqual([ENVIRONMENT_ID]);
expect(removeConnectionFromCatalog(disabled, BEARER_TARGET).disabledEnvironmentIds).toEqual([]);
});
it("persists the normalized SSH profile beside its target", () => {
const target = new SshConnectionTarget({
environmentId: ENVIRONMENT_ID,
label: "SSH",
connectionId: "ssh-1",
});
const profile = new SshConnectionProfile({
connectionId: target.connectionId,
environmentId: target.environmentId,
label: target.label,
target: {
alias: "devbox",
hostname: "devbox.example.test",
username: "developer",
port: 22,
},
});
const document = registerConnectionInCatalog(
EMPTY_CONNECTION_CATALOG_DOCUMENT,
new SshConnectionRegistration({ target, profile }),
);
expect(document.targets).toEqual([target]);
expect(document.profiles).toEqual([profile]);
expect(document.credentials).toEqual([]);
});
});