({ className, commands, events, enhancement })
| 890 | } |
| 891 | |
| 892 | function generateClass({ className, commands, events, enhancement }) { |
| 893 | const lines = [] |
| 894 | |
| 895 | lines.push(`export class ${className} {`) |
| 896 | lines.push(` private constructor(private readonly bidi: BidiConnection) {}`) |
| 897 | lines.push('') |
| 898 | lines.push(` static async create(driver: unknown): Promise<${className}> {`) |
| 899 | lines.push( |
| 900 | ` const caps = await (driver as { getCapabilities(): Promise<{ get(key: string): unknown }> }).getCapabilities()`, |
| 901 | ) |
| 902 | lines.push(` if (!caps.get('webSocketUrl')) {`) |
| 903 | lines.push(` throw new Error('WebDriver instance must support BiDi protocol')`) |
| 904 | lines.push(` }`) |
| 905 | lines.push(` const bidi = await (driver as { getBidi(): Promise<BidiConnection> }).getBidi()`) |
| 906 | lines.push(` return new ${className}(bidi)`) |
| 907 | lines.push(` }`) |
| 908 | |
| 909 | for (const cmd of commands) { |
| 910 | const override = enhancement.extraMethods?.[cmd.methodName] |
| 911 | lines.push('') |
| 912 | lines.push(override ?? generateCommandMethod(cmd)) |
| 913 | } |
| 914 | |
| 915 | for (const evt of events) { |
| 916 | const override = enhancement.extraMethods?.[evt.onMethodName] |
| 917 | lines.push('') |
| 918 | lines.push(override ?? generateEventMethod(evt)) |
| 919 | } |
| 920 | |
| 921 | if (enhancement.extraMethods) { |
| 922 | const knownNames = new Set([...commands.map((c) => c.methodName), ...events.map((e) => e.onMethodName)]) |
| 923 | for (const [name, body] of Object.entries(enhancement.extraMethods)) { |
| 924 | if (!knownNames.has(name)) { |
| 925 | // Purely additive method not tied to a command or event. |
| 926 | lines.push('') |
| 927 | lines.push(body) |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | lines.push(`}`) |
| 933 | return lines.join('\n') |
| 934 | } |
| 935 | |
| 936 | function generateCommandMethod(cmd) { |
| 937 | const { methodName, methodStr, paramsTypeName, hasParams, resultTypeName } = cmd |
no test coverage detected