All files / src/mocks MockAdapter.ts

98.14% Statements 53/54
80% Branches 12/15
77.77% Functions 21/27
98.14% Lines 53/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182                                                      26x 26x       26x                   26x 26x     26x 26x       8x 8x 8x       4x 4x       2x       6x                       7x 7x   7x 7x 7x 6x 6x       6x 6x 6x 6x 6x         7x   1x       7x                   10x 1x 1x   10x 1x 1x 1x               10x 10x       7x 7x 7x       2x     20x     20x     2x     20x                 26x 26x   6x     1x     1x     3x     1x          
import { BaseAdapter } from "../adapters/BaseAdapter.js";
import {
  IBaseSearchOptions,
  IBaseSearchInfo,
  IBaseSearchResult,
  ISearchTask,
  IProtocolParser,
  IEngineConfig,
  MiddlewareCommand,
  EngineStatus,
  ILoadProgress,
  ITelemetryEvent,
  ILicenseInfo,
  EngineErrorCode,
} from "../types.js";
import { createMove } from "../protocol/ProtocolValidator.js";
import { EngineError } from "../errors/EngineError.js";
import { WorkerCommunicator } from "../workers/WorkerCommunicator.js";
 
/**
 * CI/CD および開発用の軽量なモックアダプター。
 */
export class MockAdapter extends BaseAdapter<
  IBaseSearchOptions,
  IBaseSearchInfo,
  IBaseSearchResult
> {
  public override readonly version: string = "1.0.0-mock";
  public override readonly engineLicense: ILicenseInfo = {
    name: "MIT",
    url: "",
  };
  public override readonly adapterLicense: ILicenseInfo = {
    name: "MIT",
    url: "",
  };
  public override readonly parser: IProtocolParser<
    IBaseSearchOptions,
    IBaseSearchInfo,
    IBaseSearchResult
  >;
 
  private mockPendingReject: ((err: unknown) => void) | null = null;
  private activeTimer: ReturnType<typeof setTimeout> | null = null;
 
  constructor(config: IEngineConfig = {}) {
    super(config.id ?? "mock-engine", config.name ?? "Mock Engine", config);
    this.parser = new MockParser();
  }
 
  public async load(_loader?: unknown): Promise<void> {
    this.emitStatusChange("loading");
    this._status = "ready";
    this.emitStatusChange("ready");
  }
 
  public setStatus(status: EngineStatus): void {
    this._status = status;
    this.emitStatusChange(status);
  }
 
  public testHandleIncomingMessage(data: unknown): void {
    this.handleIncomingMessage(data);
  }
 
  public setCommunicator(comm: unknown): void {
    this.communicator = comm as WorkerCommunicator;
  }
 
  protected async onInitialize(): Promise<void> {}
  protected async onSearchRaw(_command: unknown): Promise<void> {}
  protected async onStop(): Promise<void> {}
  protected async onDispose(): Promise<void> {}
  protected async onBookLoaded(_url: string): Promise<void> {}
 
  public searchRaw(
    _command: MiddlewareCommand,
  ): ISearchTask<IBaseSearchInfo, IBaseSearchResult> {
    this._status = "busy";
    this.emitStatusChange("busy");
 
    const resultPromise = new Promise<IBaseSearchResult>((resolve, reject) => {
      this.mockPendingReject = reject;
      this.activeTimer = setTimeout(() => {
        Eif (this._status === "busy") {
          const result: IBaseSearchResult = {
            bestMove: createMove("e2e4"),
            raw: "bestmove e2e4",
          };
          resolve(result);
          this._status = "ready";
          this.emitStatusChange("ready");
          this.mockPendingReject = null;
          this.activeTimer = null;
        }
      }, 10);
    });
 
    const infoStream: AsyncIterable<IBaseSearchInfo> = {
      [Symbol.asyncIterator]: async function* () {
        yield { raw: "info depth 1" };
      },
    };
 
    return {
      info: infoStream,
      result: resultPromise,
      stop: () => {
        void this.stop();
      },
    };
  }
 
  public async stop(): Promise<void> {
    if (this.activeTimer) {
      clearTimeout(this.activeTimer);
      this.activeTimer = null;
    }
    if (this.mockPendingReject) {
      const reject = this.mockPendingReject;
      this.mockPendingReject = null;
      reject(
        new EngineError({
          code: EngineErrorCode.SEARCH_ABORTED,
          message: "Stopped",
          engineId: this.id,
        }),
      );
    }
    this._status = "ready";
    this.emitStatusChange("ready");
  }
 
  public async dispose(): Promise<void> {
    await this.stop();
    this._status = "terminated";
    this.emitStatusChange("terminated");
  }
 
  onStatusChange(callback: (status: EngineStatus) => void): () => void {
    return super.onStatusChange(callback);
  }
  onInfo(callback: (info: IBaseSearchInfo) => void): () => void {
    return super.onInfo(callback);
  }
  onSearchResult(callback: (result: IBaseSearchResult) => void): () => void {
    return super.onSearchResult(callback);
  }
  onProgress(callback: (progress: ILoadProgress) => void): () => void {
    return super.onProgress(callback);
  }
  onTelemetry(callback: (event: ITelemetryEvent) => void): () => void {
    return super.onTelemetry(callback);
  }
}
 
class MockParser implements IProtocolParser<
  IBaseSearchOptions,
  IBaseSearchInfo,
  IBaseSearchResult
> {
  isReadyCommand = "isready";
  readyResponse = "readyok";
  createSearchCommand(_options: IBaseSearchOptions): MiddlewareCommand {
    return "go";
  }
  createStopCommand(): MiddlewareCommand {
    return "stop";
  }
  createOptionCommand(_name: string, _value: unknown): MiddlewareCommand {
    return "setoption";
  }
  parseInfo(line: unknown): IBaseSearchInfo | null {
    return typeof line === "string" ? { raw: line } : null;
  }
  parseResult(line: unknown): IBaseSearchResult | null {
    return typeof line === "string"
      ? { bestMove: createMove("e2e4"), raw: line }
      : null;
  }
}