#!/usr/bin/env ruby
# $Id: tc_quickml.rb,v 1.15 2004/04/21 04:36:05 eto Exp $
# Copyright (C) 2003-2004 Kouichirou Eto, All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
# You can redistribute it and/or modify it under the terms of the GNU GPL2.

$LOAD_PATH.unshift('../../lib')
$LOAD_PATH.unshift('../../compat')
require 'test/unit'
require 'quickml'
require 'qwik/quickml'

module QuickML
  class QuickML
    def test(t)
      # test private method
      t.assert_equal("test@q...", obfuscate_address("test@qwik.jp"))
      t.assert_equal("http://qwik.jp/test/", qwik_url())
      t.assert_equal("\n-- \nqwik-> http://qwik.jp/test/  ML-> test@qwik.jp\n", generate_footer())
      t.assert_instance_of(Time, last_article_time())

      # test member file
      t.assert_match(%r|data/test/,members|, @members_file)
      t.assert_match(%r|data/test/,count|, @count_file)
      t.assert_equal(false, File.exist?(@members_file))

      save_member_file
      t.assert_equal(true, File.exist?(@members_file))
      t.assert_equal("", members_file_read)

      add_member("test@example.com")
      t.assert_equal("test@example.com\n", members_file_read)

      add_error_member("test@example.com")
      t.assert_match(/test@example.com\n; test@example.com 1 /, members_file_read)

      remove_member("test@example.com")
      t.assert_equal("# test@example.com\n", members_file_read)
    end

    def members_file_read
      open(@members_file){|f| f.read }
    end

    def test_submit(t)
      mail = Mail.new
      mail.mail_from = 'test@example.com'
      mail.add_recipient("test@qwik.jp")
      str = <<"EOT".euctojis
From: "Test User" <test@example.com>
To: "Test Mailing List" <test@qwik.jp>
Subject: Re: [test:1] テスト 
Date: Mon, 3 Feb 2001 12:34:56 +0900

これはテストです。
EOT
      mail.read(str)

      # test_mail
      t.assert_equal(["test@qwik.jp"], mail.recipients)
      t.assert_equal(nil, mail.charset)
      t.assert_equal(nil, mail.content_type)
      t.assert_equal('test@example.com', mail.mail_from)
      t.assert_equal("これはテストです。\n".euctojis, mail.body)
      t.assert_equal(str, mail.bare)
      t.assert_equal('Re: [test:1] テスト '.euctojis, mail["Subject"])

      # test_ml_submit
      t.assert_equal('test', @name)
      encoded_subject = Mail.rewrite_subject(mail["Subject"], @name, 1)
      t.assert_equal("[test:1] Re: =?iso-2022-jp?B?GyRCJUYlOSVIGyhCIA==?=", encoded_subject)
      t.assert_equal(" Re: テスト ", Mail.clean_subject(encoded_subject))
      t.assert_equal("テスト", mail.get_clean_subject)
      t.assert_equal("これはテストです。\n".euctojis, mail.get_plain_body)
      t.assert_equal("Mon, 3 Feb 2001 12:34:56 +0900", mail["Date"])
      subject = qwik_subject(mail)
      t.assert_equal("テスト".euctou8, subject)
      message = qwik_message(mail)
      t.assert_equal("{{mail(test@example.com)\nこれはテストです。\n\n}}\n".euctou8, message)
      qwik_post(subject, message)

      @qwikconfig = Qwik::Config.new(true)
      @site = Qwik::Site.new(@qwikconfig, 'test')
      t.assert_equal('テスト'.euctou8, @site['1'].get_title)
      t.assert_equal("* テスト\n{{mail(test@example.com)\nこれはテストです。\n\n}}\n".euctou8, @site['1'].load)

      # 2nd mail
      mail = Mail.new
      mail.mail_from = 'guest@example.com'
      mail.add_recipient("test@qwik.jp")
      str = <<"EOT".euctojis
From: "Guest User" <guest@example.com>
To: "Test Mailing List" <test@qwik.jp>
Subject: Re: [test:2] テスト 
Date: Mon, 4 Feb 2001 12:34:56 +0900

これもテストです。
EOT
      mail.read(str)

      subject = qwik_subject(mail)
      message = qwik_message(mail)
      qwik_post(subject, message)
      t.assert_equal('テスト'.euctou8, @site['1'].get_title)
      t.assert_equal("* テスト\n{{mail(test@example.com)\nこれはテストです。\n\n}}\n{{mail(guest@example.com)\nこれもテストです。\n\n}}\n".euctou8, @site['1'].load)

      # 3rd mail
      mail = Mail.new
      mail.mail_from = 'eto@example.com'
      mail.add_recipient("test@qwik.jp")
      str = <<"EOT".euctojis
Date: Thu, 15 Apr 2004 00:00:02 +0900
From: Kouichirou Eto <eto@example.com>
To: test@qwik.jp
Subject: 進捗
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-2022-JP"
Content-Transfer-Encoding: 7bit

これもテストであります。
EOT
      mail.read(str)

      subject = qwik_subject(mail)
      message = qwik_message(mail)
      qwik_post(subject, message)
      t.assert_equal('進捗'.euctou8, @site['2'].get_title)
      t.assert_equal("* 進捗\n{{mail(eto@example.com)\nこれもテストであります。\n\n}}\n".euctou8, @site['2'].load)

      # 4th mail
      mail = Mail.new
      mail.mail_from = 'eto@example.com'
      mail.add_recipient("test@qwik.jp")
      str = <<"EOM".euctojis
Date: Thu, 15 Apr 2004 21:00:11 +0900
From: Kouichirou Eto <eto@example.com>
To: test@qwik.jp
Subject: コ

あ
EOM
      mail.read(str)

      subject = qwik_subject(mail)
      message = qwik_message(mail)
      qwik_post(subject, message)
      t.assert_equal("コ".euctou8, @site['3'].get_title)
      t.assert_equal("* コ\n{{mail(eto@example.com)\nあ\n\n}}\n".euctou8, @site['3'].load)
    end

    def close
      @logger.log "[#{@name}]: ML Closed"
    end
  end

  class Logger
    def puts_log(msg) # for test
      return if msg =~ /New ML/
      time = Time.now.strftime("%Y-%m-%dT%H:%M:%S")
      puts "#{time}: #{msg}"
    end
  end

  class Sweeper
    def test(t)
#      t.assert_equal(true, ml_file?("/qwik/data/test"))
#      sweep
    end
  end
end

require 'pathname'

class TC_QuickML < Test::Unit::TestCase
  include QuickML::GetText::GetText
  def setup()
    if $quickml_config.nil?
      $quickml_config  = QuickML::Config.load("../../bin/quickmlrc")
    end
    @config = $quickml_config
    @dir = @config.data_dir.path+"test"
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
    @catalog = @config.catalog
    @message_charset = "iso-2022-jp"
  end

  def test_quickml
    qml = QuickML::QuickML.new(@config, "test@qwik.jp")

    # test private
    qml.test(self)

    # test check mail address
    assert(QuickML::QuickML.valid_name?("test@qwik.jp"))
    assert(!QuickML::QuickML.valid_name?("test@qwik@jp"))
    assert(QuickML::QuickML.valid_name?("test"))
    assert(!QuickML::QuickML.valid_name?("te.st"))

    # test_sweeper
    sweeper = QuickML::Sweeper.new(@config)
    assert_instance_of(QuickML::Sweeper, sweeper)
    sweeper.test(self)

    # test_encode_field
    assert_equal('t', QuickML::Mail.encode_field('t'))
    assert_equal("=?iso-2022-jp?B?GyRCJCIbKEI=?=", QuickML::Mail.encode_field('あ'))
    assert_equal("=?iso-2022-jp?B?GyRCJCIkJBsoQg==?=", QuickML::Mail.encode_field('あい'))
    assert_equal("=?iso-2022-jp?B?GyRCJCIkJCQmJCgkKiQrJC0kLyQxJDMbKEI=?=\n =?iso-2022-jp?B?GyRCJDUkNyQ5JDskPRsoQg==?=", QuickML::Mail.encode_field('あいうえおかきくけこさしすせそ'))

    # test submit
    qml.test_submit(self)
  end

  include QuickML::GetText::GetText
  def test_gettext
    assert_equal("使い方: a\n".euctojis, _("Info: %s\n", "a"))
    assert_equal("使い方: a\n".euctojis, gettext("Info: %s\n", "a"))
    assert_equal("もし承認する場合、\n".euctojis, _("If you agree, then,\n"))
  end

  def teardown()
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
  end
end
