"use strict";

const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const test = require("node:test");

const repositoryRoot = path.resolve(__dirname, "..", "..");
const app = fs.readFileSync(path.join(repositoryRoot, "src",
  "MBN_STOCK_WEBVIEW.LegacyParityApp", "Web", "app.js"), "utf8");

test("expert preview renders a legacy NULL buy amount as an empty value", () => {
  const start = app.indexOf("function expertRecommendationText");
  const end = app.indexOf("function renderExpert", start);
  assert.ok(start >= 0 && end > start);
  const source = app.slice(start, end);
  const format = new Function(`${source}; return expertRecommendationText;`)();

  assert.equal(format({ stockName: "삼성전자", buyAmount: null }), "삼성전자 · ");
  assert.equal(format({ stockName: "카카오", buyAmount: 42000 }), "카카오 · 42000");
  assert.doesNotMatch(source, /\+\s*row\.buyAmount/);
});

test("EList editor keeps NULL blank and sends NULL back without inventing a value", () => {
  assert.match(app, /detail\.value = row\.buyAmount == null \? "" : String\(row\.buyAmount\)/);
  assert.match(app,
    /if \(!text\) \{[\s\S]*?set-operator-catalog-draft-buy-amount[\s\S]*?buyAmount: null/);
  assert.match(app,
    /if \(!operatorCatalogModal\.hidden && renderStateKey === operatorCatalogRenderStateKey\)/);
});
