





















@@ -29,10 +29,19 @@ const {
2929 registerUnhandledRejectionHandler,
3030 logger,
3131} = mocks;
32+const dnsLabelEncoder = new TextEncoder();
32333334const asString = (value: unknown, fallback: string) =>
3435typeof value === "string" && value.trim() ? value : fallback;
353637+function expectDnsLabelByteLength(value: string, expected: number) {
38+expect(dnsLabelEncoder.encode(value).byteLength).toBe(expected);
39+}
40+41+function expectDnsLabelWithinLimit(value: string) {
42+expect(dnsLabelEncoder.encode(value).byteLength).toBeLessThanOrEqual(63);
43+}
44+3645function enableAdvertiserUnitMode(hostname = "test-host") {
3746// Allow advertiser to run in unit tests.
3847delete process.env.VITEST;
@@ -735,8 +744,32 @@ describe("gateway bonjour advertiser", () => {
735744await started.stop();
736745});
737746738-it("truncates service name exceeding 63-byte DNS label limit", async () => {
739-const longHostname = "app-41627eae5842473f9e05f139ea307277-7f9477f4d6-lqqzf-abcdefghi";
747+it("truncates reported Kubernetes service name at the DNS label byte limit", async () => {
748+const reportedHostname = "app-41627eae5842473f9e05f139ea307277-7f9477f4d6-lqqzf";
749+enableAdvertiserUnitMode(reportedHostname);
750+751+const destroy = vi.fn().mockResolvedValue(undefined);
752+const advertise = vi.fn().mockResolvedValue(undefined);
753+mockCiaoService({ advertise, destroy });
754+755+const started = await startAdvertiser({
756+gatewayPort: 18789,
757+sshPort: 2222,
758+});
759+760+const [gatewayCall] = createService.mock.calls as Array<[ServiceCall]>;
761+const serviceName = gatewayCall?.[0]?.name as string;
762+const hostname = gatewayCall?.[0]?.hostname as string;
763+764+expectDnsLabelByteLength(`${reportedHostname} (OpenClaw)`, 64);
765+expect(hostname).toBe(reportedHostname);
766+expectDnsLabelWithinLimit(serviceName);
767+768+await started.stop();
769+});
770+771+it("truncates host labels exceeding the 63-byte DNS label limit", async () => {
772+const longHostname = "app-41627eae5842473f9e05f139ea307277-7f9477f4d6-lqqzf-abcdefghij";
740773enableAdvertiserUnitMode(longHostname);
741774742775const destroy = vi.fn().mockResolvedValue(undefined);
@@ -752,10 +785,11 @@ describe("gateway bonjour advertiser", () => {
752785const serviceName = gatewayCall?.[0]?.name as string;
753786const hostname = gatewayCall?.[0]?.hostname as string;
754787755-// Both name and hostname must be within the 63-byte DNS label limit
756-expect(new TextEncoder().encode(serviceName).byteLength).toBeLessThanOrEqual(63);
757-expect(new TextEncoder().encode(hostname).byteLength).toBeLessThanOrEqual(63);
788+expectDnsLabelByteLength(longHostname, 64);
789+expectDnsLabelByteLength(hostname, 63);
790+expect(hostname).toBe(longHostname.slice(0, -1));
758791expect(hostname).not.toMatch(/-$/);
792+expectDnsLabelWithinLimit(serviceName);
759793760794await started.stop();
761795});
@@ -777,8 +811,7 @@ describe("gateway bonjour advertiser", () => {
777811const [gatewayCall] = createService.mock.calls as Array<[ServiceCall]>;
778812const serviceName = gatewayCall?.[0]?.name as string;
779813780-expect(new TextEncoder().encode(serviceName).byteLength).toBeLessThanOrEqual(63);
781-// Should not end with a replacement character from incomplete multi-byte truncation
814+expectDnsLabelWithinLimit(serviceName);
782815expect(serviceName).not.toMatch(/\uFFFD$/);
783816784817await started.stop();
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。