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

View File

@@ -6,18 +6,36 @@ import {
getThreadBindingManager,
} 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", () => {
beforeEach(() => {
threadBindingsTesting.resetThreadBindingsForTests();
});
it("shares managers between ESM and Jiti-loaded module instances", () => {
const jiti = createJiti(import.meta.url, {
interopDefault: true,
});
const viaJiti = jiti("./thread-bindings.ts") as {
getThreadBindingManager: typeof getThreadBindingManager;
};
it("shares managers between ESM and Jiti-loaded module instances", async () => {
const viaJiti = await loadThreadBindingsViaAlternateLoader();
createThreadBindingManager({
accountId: "work",

View File

@@ -4,7 +4,7 @@ import process from "node:process";
import { afterEach, describe, expect, it } from "vitest";
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) => {
let buffer = "";
@@ -89,11 +89,11 @@ describe("attachChildProcessBridge", () => {
addedSigterm("SIGTERM");
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", () => {
clearTimeout(timeout);
resolve();
});
});
}, 5_000);
}, 15_000);
});

View File

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