#!/usr/bin/env ruby
# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/test-module-quickml"
require "qwik/quickml"

class TC_SubmitBasic < Test::Unit::TestCase
  include TestQuickMLModule

  def test_submit_1
    qml = QuickML::QuickML.new(@ml_config, "test@example.com")
    qml.setup_test_config

    # 1st mail
    str = <<"EOT"
From: "Test User" <user@e.com>
To: "Test Mailing List" <test@example.com>
Subject: Re: [test:1] Test Mail
Date: Mon, 3 Feb 2001 12:34:56 +0900

This is a test.
EOT
    mail = ml_mail("user@e.com", "test@example.com", str)
    assert_equal("[test:1] Re: Test Mail",
		 QuickML::Mail.rewrite_subject(mail["Subject"], qml.name, 1))
    qml.qwik_post(mail, true)
    assert_equal("Test Mail", @site["1"].get_title)
    assert_equal("* Test Mail\n{{mail(user@e.com,0)\nThis is a test.\n}}\n",
		 @site["1"].load)

    # 2nd mail
    str = <<"EOT"
From: "Guest User" <guest@example.com>
To: "Test Mailing List" <test@example.com>
Subject: Re: [test:2] Test Mail
Date: Mon, 4 Feb 2001 12:34:56 +0900

This is a test too.
EOT
    mail = ml_mail("guest@example.com", "test@example.com", str)

    qml.qwik_post(mail, true)
    assert_equal("* Test Mail\n{{mail(user@e.com,0)\nThis is a test.\n}}\n{{mail(guest@example.com,0)\nThis is a test too.\n}}\n", @site["1"].load) # the new mail is added.
  end

  def test_submit_3
    qml = QuickML::QuickML.new(@ml_config, "test@example.com")
    qml.setup_test_config
    str = <<"EOT"
Date: Thu, 15 Apr 2004 00:00:02 +0900
From: Kouichirou Eto <eto@example.com>
To: test@example.com
Subject: Another mail.
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-2022-JP"
Content-Transfer-Encoding: 7bit

This is also a test.
EOT
    mail = ml_mail("eto@example.com", "test@example.com", str)
    qml.qwik_post(mail, true)
    assert_equal("Another mail.", @site["1"].get_title)
    assert_equal("* Another mail.\n{{mail(eto@example.com,0)\nThis is also a test.\n}}\n",
		 @site["1"].load)
  end

  def test_submit_4
    qml = QuickML::QuickML.new(@ml_config, "test@example.com")
    qml.setup_test_config
    str = <<"EOT"
Date: Thu, 15 Apr 2004 21:00:11 +0900
From: Kouichirou Eto <eto@example.com>
To: test@example.com
Subject: Ko

a
EOT
    mail = ml_mail("eto@example.com", "test@example.com", str)
    qml.qwik_post(mail, true)
    assert_equal("Ko", @site["Ko"].get_title)
    assert_equal("* Ko\n{{mail(eto@example.com,0)\na\n}}\n",
		 @site["Ko"].load)
  end
end
