// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========

import { describe, expect, it } from 'vitest'
import { formatSegment, parseSegment, sanitizeName, stripDashes } from './pathing.ts'

describe('sanitizeName', () => {
  it('replaces spaces with underscores', () => {
    expect(sanitizeName('Hello World')).toBe('Hello_World')
  })
  it('replaces unsafe characters with underscores and collapses runs', () => {
    expect(sanitizeName("a/b's c")).toBe('a_b_s_c')
  })
  it('keeps dashes and dots', () => {
    expect(sanitizeName('v1.2-final')).toBe('v1.2-final')
  })
  it('strips leading and trailing underscores', () => {
    expect(sanitizeName('!keep!')).toBe('keep')
  })
  it('returns "unknown" for blank input', () => {
    expect(sanitizeName('   ')).toBe('unknown')
  })
  it('truncates to 100 characters', () => {
    expect(sanitizeName('x'.repeat(150))).toHaveLength(100)
  })
})

describe('stripDashes', () => {
  it('strips dashes from a uuid-like string', () => {
    expect(stripDashes('a-b-c-d-e')).toBe('abcde')
  })
  it('strips all dashes', () => {
    expect(stripDashes('aaa-bbb-ccc-ddd-eee')).toBe('aaabbbcccdddeee')
  })
})

describe('formatSegment', () => {
  it('joins sanitized title and raw id with double underscore', () => {
    expect(formatSegment({ id: 'aaaa1111-2222-3333-4444-555566667777', title: 'My Page' })).toBe(
      'My_Page__aaaa1111-2222-3333-4444-555566667777',
    )
  })
  it('uses "untitled" when the title is empty', () => {
    expect(formatSegment({ id: 'aaaa1111-2222-3333-4444-555566667777', title: '' })).toBe(
      'untitled__aaaa1111-2222-3333-4444-555566667777',
    )
  })
})

describe('parseSegment', () => {
  it('splits into title and id', () => {
    expect(parseSegment('My_Page__aaaa1111-2222-3333-4444-555566667777')).toEqual({
      title: 'My_Page',
      id: 'aaaa1111-2222-3333-4444-555566667777',
    })
  })
  it('splits on the LAST __ separator', () => {
    expect(parseSegment('Page__with__multiple__sep__aaaa1111-2222-3333-4444-555566667777')).toEqual(
      {
        title: 'Page__with__multiple__sep',
        id: 'aaaa1111-2222-3333-4444-555566667777',
      },
    )
  })
  it('throws on segment without a separator', () => {
    expect(() => parseSegment('no-id')).toThrow(/no-id/)
  })
  it('throws when the id part is empty', () => {
    expect(() => parseSegment('Page__')).toThrow(/__/)
  })
})

describe('formatSegment / parseSegment round-trip', () => {
  it('round-trips a sanitized title', () => {
    const page = { id: 'aaaa1111-2222-3333-4444-555566667777', title: 'My_Page' }
    expect(parseSegment(formatSegment(page))).toEqual(page)
  })
  it('round-trips a title containing double underscore in the middle', () => {
    expect(parseSegment('a__b__aaaa1111-2222-3333-4444-555566667777')).toEqual({
      title: 'a__b',
      id: 'aaaa1111-2222-3333-4444-555566667777',
    })
  })
})
