'use strict';

const test = require('node:test');
const assert = require('node:assert/strict');
const { isoLocal } = require('../iso-local.js');

const OFFSET_ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}[+-]\d{2}:\d{2}$/;

test('carries a local UTC offset, never a Z suffix', () => {
  const s = isoLocal(Date.UTC(2026, 6, 5, 1, 49, 4, 96));
  assert.match(s, OFFSET_ISO);
  assert.ok(!s.endsWith('Z'));
});

test('offset matches the process timezone', () => {
  const ms = Date.UTC(2026, 6, 5, 1, 49, 4, 96);
  const offsetMin = -new Date(ms).getTimezoneOffset();
  const sign = offsetMin >= 0 ? '+' : '-';
  const abs = Math.abs(offsetMin);
  const expected = `${sign}${String(Math.floor(abs / 60)).padStart(2, '0')}:${String(abs % 60).padStart(2, '0')}`;
  assert.ok(isoLocal(ms).endsWith(expected));
});

test('denotes the same instant as toISOString', () => {
  const ms = Date.UTC(2026, 6, 5, 1, 49, 4, 96);
  assert.equal(new Date(isoLocal(ms)).getTime(), ms);
});

test('pads milliseconds to three digits', () => {
  const s = isoLocal(Date.UTC(2026, 0, 1, 0, 0, 0, 7));
  assert.match(s, /\.007[+-]/);
});

test('defaults to now when called with no argument', () => {
  assert.match(isoLocal(), OFFSET_ISO);
});
