import { describe, it, expect } from 'vitest';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';

// Guard for the "スペース" → "ワークスペース" display-term rename
// (chore/rename-space-to-workspace). Scoped to the layout nav catalog, the
// single i18n value that carries the product term we swept. Code identifiers
// (space_id, route id 'spaces', testids) are NOT display text and are out of
// scope here — we only assert on the rendered string value.

const here = dirname(fileURLToPath(import.meta.url));

function readJson(rel: string): Record<string, unknown> {
  return JSON.parse(readFileSync(join(here, rel), 'utf8'));
}

// Match a standalone "スペース" that is NOT already part of "ワークスペース".
const STANDALONE_SPACE = /(?<!ワーク)スペース/;

describe('i18n layout: スペース → ワークスペース rename guard', () => {
  it('ja layout nav uses ワークスペース for the spaces label', () => {
    const ja = readJson('./locales/ja/layout.json') as { nav: Record<string, string> };
    expect(ja.nav.spaces).toBe('ワークスペース');
    // No standalone "スペース" should survive anywhere in the ja layout values.
    const values = JSON.stringify(ja);
    expect(STANDALONE_SPACE.test(values)).toBe(false);
  });

  it('en layout nav uses Workspaces for the spaces label', () => {
    const en = readJson('./locales/en/layout.json') as { nav: Record<string, string> };
    expect(en.nav.spaces).toBe('Workspaces');
    // The product term "Space"/"Spaces" must not denote the concept here.
    expect(/\bSpaces?\b/.test(JSON.stringify(en))).toBe(false);
  });
});

describe('i18n spaces namespace: term-rule guard', () => {
  // spaces.json carries ~245 user-facing strings for the workspace UI — the place
  // where the "スペース → ワークスペース" / "Space → Workspace" term rule matters
  // most. Guard both locales so future edits can't reintroduce the bare concept
  // noun. ("Workspace"/"ワークスペース" are fine: \b and the (?<!ワーク) lookbehind
  // exclude them.)
  it('ja spaces values never use a standalone スペース for the concept', () => {
    const ja = readJson('./locales/ja/spaces.json');
    expect(STANDALONE_SPACE.test(JSON.stringify(ja))).toBe(false);
  });

  it('en spaces values never use "Space"/"Spaces" as the concept noun', () => {
    const en = readJson('./locales/en/spaces.json');
    expect(/\bSpaces?\b/.test(JSON.stringify(en))).toBe(false);
  });
});
