All files JanggiAdapter.ts

95.83% Statements 46/48
85.18% Branches 23/27
100% Functions 5/5
95.83% Lines 46/48

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                                                              9x     9x 9x 9x 9x       8x 8x 8x   8x 1x 1x             7x   7x 7x                   7x 7x 8x 8x       7x           6x 1x 1x               5x   5x 5x 6x 6x 1x       5x 1x     5x 8x     5x 8x       5x 8x   4x   4x 1x 1x   4x 1x 1x   4x 4x         1x      
import {
  BaseAdapter,
  IEngineLoader,
  WorkerCommunicator,
  EngineError,
  EngineErrorCode,
  IEngineConfig,
  IEngineSourceConfig,
  ResourceMap,
  createI18nKey,
} from "@multi-game-engines/core";
 
import {
  IJanggiSearchOptions,
  IJanggiSearchInfo,
  IJanggiSearchResult,
} from "@multi-game-engines/domain-janggi";
import { JanggiParser } from "./JanggiParser.js";
import { tCommon as translate } from "@multi-game-engines/i18n-common";
 
/**
 * 2026 Zenith Tier: Janggi (UJCI) Adapter.
 */
export class JanggiAdapter extends BaseAdapter<
  IJanggiSearchOptions,
  IJanggiSearchInfo,
  IJanggiSearchResult
> {
  readonly id: string;
  readonly name: string;
  readonly version: string;
  readonly parser = new JanggiParser();
 
  constructor(config: IEngineConfig) {
    super(config);
    this.id = config.id ?? "janggi";
    this.name = config.name ?? "Janggi Engine";
    this.version = config.version ?? "unknown";
  }
 
  async load(loader?: IEngineLoader, signal?: AbortSignal): Promise<void> {
    this.emitStatusChange("loading");
    try {
      this.validateSources();
 
      if (!loader) {
        const i18nKey = createI18nKey("engine.errors.loaderRequired");
        throw new EngineError({
          code: EngineErrorCode.VALIDATION_ERROR,
          message: translate(i18nKey),
          engineId: this.id,
          i18nKey,
        });
      }
      this.activeLoader = loader;
 
      const sources = this.config.sources;
      Iif (!sources) {
        const i18nKey = createI18nKey("engine.errors.missingSources");
        throw new EngineError({
          code: EngineErrorCode.VALIDATION_ERROR,
          message: translate(i18nKey),
          engineId: this.id,
          i18nKey,
        });
      }
 
      const validSources: Record<string, IEngineSourceConfig> = {};
      for (const [key, value] of Object.entries(sources)) {
        Eif (value && typeof value === "object" && "url" in value) {
          validSources[key] = value as IEngineSourceConfig;
        }
      }
 
      const resources = await this.loadWithProgress(
        loader,
        validSources,
        signal,
      );
 
      if (!resources["main"]) {
        const i18nKey = createI18nKey("engine.errors.missingMainEntryPoint");
        throw new EngineError({
          code: EngineErrorCode.VALIDATION_ERROR,
          message: translate(i18nKey),
          engineId: this.id,
          i18nKey,
        });
      }
 
      this.communicator = new WorkerCommunicator(resources["main"]);
 
      const resourceMap: ResourceMap = {};
      for (const [key, sourceVal] of Object.entries(sources)) {
        const source = sourceVal as IEngineSourceConfig | undefined;
        if (source?.mountPath && resources[key]) {
          resourceMap[source.mountPath] = resources[key]!;
        }
      }
 
      if (Object.keys(resourceMap).length > 0) {
        await this.injectResources(resourceMap);
      }
 
      this.messageUnsubscriber = this.communicator.onMessage((data) => {
        this.handleIncomingMessage(data);
      });
 
      const ujciOkPromise = this.communicator.expectMessage(
        (line) => line === "ujciok",
        { timeoutMs: 10000, signal },
      );
 
      void this.communicator?.postMessage("ujci");
      await ujciOkPromise;
 
      this.emitStatusChange("ready");
    } catch (error) {
      if (this.messageUnsubscriber) {
        this.messageUnsubscriber();
        this.messageUnsubscriber = null;
      }
      if (this.communicator) {
        void this.communicator.terminate();
        this.communicator = null;
      }
      this.emitStatusChange("error");
      throw EngineError.from(error, this.id);
    }
  }
 
  protected async onBookLoaded(url: string): Promise<void> {
    await this.setOption("BookFile", url);
  }
}