fix: stabilize swift protocol generation and flaky tests

This commit is contained in:
Peter Steinberger 2026-02-21 16:53:41 +01:00
parent 8588183abe
commit fa89ae8e9e
6 changed files with 717 additions and 502 deletions

File diff suppressed because it is too large Load Diff

View File

@ -114,11 +114,10 @@ function emitStruct(name: string, schema: JsonSchema): string {
const props = schema.properties ?? {}; const props = schema.properties ?? {};
const required = new Set(schema.required ?? []); const required = new Set(schema.required ?? []);
const lines: string[] = []; const lines: string[] = [];
lines.push(`public struct ${name}: Codable, Sendable {`);
if (Object.keys(props).length === 0) { if (Object.keys(props).length === 0) {
lines.push("}\n"); return `public struct ${name}: Codable, Sendable {}\n`;
return lines.join("\n");
} }
lines.push(`public struct ${name}: Codable, Sendable {`);
const codingKeys: string[] = []; const codingKeys: string[] = [];
for (const [key, propSchema] of Object.entries(props)) { for (const [key, propSchema] of Object.entries(props)) {
const propName = safeName(key); const propName = safeName(key);
@ -139,14 +138,15 @@ function emitStruct(name: string, schema: JsonSchema): string {
return ` ${propName}: ${swiftType(prop, true)}${req ? "" : "?"}`; return ` ${propName}: ${swiftType(prop, true)}${req ? "" : "?"}`;
}) })
.join(",\n") + .join(",\n") +
"\n ) {\n" + ")\n" +
" {\n" +
Object.entries(props) Object.entries(props)
.map(([key]) => { .map(([key]) => {
const propName = safeName(key); const propName = safeName(key);
return ` self.${propName} = ${propName}`; return ` self.${propName} = ${propName}`;
}) })
.join("\n") + .join("\n") +
"\n }\n" + "\n }\n\n" +
" private enum CodingKeys: String, CodingKey {\n" + " private enum CodingKeys: String, CodingKey {\n" +
codingKeys.join("\n") + codingKeys.join("\n") +
"\n }\n}", "\n }\n}",
@ -173,11 +173,11 @@ function emitGatewayFrame(): string {
let type = try typeContainer.decode(String.self, forKey: .type) let type = try typeContainer.decode(String.self, forKey: .type)
switch type { switch type {
case "req": case "req":
self = .req(try RequestFrame(from: decoder)) self = try .req(RequestFrame(from: decoder))
case "res": case "res":
self = .res(try ResponseFrame(from: decoder)) self = try .res(ResponseFrame(from: decoder))
case "event": case "event":
self = .event(try EventFrame(from: decoder)) self = try .event(EventFrame(from: decoder))
default: default:
let container = try decoder.singleValueContainer() let container = try decoder.singleValueContainer()
let raw = try container.decode([String: AnyCodable].self) let raw = try container.decode([String: AnyCodable].self)
@ -187,10 +187,13 @@ function emitGatewayFrame(): string {
public func encode(to encoder: Encoder) throws { public func encode(to encoder: Encoder) throws {
switch self { switch self {
case .req(let v): try v.encode(to: encoder) case let .req(v):
case .res(let v): try v.encode(to: encoder) try v.encode(to: encoder)
case .event(let v): try v.encode(to: encoder) case let .res(v):
case .unknown(_, let raw): try v.encode(to: encoder)
case let .event(v):
try v.encode(to: encoder)
case let .unknown(_, raw):
var container = encoder.singleValueContainer() var container = encoder.singleValueContainer()
try container.encode(raw) try container.encode(raw)
} }
@ -201,7 +204,7 @@ function emitGatewayFrame(): string {
"public enum GatewayFrame: Codable, Sendable {", "public enum GatewayFrame: Codable, Sendable {",
...caseLines, ...caseLines,
" case unknown(type: String, raw: [String: AnyCodable])", " case unknown(type: String, raw: [String: AnyCodable])",
initLines, initLines.trimEnd(),
"}", "}",
"", "",
].join("\n"); ].join("\n");

View File

@ -6,18 +6,36 @@ import {
getThreadBindingManager, getThreadBindingManager,
} from "./thread-bindings.js"; } from "./thread-bindings.js";
type ThreadBindingsModule = {
getThreadBindingManager: typeof getThreadBindingManager;
};
async function loadThreadBindingsViaAlternateLoader(): Promise<ThreadBindingsModule> {
const jiti = createJiti(import.meta.url, {
interopDefault: true,
});
try {
return await jiti.import<ThreadBindingsModule>("./thread-bindings.ts");
} catch (error) {
// jiti@2 can fail under ESM test runners when mutating module.require.
if (
!(error instanceof TypeError) ||
!String(error.message).includes("Cannot set property require")
) {
throw error;
}
const fallbackPath = "./thread-bindings.ts?vitest-loader-fallback";
return (await import(/* @vite-ignore */ fallbackPath)) as ThreadBindingsModule;
}
}
describe("thread binding manager state", () => { describe("thread binding manager state", () => {
beforeEach(() => { beforeEach(() => {
threadBindingsTesting.resetThreadBindingsForTests(); threadBindingsTesting.resetThreadBindingsForTests();
}); });
it("shares managers between ESM and Jiti-loaded module instances", () => { it("shares managers between ESM and Jiti-loaded module instances", async () => {
const jiti = createJiti(import.meta.url, { const viaJiti = await loadThreadBindingsViaAlternateLoader();
interopDefault: true,
});
const viaJiti = jiti("./thread-bindings.ts") as {
getThreadBindingManager: typeof getThreadBindingManager;
};
createThreadBindingManager({ createThreadBindingManager({
accountId: "work", accountId: "work",

View File

@ -4,7 +4,7 @@ import process from "node:process";
import { afterEach, describe, expect, it } from "vitest"; import { afterEach, describe, expect, it } from "vitest";
import { attachChildProcessBridge } from "./child-process-bridge.js"; import { attachChildProcessBridge } from "./child-process-bridge.js";
function waitForLine(stream: NodeJS.ReadableStream, timeoutMs = 2000): Promise<string> { function waitForLine(stream: NodeJS.ReadableStream, timeoutMs = 10_000): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let buffer = ""; let buffer = "";
@ -89,11 +89,11 @@ describe("attachChildProcessBridge", () => {
addedSigterm("SIGTERM"); addedSigterm("SIGTERM");
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error("timeout waiting for child exit")), 2_000); const timeout = setTimeout(() => reject(new Error("timeout waiting for child exit")), 10_000);
child.once("exit", () => { child.once("exit", () => {
clearTimeout(timeout); clearTimeout(timeout);
resolve(); resolve();
}); });
}); });
}, 5_000); }, 15_000);
}); });

View File

@ -9,7 +9,7 @@ describe("process supervisor", () => {
backendId: "test", backendId: "test",
mode: "child", mode: "child",
argv: [process.execPath, "-e", 'process.stdout.write("ok")'], argv: [process.execPath, "-e", 'process.stdout.write("ok")'],
timeoutMs: 800, timeoutMs: 2_500,
stdinMode: "pipe-closed", stdinMode: "pipe-closed",
}); });
const exit = await run.wait(); const exit = await run.wait();
@ -54,7 +54,7 @@ describe("process supervisor", () => {
replaceExistingScope: true, replaceExistingScope: true,
mode: "child", mode: "child",
argv: [process.execPath, "-e", 'process.stdout.write("new")'], argv: [process.execPath, "-e", 'process.stdout.write("new")'],
timeoutMs: 800, timeoutMs: 2_500,
stdinMode: "pipe-closed", stdinMode: "pipe-closed",
}); });
@ -88,7 +88,7 @@ describe("process supervisor", () => {
backendId: "test", backendId: "test",
mode: "child", mode: "child",
argv: [process.execPath, "-e", 'process.stdout.write("streamed")'], argv: [process.execPath, "-e", 'process.stdout.write("streamed")'],
timeoutMs: 800, timeoutMs: 2_500,
stdinMode: "pipe-closed", stdinMode: "pipe-closed",
captureOutput: false, captureOutput: false,
onStdout: (chunk) => { onStdout: (chunk) => {