runtime config와 release manifest는 빈 값이 아닌 build·release id를 받는데, telemetry와 diagnostics는 64자 ASCII 규칙으로 본다. `+`·한글이 있거나 긴 id로 배포하면 release.mismatch.detected와 부팅·렌더 실패 기록이 통째로 버려진다. build_id·release_id·active_release_id 셋과 Service Worker manifest의 releaseId를 contracts의 규칙 하나(제어문자 없는 1~512자, code point)로 본다. 규칙 모듈은 import가 없어 worker가 관측 모듈을 끌어오지 않는다. manifest의 buildId 규칙은 그대로다. 공용 규칙을 넓히는 안은 다른 속성의 개인정보·cardinality 방어가 약해지고, 자르거나 hash하는 안은 collector가 붙이는 원값과 달라져 배포별로 묶을 수 없어 버렸다. `+`만 더하는 안은 긴 ref와 한글이 남는다. 위험: 이벤트 하나가 길어질 수 있다(값마다 최대 512자). diagnostics가 이 세 속성에 숫자·boolean을 더는 받지 않는다. worker 번들과 초기 JS 크기는 merge 단계 전에는 재지 않았다. 검증(노트북): 고치기 전 코드에 이 커밋의 시험을 얹으면 두 시험 파일 142개 가운데 30개가 실패한다. 이 트리에서는 142개와 manifest decoder 시험이 통과한다. check:types, lint, check:architecture, check:adapter-inventory, test:unit, test:contract, test:runtime-schema 통과. Refs: REV-FE-0042
212 lines
6.8 KiB
TypeScript
212 lines
6.8 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { createApplication } from "../../src/application/create-application.ts";
|
|
import { createAnonymousSessionAdapter } from "../../src/adapters/auth/external-session-adapter.ts";
|
|
import { createDiagnosticsAdapter } from "../../src/adapters/diagnostics/bounded-diagnostics.ts";
|
|
import { createTelemetryAdapter } from "../../src/adapters/telemetry/best-effort-telemetry.ts";
|
|
import { createTelemetryCollector } from "../../src/adapters/telemetry/telemetry-collector.ts";
|
|
import type { StoragePort } from "../../src/application/ports/storage-port.ts";
|
|
import type { DiagnosticsPort } from "../../src/application/ports/diagnostics-port.ts";
|
|
import type { TelemetryPort } from "../../src/application/ports/telemetry-port.ts";
|
|
import { createProductFeaturesStub, createRuntimeCapabilitiesStub } from "../helpers/runtime-capabilities-stub.ts";
|
|
|
|
type ReleaseFixture = {
|
|
buildId: string;
|
|
releaseId: string;
|
|
configSchemaVersion: string;
|
|
apiContractVersion: string;
|
|
assetManifestHash: string;
|
|
routeChunks: Record<string, string>;
|
|
};
|
|
|
|
function release(buildId: string, releaseId: string): ReleaseFixture {
|
|
return {
|
|
buildId,
|
|
releaseId,
|
|
configSchemaVersion: "1",
|
|
apiContractVersion: "1",
|
|
assetManifestHash: `${buildId}-assets`,
|
|
routeChunks: { "route-home": `assets/${buildId}-home.js` },
|
|
};
|
|
}
|
|
|
|
function memoryStorage(): StoragePort {
|
|
let value: unknown;
|
|
return {
|
|
read: () => ({ ok: true, value }),
|
|
write: (_name, next) => {
|
|
value = next;
|
|
return { ok: true };
|
|
},
|
|
remove: () => ({ ok: true }),
|
|
};
|
|
}
|
|
|
|
function applicationWith(options: {
|
|
storage?: StoragePort;
|
|
current?: ReleaseFixture;
|
|
refresh?: () => Promise<ReturnType<typeof release>>;
|
|
reload?: () => void;
|
|
diagnostics?: DiagnosticsPort;
|
|
telemetry?: TelemetryPort;
|
|
}) {
|
|
const current = options.current ?? release("build-a", "release-a");
|
|
return createApplication({
|
|
session: createAnonymousSessionAdapter(),
|
|
preferences: options.storage ?? memoryStorage(),
|
|
diagnostics: options.diagnostics ?? { record: () => {} },
|
|
telemetry: options.telemetry ?? { emit: () => {} },
|
|
runtimeCapabilities: createRuntimeCapabilitiesStub(),
|
|
productFeatures: createProductFeaturesStub(),
|
|
releaseInfo: {
|
|
getCurrent: async () => current,
|
|
refresh:
|
|
options.refresh ??
|
|
(async () => release("build-b", "release-b")),
|
|
},
|
|
navigation: { reload: options.reload ?? (() => {}) },
|
|
});
|
|
}
|
|
|
|
describe("production chunk recovery application input", () => {
|
|
it("reloads exactly once for one active build/release pair", async () => {
|
|
const reload = vi.fn();
|
|
const record = vi.fn<DiagnosticsPort["record"]>();
|
|
const emit = vi.fn<TelemetryPort["emit"]>();
|
|
const application = applicationWith({
|
|
reload,
|
|
diagnostics: { record },
|
|
telemetry: { emit },
|
|
});
|
|
const input = {
|
|
chunkId: "route-home",
|
|
failureKind: "CHUNK_LOAD_FAILURE" as const,
|
|
};
|
|
|
|
await expect(application.recovery.recoverChunk(input)).resolves.toEqual({
|
|
action: "reload-once",
|
|
releasePair: "build-a/release-a->build-b/release-b",
|
|
});
|
|
expect(record).toHaveBeenCalledWith({
|
|
level: "warn",
|
|
eventId: "release.mismatch.detected",
|
|
context: {
|
|
build_id: "build-a",
|
|
active_release_id: "release-b",
|
|
mismatch_kind: "BUILD_MISMATCH",
|
|
},
|
|
});
|
|
expect(emit).toHaveBeenCalledWith("release.mismatch.detected", {
|
|
build_id: "build-a",
|
|
active_release_id: "release-b",
|
|
mismatch_kind: "BUILD_MISMATCH",
|
|
});
|
|
await expect(application.recovery.recoverChunk(input)).resolves.toEqual({
|
|
action: "support",
|
|
reason: "reload-already-attempted",
|
|
});
|
|
expect(reload).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
// REV-FE-0042. Through the real adapters: the deploy's own ids reach both sinks.
|
|
it.each([
|
|
["a build suffix", "refs/tags/v1.0.0+build.7-9-1"],
|
|
["Hangul", "refs/heads/기능-12-1"],
|
|
["78 characters", "refs/heads/feature/a-long-branch-name-that-a-derived-service-might-use-12345-1"],
|
|
])("delivers a mismatch to a release id with %s unchanged", async (_case, releaseId) => {
|
|
const buildId = "build+7";
|
|
const fetcher = vi.fn<typeof fetch>(async () => new Response(null, { status: 204 }));
|
|
const endpoint = "https://telemetry.test/events";
|
|
const telemetry = createTelemetryAdapter({
|
|
enabled: true,
|
|
endpoint,
|
|
schedule: () => {},
|
|
collector: createTelemetryCollector({
|
|
endpoint,
|
|
fetcher,
|
|
release: { buildId, releaseId: "release-a" },
|
|
}),
|
|
});
|
|
const diagnostics = createDiagnosticsAdapter();
|
|
const application = applicationWith({
|
|
current: release(buildId, "release-a"),
|
|
refresh: async () => release(buildId, releaseId),
|
|
diagnostics,
|
|
telemetry,
|
|
});
|
|
|
|
await application.recovery.recoverChunk({
|
|
chunkId: "route-home",
|
|
failureKind: "CHUNK_LOAD_FAILURE",
|
|
});
|
|
await telemetry.flush();
|
|
|
|
const attributes = {
|
|
build_id: buildId,
|
|
active_release_id: releaseId,
|
|
mismatch_kind: "RELEASE_MISMATCH",
|
|
};
|
|
expect(diagnostics.dropped()).toEqual({});
|
|
expect(diagnostics.entries()).toMatchObject([
|
|
{ eventId: "release.mismatch.detected", context: attributes },
|
|
]);
|
|
expect(telemetry.dropReasons()).toEqual({});
|
|
expect(fetcher).toHaveBeenCalledOnce();
|
|
expect(JSON.parse(String(fetcher.mock.calls[0]?.[1]?.body))).toMatchObject({
|
|
events: [{ eventName: "release.mismatch.detected", attributes }],
|
|
});
|
|
telemetry.dispose();
|
|
});
|
|
|
|
it.each([
|
|
[
|
|
{
|
|
refresh: async () => {
|
|
throw new Error("offline");
|
|
},
|
|
},
|
|
"manifest-unavailable",
|
|
],
|
|
[
|
|
{
|
|
refresh: async () => ({
|
|
...release("build-b", "release-b"),
|
|
routeChunks: {},
|
|
}),
|
|
},
|
|
"active-chunk-unknown",
|
|
],
|
|
[
|
|
{
|
|
storage: {
|
|
read: () => ({
|
|
ok: false as const,
|
|
error: {
|
|
kind: "STORAGE_UNAVAILABLE" as const,
|
|
code: "STORAGE_UNAVAILABLE",
|
|
retryable: false,
|
|
operationId: "STORAGE",
|
|
attemptCount: 1,
|
|
userMessageKey: "error.storage_unavailable",
|
|
action: "none" as const,
|
|
},
|
|
}),
|
|
write: () => ({ ok: true as const }),
|
|
remove: () => ({ ok: true as const }),
|
|
},
|
|
},
|
|
"guard-read-failed",
|
|
],
|
|
])("fails closed for recovery dependency case %#", async (options, reason) => {
|
|
const reload = vi.fn();
|
|
const application = applicationWith({ ...options, reload });
|
|
await expect(
|
|
application.recovery.recoverChunk({
|
|
chunkId: "route-home",
|
|
failureKind: "CHUNK_LOAD_FAILURE",
|
|
}),
|
|
).resolves.toEqual({ action: "support", reason });
|
|
expect(reload).not.toHaveBeenCalled();
|
|
});
|
|
});
|