/*
 * Copyright (C) Sapphirecode - All Rights Reserved
 * This file is part of graphviz-builder which is released under MIT.
 * See file 'LICENSE' for full license details.
 * Created by Timo Hocker <timo@scode.ovh>, May 2020
 */

import test from 'ava';
import { Node, Color } from '../lib';

const serialized_simple
= 'bar_foo [label="baz", style="dashed", color="#00ff00"]';
const serialized_table = `bar_foo [label=<<table>
  <tr><td>foo</td><td>bar</td><td>baz</td></tr>
  <tr><td>bar</td><td>baz</td><td>foo</td></tr>
  <tr><td>baz</td><td>foo</td><td>bar</td></tr>
</table>>, style="invis", color="#00ff00"]`;

test ('serialize simple', (t) => {
  const g = new Node ('foo', 'bar', 'baz');

  g.color = Color.green;
  g.style = 'dashed';

  const serialized = g.toString ();

  t.is (g.full_name, 'bar_foo');
  t.is (serialized, serialized_simple);
});

test ('serialize table', (t) => {
  const g = new Node ('foo', 'bar', 'baz');

  g.color = Color.green;
  g.style = 'invis';

  g.table_contents = [
    [
      'foo',
      'bar',
      'baz'
    ],
    [
      'bar',
      'baz',
      'foo'
    ],
    [
      'baz',
      'foo',
      'bar'
    ]
  ];
  g.is_table = true;

  const serialized = g.toString ();

  t.is (g.full_name, 'bar_foo');
  t.is (serialized, serialized_table);
});

test ('adhere to naming convention', (t) => {
  const n = new Node ('invalid.name', 'parent');
  t.is (n.name, 'invalidname');
});

test ('throw on invalid name', (t) => {
  t.throws (() => {
    const n = new Node ('564#+-.,/@', 'parent');
    return n.toString ();
  }, { message: 'invalid node name 564#+-.,/@' });
});

test ('leave numbers after the first letter', (t) => {
  const n = new Node ('i123nvalid.name', 'parent');
  t.is (n.name, 'i123nvalidname');
});